# Apply vertex paint to a vertex

Through the user interface, you paint a vertex by hitting V in 3D View, and clicking on a vertex.

How can this be achieved in Python?

• Why did you ask then answer your own question in the same minute? Apr 14, 2014 at 1:36
• If you have more than 15 reputation and already know the answer, click the checkbox that says "Answer your own question" at the bottom of the Ask Question page. Type in your answer, then submit both question and answer together. Apr 14, 2014 at 2:53
• Thanks for your response. I learned something new. i had view the stack exchange as a place to get and give answers; but now I see it is also a platform for sharing what you already know. This quote states it best "To be crystal clear, it is not merely OK to ask and answer your own question, it is explicitly encouraged." Apr 14, 2014 at 14:50

This function should do the trick:

import bpy

def color_vertex(obj, vert, color):
"""Paints a single vertex where vert is the index of the vertex
and color is a tuple with the RGB values."""

mesh = obj.data
scn = bpy.context.scene

#check if our mesh already has Vertex Colors, and if not add some... (first we need to make sure it's the active object)
scn.objects.active = obj
obj.select = True
if mesh.vertex_colors:
vcol_layer = mesh.vertex_colors.active
else:
vcol_layer = mesh.vertex_colors.new()

for poly in mesh.polygons:
for loop_index in poly.loop_indices:
loop_vert_index = mesh.loops[loop_index].vertex_index
if vert == loop_vert_index:
vcol_layer.data[loop_index].color = color

#example usage
color = (1.0, 0.0, 1.0)  # pink
color_vertex(bpy.context.scene.objects['Cube'], 1, color)

• note, the method used to do polygon vertex looping was a bit strange, Ive updated the answer to use a more efficient method. Apr 14, 2014 at 9:48