Imagine you have the following situation: you want to color the vertices relative to a certain vertex group x in red. I used the following function the problem is that it is not very efficient in fact blender stops working immediately, I'm working on a mesh with about 230,000 vertices and 500,000 faces (I just started developing addons blender and I still have a long way to go). PS. This operation will not be repeated once but many more depending on how many vertex groups I need to color
name_vertex_group = "myVG"
name_mesh = "myMesh"
mesh = bpy.data.objects[name_mesh].data
vertex_group = bpy.data.objects[name_mesh].vertex_groups[name_vertex_group].index
bpy.ops.object.mode_set(mode = "VERTEX_PAINT")
index_vertex = []
vs = [vertex for vertex in mesh.vertices if(vertex.groups)]
for vertex in vs:
for vgroup in vertex.groups:
if(vgroup.group==vertex_group):
index_vertex.append(vertex.index)
break
for face in mesh.polygons:
for vert in index_vertex:
for loop_index in face.loop_indices:
loop_vert_index = mesh.loops[loop_index].vertex_index
if vert == loop_vert_index:
mesh.vertex_colors.active.data[loop_index].color = [255, 0, 0, 1]
break
bpy.ops.object.mode_set(mode = "EDIT")