Setting Vertex Color via Python

I just want to know how do people set vertex colors using the Blender 2.5 API? I am doing this:


from bpy import *
from random import *

mesh = data.objects["Mesh"].data

# loop through each face
for f in mesh.vertex_colors[0].data:
	# assign a random RGB to each each vertex of the quad
	f.color1= [random(), random(), random()]
	f.color2= [random(), random(), random()]
	f.color3= [random(), random(), random()]
	f.color4= [random(), random(), random()]

http://mikepan.com/files/misc/vertex.png

What I want to achieve is at the top (where the vertex color is automatically blended and shared across vertices), but when I run the code, i get the bottom image because I have to assign a unique color to each vertex of the face. How do I force Blender to do an interpolation?

hi,

try setting a color for every vertex, then setting that color for every vertex in every face


from bpy import *
from random import *

mesh = data.objects["Mesh"].data
vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces

vColour = [(random(),random(),random()) for i in mesh.verts]

vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces

for i in range(len(faces)):
    v = vertexColour[i]
    f = faces[i].raw_verts
    v.color1 = vColour[f[0]]
    v.color2 = vColour[f[1]]
    v.color3 = vColour[f[2]]
    v.color4 = vColour[f[3]]


i dont know if this works, 'cuz i dont even know how to add a vertex colour ‘stuff’ to an object, so i couldnt test it. but still hope this helped

i tried the first script and get an error that index out of range !

i did add a plane subdivided a few time
can somone tell me what is missing here ?

Thanks

You’ll probably need to manually create a vertex color layer first.

Dreampainter, Thanks but that didn’t seems to work.

thats what i get for not testing a script :o

the face.raw_verts was supposed to be face.verts_raw, then the script worked.


from bpy import *
from random import *

# select the currently selected mesh (be sure its a mesh object thought :P)
mesh = context.active_object.data
# mesh = data.objects["Mesh"].data   # for using a predefined name

# vertex colour data
vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces

# create random vertex colours
vColour = [(random(),random(),random()) for i in mesh.verts]

vertexColour = mesh.vertex_colors[0].data
faces = mesh.faces

# asign colours to verts
for i in range(len(faces)):
    v = vertexColour[i]
    f = faces[i].verts_raw
    v.color1 = vColour[f[0]]
    v.color2 = vColour[f[1]]
    v.color3 = vColour[f[2]]
    v.color4 = vColour[f[3]]

hope that helps

@ ricky blender
adding a vertex colour layer is simple:
in 3d view: select object
in properties view in material tab: add material (or use existing one)

  • at options enable Vertex color paint
    in 3d view go to vertex paint mode, using the drop down menu in header ( the one with object mode, edit mode, pose mode, etc…) and go to object mode again
    vertex colour layer added :yes:

have fun

working nicely thanks

weird colors but nice anyway

with a script is there anyway to add this vertex layer in the script
instead of doing it manually in viewport ?

but this does not do color bleed from one quad to the next !
thus may requires to do a sort of color gradient
and i don’t think remember that you can do that with vertex paniting
mind you this is probably the oldest way to color faces in blender !

but might be easier and possible to do it with the paint brush blur in UV editor may be !

happy 2.5

All your scripts do not work at my latest Blender 2.5 (from yesterday) (and I do not understand either)

This is a .blend you can play with: http://dl.dropbox.com/u/8767830/5JuliVertexColors.blend

EDIT, removed because of not working in latest SVN 365**

i tried your blend and with me, i build my own and im at r29994, every thing works.

about my code:

the current way vertex colours are stored is for every face per corner.not per vertex. as such you have to set the same colour for every corner that makes up a vertex, to get smooth colour transitions.

so i first defined a colour for every vertex.
then for every face, i check which vertex is which corner and give the corner the colour of the vertex.

it so happens to be that the order of verts that make up a face, is the same order in the vertex_colour data, and thats what i use

hope that helps

Ah, understood:
Your code (fresh start of blender, nothing done) gives this error message:
Traceback (most recent call last):
File “C:\Users\Peter\Blender\Lucy\Levels\Init.blend/Text”, line 10, in <module
>
IndexError: bpy_prop_collection[index]: index 0 out of range

Reason not yet available so add the next line (without # naturally :yes: )
#mesh.add_vertex_color()
removing the # all works nicely AFTER checking: Vertex Colour Paint in the objects panel…

??the second #vertexColour = mesh.vertex_colors[0].data really needed (line 15,or 16)?
It worked without!

And now: how to animate this? Would give a nice ‘film’ :wink:

I mean animated color change by vertex color manipulation.

Thanks Dreampainter that’s exactly what I wanted.

Thanks.

The format I use supports RGBA vertex colors, but Blender only seems to support RGB. Information will get lost after importing and exporting again. Can anything be done?