Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

March 26, 2013

Painting Vertex Color Map using selected Faces

This script is obsolete, shift+K will fill all selected faces/ verts with the brush colour.

selected faces

During a recent conversation with Jimmy Gunawan of blendersushi I remembered that I wanted to write a script that lets me paint the vertex color map as a function of which faces are selected. Here's a version of this script that runs in vertex paint mode with face masking mode on.
import bpy
# instructions:
# "Use face masking mode, while in Vertex Paint mode"
def make_paint_list(my_object, faces):
# paint_list will contain all vertex color map indices to
# be used for overpainting.
paint_list = []
i = 0
for poly in my_object.polygons:
face_is_selected = poly.index in faces
for idx in poly.loop_indices:
if face_is_selected:
paint_list.append(i)
i += 1
return paint_list
def do_painting(color_map, paint_list):
print("vertex color indices: ", paint_list)
for i in paint_list:
color_map.data[i].color = color_choice
bpy.ops.object.mode_set(mode='VERTEX_PAINT')
def find_and_paint(color_choice):
my_object = bpy.context.active_object.data
# use active color map, create one if none available
if not my_object.vertex_colors:
my_object.vertex_colors.new()
color_map = my_object.vertex_colors.active
# find selected faces
bpy.ops.object.mode_set(mode='OBJECT')
faces = [f.index for f in my_object.polygons if f.select]
print("selected faces: ", faces)
paint_list = make_paint_list(my_object, faces)
do_painting(color_map, paint_list)
def vertex_paint_mode():
return (bpy.context.active_object.mode == 'VERTEX_PAINT')
def face_masking():
return bpy.context.active_object.data.use_paint_mask
if vertex_paint_mode() and face_masking():
color_choice = (0.2, 0.3, 0.7)
find_and_paint(color_choice)
else:
print("Use face masking mode, while in Vertex Paint mode")
i'll leave refactoring this snippet to you.

I've limited some freedom by forcing the user to be in 'VERTEX_PAINT' and Face Masking Active, but it's safer this way (even after some bugfixing by campbell) -- as it is still easy to crash blender in obscure ways otherwise.