11
$\begingroup$

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

enter image description here

How can this be achieved in Python?

$\endgroup$
4
  • $\begingroup$ Why did you ask then answer your own question in the same minute? $\endgroup$
    – David
    Apr 14, 2014 at 1:36
  • 1
    $\begingroup$ @David, I think this page or this page should answer your question. $\endgroup$
    – Garrett
    Apr 14, 2014 at 2:49
  • 3
    $\begingroup$ 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. $\endgroup$
    – Garrett
    Apr 14, 2014 at 2:53
  • 2
    $\begingroup$ 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." $\endgroup$
    – David
    Apr 14, 2014 at 14:50

1 Answer 1

13
$\begingroup$

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)
$\endgroup$
1
  • $\begingroup$ note, the method used to do polygon vertex looping was a bit strange, Ive updated the answer to use a more efficient method. $\endgroup$
    – ideasman42
    Apr 14, 2014 at 9:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.