Skip to content

Instantly share code, notes, and snippets.

@tamask
Created December 29, 2011 18:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamask/1535492 to your computer and use it in GitHub Desktop.
Save tamask/1535492 to your computer and use it in GitHub Desktop.
blender vertex paint function
import bpy
from mathutils import Color
MODES = {
'=': lambda a, b: b,
'+': lambda a, b: a + b,
'-': lambda a, b: a - b,
'*': lambda a, b: Color((a.r * b.r, a.g * b.g, a.b * b.b)),
'/': lambda a, b: Color((a.r / b.r, a.g / b.g, a.b / b.b)),
}
def paint(fn, mode='='):
obj = bpy.context.active_object
colors = obj.data.vertex_colors.active.data
if not callable(fn) and not hasattr(fn, '__len__'):
fn = (fn, fn, fn)
for face_index, face_colors in enumerate(colors):
if obj.data.faces[face_index].select:
for attr in ('color1', 'color2', 'color3', 'color4'):
i = Color(getattr(face_colors, attr))
o = Color(fn(i) if callable(fn) else fn)
setattr(face_colors, attr, MODES[mode](i, o))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment