I am new to blender python interface. I want to assign a color to a polygon(or face) based on its vertices locations. I have looked around and came up with the following code by referencing Setting Vertex Color via Python. However, I found that len(mesh.data.vertex_colors[0].data) != len(mesh.data.polygons). I don’t quite understand why this is the case, and how can I fix this problem.
faces = mesh.data.polygons
vertices = mesh.data.vertices
color_layer = mesh.data.vertex_colors.active
for i, f in enumerate(mesh.data.vertex_colors[0].data):
v_ind = faces[i].vertices
vertex1 = vertices[v[0]].co
vertex2 = vertices[v[1]].co
vertex3 = vertices[v[2]].co
f.color = get_face_color(vertex1, vertex2, vertex3)
UPDATE: I found out that len(mesh.data.vertex_colors[0].data) = 3*len(mesh.polygons), does that mean the code should be something like this instead?
faces = mesh.data.polygons
vertices = mesh.data.vertices
face_loop_ind = 0
for i, f in enumerate(faces):
flidx0 = face_loop_ind
flidx1 = face_loop_ind+1
flidx2 = face_loop_ind+2
face_loop_ind += 3
v_ind = faces[i].vertices
mesh.data.vertex_colors[0].data[flidx0].color = get_vert_color(vertices[v_ind[0]].co)
mesh.data.vertex_colors[0].data[flidx1].color = get_vert_color(vertices[v_ind[1]].co)
mesh.data.vertex_colors[0].data[flidx2].color = get_vert_color(vertices[v_ind[2]].co)