# set a specified vertex color to black via python

how to set the vertex color in the center of the mesh to black and the rest of the vertices to red via python? a lot of the answers just tell me that i can color all the vertices in one face like that:

for poly in mesh.polygons:
for loop in poly.loop_indices:
color_layer.data[loop].color = (0,0,0)


but how can I get the specified vertex in the center and set it's color? Thank you very much

supplement(my script is as follows):

import bpy
import bmesh

#create a hexagon
bpy.context.scene.cursor_location = 0.0, 0.0, 0.0
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete()
ob=bpy.context.active_object
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action='SELECT')
bpy.ops.mesh.inset(thickness=0,depth=0)
bpy.ops.mesh.edge_collapse()

#I can select the center vertex in edit mode
bm = bmesh.from_edit_mesh(ob.data)
verts_center=[v for v in bm.verts if len(v.link_edges)==6]

vertexColors=ob.data.vertex_colors.new().data

#when it comes to object mode or vertex paint mode, how do I change the color of the vertex I selected?


as I know, in this example, the vertex I select will have six vertex color data, how do I get those six data and set the color?

• Please update your question to inclulde a complete script rather than just a fragment (along with any other steps (such as create an entry in Vertex Colors) to set up your situation) - or provide a Blend file (upload to blend-exchange.giantcowfilms.com and paste the link into your question). This will more easily allow someone to replicate your situation with the minimum of work. Could you use the number of 'loops' for the vertex to determine whether the particular vertex is in the 'middle' - eg, in your example, 2 loops would indicate the edge while 6 loops the centre. – Rich Sedman Nov 4 '18 at 7:38
• Sorry, I have added my script – bai mi Nov 4 '18 at 8:31

Look at the loop vert.

Using a method similar to Applying per-vertex colors to new Bmesh

import bpy
import bmesh
from random import uniform
context = bpy.context
mesh = context.object.data
bm = bmesh.new()
bm.from_mesh(mesh)

color_layer = bm.loops.layers.color.new("color")
red = (1, 0, 0, 1)
black = (0, 0, 0, 1)
for face in bm.faces:
for loop in face.loops:
print("Vert:", loop.vert.index)
loop[color_layer] = black if len(loop.vert.link_edges) == 6 else red

bm.to_mesh(mesh)


Result of running on a pre-made mesh.