I’m in the process of understanding python code in Blender 2.5 and I’m sorry if this question has an obvious answer. I’ve spent hours searching the net and poking in the Blender console, browsing through object properties and stuff with no avail.
How can I get a vertex’ current color, how can I change it?
you access the face then the vertex colours for the vertices of that face…
here’s a quick example for inverting vertex colours on a selection:
from Blender import Mesh, Object
for ob in Object.GetSelected():
if ob.getType() == 'Mesh':
me = ob.getData(mesh=1)
for f in me.faces:
if f.sel:
for c in f.col:
c.r = 255 - c.r
c.g = 255 - c.g
c.b = 255 - c.b
Thanks! I had no idea vertex colours were part of the face.
Nevertheless, why do I get: “Import Error : no Module named Blender” for line 1?
Also, in the console, I can’t find the “col” property in faces:
>>> dir(ob.data.faces[0].col)
Traceback (most recent call last):
File “<console>”, line 1, in <module>
AttributeError: ‘MeshFace’ object has no attribute ‘col’
import bpy
for ob in bpy.context.selected_editable_objects:
if ob.type =='MESH':
me = ob.data
for f in me.faces:
if f.sel:
for c in f.col:
c.r = 255 - c.r
c.g = 255 - c.g
c.b = 255 - c.b
Hi, the following code may be helpful. It assigns a colour to all the currently selected verts of a specific mesh…
import bpy
import random
def colourVertex(object, vertex, colour, layer=0):
"""vertex of type MeshVertex, colour a 3-tuple, layer the vertex color layer (must exist)"""
# get (faceindex,vertindex) for each vert
fv_pairs = []
for f in object.data.faces:
try:
fv_pairs.append((f.index, list(f.verts).index(vertex.index)))
except ValueError:
pass
# finally assign colour to the appropriate fields in vertex_colors
for (f,v) in fv_pairs:
mesh_colour = object.data.vertex_colors[layer].data[f]
c = {0:mesh_colour.color1,1:mesh_colour.color2,2:mesh_colour.color3,3:mesh_colour.color4}[v]
for i in range(3):
c[i] = colour[i]
obj = bpy.data.objects["Mesh"]
for v in obj.data.verts:
if v.selected:
colourVertex(obj,v,(1,1,1))