Changing Vertex Colors on a Mesh

It’s been a while since I’ve worked with blender, so when I was trying to use the old method to colorize the vertices of a mesh (circa version 2.59) it didn’t work so well. This is good because the old way was pretty backwards; you’d have to iterate through the faces of said mesh, then iterate through the vertices of the face, and change the colors that way.

My question is: how is this done now? It’s hopefully a lot more straight forward now, but I haven’t written any scripts for blender since 2.59 so I don’t know about any of the changes.

Since 2.63, you iterate over the loops of a mesh. You could do that per face for instance:

import bpyfrom mathutils import Color


me = bpy.context.object.data
if me.vertex_colors.active is None:
    me.vertex_colors.new()


color = Color()
color.hsv = (0, 1, 1)
hue_step = 1 / len(me.polygons)


for poly in me.polygons:
    for loop in poly.loop_indices:
        me.vertex_colors.active.data[loop].color = color
    color.h += hue_step
me.update()

Codemax, this is remarkable coincidence. I was just looking over for you on irc to figure this out :wink: Thanks for your help.

This looks like what I am looking for, but I am struggling to understand how I can modify this code to write the color information from all polygons (of a selected object) into a file.

I have adjusted the code a bit…



for a in list(bpy.context.selected_objects):
    if a.type == "MESH":
    a_object_name = a.name
    a_object = bpy.data.objects[a_object_name] 
    a_data_name = a_object.data.name
    a_data = bpy.data.meshes[a_data_name]  

for poly in list(a_data.polygons):
    file_handler.write('c')
    for loop in poly.loop_indices:
        farbwert = a_data.vertex_colors.active.data[loop].color
        file_handler.write(farbwert)
    file_handler.write('
')


No matter if I use a_object or a_data to call “vertex_colors.active.data” I get the error message:

“NonType” object has no attribute “Data”

Somehow I still do not fully understand how to handle objects and meshes and think the error is somewhere there. Any advise?

for ob in bpy.context.selected_objects:
    if ob.type == "MESH":

        me = ob.data
        vcol =  me.vertex_colors.active

        if vcol is None:
            continue

        for poly in me.polygons:
            file_handler.write('c')
            for loop in poly.loop_indices:
                farbwert = vcol.data[loop].color
                file_handler.write(farbwert)
                file_handler.write('
')

Thanks for the fast response. It seems like vcol is always “None” in my case, so the for-loop will not be exectued. If I remove the check “if vcol is None:”, I get the same error message again.

I am working with imports of .obj-files so the color information is provided via .mtl-files. Seems like that might be relevant as I see the colors only after turning them on via “Alt + Z”.

Is there anywhere some tutorial or so to get a faster start into the data structure Blender uses? So far I try to look up details in the documentation and through search engines… but does not always work out too well. This forum is a great help so far… but I would like to not bother people here with every small challenge ;-).

Update: So far I was working in the Object Mode. If I switch to the “Vertex Paint”-Mode it seems like vcol does exist. Need to check if it includes the right color information now… but makes me wonder how to handle the different modes if I want to implement this logic in an export script :/.