Skip to content

Instantly share code, notes, and snippets.

@zeffii
Last active January 13, 2016 17:14
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 zeffii/8eede8ff41d6dac47448 to your computer and use it in GitHub Desktop.
Save zeffii/8eede8ff41d6dac47448 to your computer and use it in GitHub Desktop.
bl_info = {
"name": "Invert Vertex Colors",
"location": "3D viewport > Header > Paint menu in Vertex Paint mode",
"version": (0,1,0),
"blender": (2,7,6),
"description": "Inverts vertex colors of active paint layer",
"author": "Jerryno",
"category": "Paint",
}
import bpy
class InvertVertexColors(bpy.types.Operator):
bl_idname = "paint.invert_colors"
bl_label = "Invert Vertex Colors"
bl_description = "Invert vertex colors of active paint layer"
@classmethod
def poll(self, context):
try:
return context.active_object is not None
except (AttributeError, KeyError, TypeError):
return False
def execute(self, context):
active = context.active_object
polygons = active.data.polygons
vcol_data = active.data.vertex_colors.active.data
for ipoly in range(len(polygons)):
for idx, ivertex in enumerate(polygons[ipoly].loop_indices):
ivert = polygons[ipoly].vertices[idx]
col = vcol_data[ivertex].color
vcol_data[ivertex].color = tuple(1-x for x in col)
return{'FINISHED'}
def menu_entry(self, context):
self.layout.operator("paint.invert_colors", icon='IMAGE_ALPHA')
def register():
bpy.utils.register_module(__name__)
bpy.types.VIEW3D_MT_paint_vertex.append(menu_entry)
def unregister():
bpy.utils.unregister_module(__name__)
bpy.types.VIEW3D_MT_paint_vertex.remove(menu_entry)
if __name__ == "__main__":
register()
@zeffii
Copy link
Author

zeffii commented Jan 13, 2016

i only changed the execute function to have aliases for polygons and vcol_data, this speeds up the code by reducing dot lookups and increases readability by making the line more code-concise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment