Hi, I’m working on a python script to realise the Vertex_color_paint.
If we use interface, it is very simple that we just click this ![]()
but the problem is how to use Python to do it.
I checked the script manual and found a command bpy.ops.paint.vertex_color_set(), but I do not know how to use it, what I should input between the parentheses.
Thank you in advance.
If you are talking about the vertex colors and not weight painting or texture painting, then check out:
Mesh.vetrex_colors[“Col”].data
This seems to be a list of MeshVertexColor objects that seem to index-match to Mesh.faces. The MeshVertexColor object seem to be able to reference 4 colors which probably index-match to the vertex indices in the Mesh.faces object.
It seems that “Col” is the default name for the first color scheme that is painted on the mesh. It looks like you can add other schemes and edit the names under the Mesh properties window under “Vertex Colors”.
I’m typing the code directly into the web so it might have syntax errors, you should probably treat this a pseudo code:
// Get the active object
obj = bpy.contex.active_object
// Make sure it is tied to a mesh
if type(obj.data) != bpy.data.Mesh:
return
// Access the mesh
mesh = obj.data
// -----
// Paint the first vertex on the first face red
// Access the colors for “Col”
colors = mesh.vertex_colors[“Col”]
// Paint the target
colors.data[0].color1.r = 1.0
colors.data[0].color1.g = 0.0
colors.data[0].color1.b = 0.0
If I’m correct (if) then that would paint the first vertex on face 0 as red.
If you want to colour vertices consistently for every face i.e. all vertices of a face are the same colour you can use the code below which works for me where rgb is a per face list of RGB triplets
for face in mesh.faces:
try:
mesh.vertex_colors[0].data
except:
mesh.vertex_colors.new()
vertexColour = mesh.vertex_colors[0].data
v = vertexColour[face.index]
v.color1 = rgb[j]
v.color2 = rgb[j]
v.color3 = rgb[j]
v.color4 = rgb[j]
j = j + 1