Skip to content

Instantly share code, notes, and snippets.

@kruncher
Last active November 8, 2021 10:06
Show Gist options
  • Save kruncher/acb223625d8a0d803b151feb4cd555d3 to your computer and use it in GitHub Desktop.
Save kruncher/acb223625d8a0d803b151feb4cd555d3 to your computer and use it in GitHub Desktop.
#unity #blender A script for Blender that allows you to swap values of vertex colors with uv coordinates so that a custom Unity shader can make use of two sets of vertex colors.
# Copyright (c) Rotorz Limited. All rights reserved.
# License: MIT
import bpy
bl_info = {
'name': 'Swap Vertex Colors with UVs',
'author': 'Rotorz Limited',
'version': (0, 1),
'blender': (2, 7, 7),
'description': 'Swaps vertex color data with uv1/uv2 data',
'category': 'Vertex Paint'
}
class SwapVertexColorsWithUVs(bpy.types.Operator):
bl_label = "Swap Vertex Colors with UVs"
bl_idname = "object.swap_vertex_colors_with_uvs"
bl_description = "Swaps vertex color data with uv1/uv2 data"
def execute(self, context):
self.report({'INFO'}, "Swapping vertex color data with uv1/uv2 data")
# start in object mode
obj = context.scene.objects.active
mesh = obj.data
# make sure that the object has vertex colors
if not mesh.vertex_colors:
mesh.vertex_colors.new()
# make sure that the object has uv data
while len(mesh.uv_textures.values()) < 2:
mesh.uv_textures.new()
# grab the active vertex color data layer
color_layer = mesh.vertex_colors.active
# grab the active uv data layer
uv_layers = mesh.uv_layers.values()
i = 0
for poly in mesh.polygons:
for idx in poly.loop_indices:
tempR = color_layer.data[i].color.r
tempG = color_layer.data[i].color.g
tempB = color_layer.data[i].color.b
#tempA = color_layer.data[i].color.a
color_layer.data[i].color.r = uv_layers[0].data[i].uv.x
color_layer.data[i].color.g = uv_layers[0].data[i].uv.y
color_layer.data[i].color.b = uv_layers[1].data[i].uv.x
#color_layer.data[i].color.a = uv_layers[1].data[i].uv.y
uv_layers[0].data[i].uv.x = tempR
uv_layers[0].data[i].uv.y = tempG
uv_layers[1].data[i].uv.x = tempB
#uv_layers[1].data[i].uv.y = tempA
i += 1
# set to vertex paint mode to see the result
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
return {'FINISHED'}
def register():
bpy.utils.register_class(SwapVertexColorsWithUVs)
bpy.context.window_manager.keyconfigs.active.keymaps['Vertex Paint'].keymap_items.new('object.swap_vertex_colors_with_uvs',value='PRESS',type='F1',ctrl=True,alt=False,shift=True,oskey=False)
def unregister():
bpy.utils.unregister_class(SwapVertexColorsWithUVs)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment