I need to export vertex colors from Blender to a custom mesh format. To do it, I need to create a dictionary where the vertex index is a key and color is a value, as proof of concept I wrote this but it's extremely inefficient. How can this be implemented better?
def _get_vertex_colors(blender_mesh):
mesh = blender_mesh.data
colors = {}
try:
color_layer = mesh.vertex_colors[0]
except:
return colors
for poly in mesh.polygons:
for loop_index in poly.loop_indices:
b = round(color_layer.data[loop_index].color[0]*255)
g = round(color_layer.data[loop_index].color[1]*255)
r = round(color_layer.data[loop_index].color[2]*255)
a = round(color_layer.data[loop_index].color[3]*255)
colors[mesh.loops[loop_index].vertex_index] = (r,g,b,a)
return colors