Script from this answer encodes polygons ID to vertex colors:

import bpy

obj = bpy.context.object
obj_data = obj.data
group_name = 'face_ID'

#check for existing group with the same name
if None == obj_data.vertex_colors.get(group_name): 
color_map =  obj_data.vertex_colors[group_name]

#get faces' number 
count = len(obj_data.polygons)

for poly in obj_data.polygons:

    ID = poly.index / count  #for normalized results
    color = (ID, ID, ID)
    for loop_ind in poly.loop_indices:
        color_map.data[loop_ind].color = color 

Can this be done with vertex indices as well?

I've tried to change poly to vert and obviously script doesn't work.


1 Answer 1


Using bmesh.

Similarly to this answer loop thru the face loops and set the associated vertex to a color based on its index. enter image description here result on default uv_sphere, cube and torus

import bpy
import bmesh

context = bpy.context
mesh = context.object.data
bm = bmesh.new()

color_layers = bm.loops.layers.color
color_layer = color_layers.get("color") or color_layers.new("color")
L = len(bm.verts) - 1
for face in bm.faces:
    for loop in face.loops:        
        x = loop.vert.index / L 
        print("Vert:", loop.vert.index)
        loop[color_layer] = (x, x, x, 1)

Note: for large meshes could make a a look up table for vert and color to avoid calculating for same vert per face loop.

LUT = {v : v.index / L for v in bm.verts}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .