Skip to main content
added 168 characters in body
Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

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 enter image description here result on 10 x 10 default griduv_sphere, cube and torus

import bpy
import bmesh
from random import uniform
context = bpy.context
mesh = context.object.data
bm = bmesh.new()
bm.from_mesh(mesh)

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 / len(bm.verts)L 
        print("Vert:", loop.vert.index, x)
        loop[color_layer] = (x, x, x, 1)
bm.to_mesh(mesh)  

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}

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 10 x 10 default grid

import bpy
import bmesh
from random import uniform
context = bpy.context
mesh = context.object.data
bm = bmesh.new()
bm.from_mesh(mesh)

color_layers = bm.loops.layers.color
color_layer = color_layers.get("color") or color_layers.new("color")

for face in bm.faces:
    for loop in face.loops:        
        x = loop.vert.index / len(bm.verts)
        print("Vert:", loop.vert.index, x)
        loop[color_layer] = (x, x, x, 1)
bm.to_mesh(mesh) 

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()
bm.from_mesh(mesh)

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)
bm.to_mesh(mesh)  

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}
Source Link
batFINGER
  • 85.2k
  • 10
  • 114
  • 244

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 10 x 10 default grid

import bpy
import bmesh
from random import uniform
context = bpy.context
mesh = context.object.data
bm = bmesh.new()
bm.from_mesh(mesh)

color_layers = bm.loops.layers.color
color_layer = color_layers.get("color") or color_layers.new("color")

for face in bm.faces:
    for loop in face.loops:        
        x = loop.vert.index / len(bm.verts)
        print("Vert:", loop.vert.index, x)
        loop[color_layer] = (x, x, x, 1)
bm.to_mesh(mesh)