How to convert this vertex paint into a useable material (scripted)?

I have a single, the all-in-one mesh object created and then vertex-painted with a script. I can see the colors in vertex paint mode, but I haven't been able to successfully turn it into a renderable (or even viewable) material.

I've been working from this very helpful answer. Here is a bit of the script:

vcols = ob.data.vertex_colors

if len(vcols) == 0:
vcols.new()

vcols[-1].data.foreach_set("color", flattened_colors)


I also created the attribute node, but I'm not sure exactly how to use it. Are the vertices "painted" as soon as I run the above lines? What is the purpose of the attribute node? Is it only for conversion to a real material?

I tried to make a material using the script below, so far it doesn't seem to work (it shows as all black or all white when viewing material).

I'm basically using these pieces of the puzzle without understanding how they interact. What I need is a short description of the bits of script and the nodes, and how to get them to work together... "this does this, that does that, do them in this order."

Here is what I've done so far - make sure to scroll all the way through:

Then I execute the following lines, found similar here:

mat = bpy.data.materials.new('vertex_material')
mat.use_vertex_color_paint = True
mat.use_vertex_color_light = True  # material affected by lights
obj = bpy.context.active_object
obj.active_material = mat


The nodes disappear, the material appears but I end up with a white material.

• The attribute node provides only the color input. It needs to be plugged into a shader. Probably a diffuse or an emission. I don't know about the script so I can't provide a full answer. – Drudge Dec 22 '16 at 10:09
• You can append the material or assign it to the first slot via index operator obj.data.materials[0] = mat, see: blender.stackexchange.com/questions/23433/… – p2or Dec 22 '16 at 13:36
• @poor It seems that in 2.77 obj.active_material = mat takes care of that. It shows up there. len(obj.data.materials) changes from 0 to 1 and obj.jdata.materials[0].name is now 'vertex_material'. – uhoh Dec 22 '16 at 13:39
• Ok interesting. However it assigns it to the active slot (if relevant). You probably forgot to enable using nodes: mat.use_nodes = True, How to setup the material you can find here: blender.stackexchange.com/questions/23436/… – p2or Dec 22 '16 at 13:56
• @poor I added that but no change. With it or without it, when I execute the five lines the nodes disappear when the material is generated, so it is "aware" of the nodes at least. Still the material is just white in the material view or rendering. It's late here so I will look at your link in the morning and if no luck I'll generate a mcve and clean up the question further. Thanks! – uhoh Dec 22 '16 at 15:40