Skip to content

Instantly share code, notes, and snippets.

@cyaoeu
Last active June 16, 2018 14:52
Show Gist options
  • Save cyaoeu/1f4b19c312108423bb19e6e88cbe9c09 to your computer and use it in GitHub Desktop.
Save cyaoeu/1f4b19c312108423bb19e6e88cbe9c09 to your computer and use it in GitHub Desktop.
Blender addon: Set Vertex Color (License: GPL)
#
# Save into your addons directory as mesh_set_vertex_color.py
# and activate the add-on in user preferences
#
bl_info = {
"name" : "Set Vertex Color",
"author" : "Stanislav Blinov, edited by Henrik Berglund",
"version" : (1, 1, 0),
"blender" : (2, 79, 0),
"description" : "Set vertex color for selected vertices",
"category" : "Mesh",}
import bmesh
import bpy
from mathutils import Vector, Color
class MESH_xOT_SetVertexColor(bpy.types.Operator):
bl_idname = "mesh.addon_set_vertex_color"
bl_label = "Set Vertex Color..."
bl_options = {'REGISTER', 'UNDO'}
#R = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 0.0, 0.0])
#G = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 1.0, 0.0])
#B = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 0.0, 1.0])
#yellow = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 1.0, 0.0])
#magenta = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 0.0, 1.0])
#teal = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 1.0, 1.0])
#easingItems = [
#("EASE_IN", "Ease In", "", "", 0),
#("EASE_OUT", "Ease Out", "", "", 1),
#("EASE_IN_OUT", "Ease In-Out", "", "", 2)]
# prop defined in the addon/node class
#easingType = bpy.props.EnumProperty(
# name="Easing",
# default="EASE_IN",
# items=easingItems)
nicecolor = bpy.props.FloatVectorProperty(
name="Color",
description="Color",
subtype='COLOR',
min=0.0,
max=1.0,
)
updated = False
lastcolor = bpy.props.FloatVectorProperty(
name="Color",
description="Color",
subtype='COLOR',
min=0.0,
max=1.0,
)
lastpalette = bpy.props.FloatVectorProperty(
name="Color",
description="Color",
subtype='COLOR',
min=0.0,
max=1.0,
)
open_popup = bpy.props.BoolProperty(name="open popup")
@classmethod
def poll(cls, context):
return context.mode == 'EDIT_MESH'
def execute(self, context):
print("execute")
finalcolor = (self.nicecolor.r, self.nicecolor.g, self.nicecolor.b, 1)
bm = bmesh.from_edit_mesh(context.object.data)
faces = [f for f in bm.faces if f.select]
if faces:
colors = bm.loops.layers.color.active
if not colors:
colors = bm.loops.layers.color.new("Col")
for f in faces:
for loop in f.loops:
loop[colors] = finalcolor
#loop[colors] = (1,0,0,0)
bmesh.update_edit_mesh(context.object.data)
return {'FINISHED'}
def check(self, context):
print("check")
active_color = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
print("lastcolor: " + str(self.lastcolor))
print("nicecolor: " + str(self.nicecolor))
print("activecolor: " + str(active_color))
if (self.lastcolor.r != active_color.r) or (self.lastcolor.g != active_color.g) or (self.lastcolor.b != active_color.b):
if (self.lastcolor.r == self.nicecolor.r) and (self.lastcolor.g == self.nicecolor.g) and (
self.lastcolor.b == self.nicecolor.b):
if self.lastpalette != active_color:
self.lastpalette = active_color
self.nicecolor = active_color
print("palette changed")
else:
print("same")
else:
self.lastcolor = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
self.nicecolor = self.lastcolor #WORK!
#if self.nicecolor != bpy.context.tool_settings.vertex_paint.palette.colors.active.color:
# self.nicecolor = bpy.context.tool_settings.vertex_paint.palette.colors.active.color #NOWORK
return True
#bug: if click color and then don't click in window, color gets set to old color
def invoke(self, context, event):
print("invoke")
pal = bpy.data.palettes.get("CustomPalette")
if pal is None:
pal = bpy.data.palettes.new("CustomPalette")
# add a color to that palette
R = pal.colors.new()
R.color = (1, 0, 0)
G = pal.colors.new()
G.color = (0, 1, 0)
B = pal.colors.new()
B.color = (0, 0, 1)
yellow = pal.colors.new()
yellow.color = (1, 1, 0)
magenta = pal.colors.new()
magenta.color = (1, 0, 1)
teal = pal.colors.new()
teal.color = (0, 1, 1)
bpy.context.tool_settings.vertex_paint.palette = pal
loc = event.mouse_region_x, event.mouse_region_y #Get mouse coordinates
bpy.ops.object.mode_set(mode="VERTEX_PAINT")
bpy.ops.paint.sample_color(location=loc) #Sample colors under the cursor
bpy.ops.object.mode_set(mode="EDIT")
self.nicecolor = bpy.data.brushes['Draw'].color #Set default color to the sampled color (of the brush)
self.lastcolor = self.nicecolor
self.lastpalette = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
#return context.window_manager.invoke_props_dialog(self)
#self.execute(context)
return context.window_manager.invoke_props_popup(self, event)
#return context.window_manager.popup_menu_pie()
def draw(self, context):
layout = self.layout
ts = bpy.context.tool_settings
if ts.vertex_paint.palette:
layout.template_palette(ts.vertex_paint, "palette", color=True)
#layout.prop(ts.image_paint,"palette")
layout.prop(self, "nicecolor")
#layout.prop(self, "easingType")
#def draw(self, context):
# nicecolor = bpy.context.tool_settings.image_paint.brush
# layout = self.layout
# ts = context.tool_settings
# pie = layout.menu_pie()
# row = pie.row()
# # add a colour swatch that can popup a colour picker
# row.prop(nicecolor,"color")
# box = pie.box()
# #show the colour picker directly
# box.template_color_picker(nicecolor, 'color', value_slider=True)
def register():
bpy.utils.register_class(MESH_xOT_SetVertexColor)
def unregister():
bpy.utils.unregister_class(MESH_xOT_SetVertexColor)
if __name__ == "__main__":
register()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment