Guest User

Untitled

a guest
Jun 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. #
  2. # Save into your addons directory as mesh_set_vertex_color.py
  3. # and activate the add-on in user preferences
  4. #
  5.  
  6. bl_info = {
  7. "name" : "Set Vertex Color",
  8. "author" : "Stanislav Blinov, edited by Henrik Berglund",
  9. "version" : (1, 1, 0),
  10. "blender" : (2, 79, 0),
  11. "description" : "Set vertex color for selected vertices",
  12. "category" : "Mesh",}
  13.  
  14. import bmesh
  15. import bpy
  16. from mathutils import Vector, Color
  17.  
  18.  
  19. class MESH_xOT_SetVertexColor(bpy.types.Operator):
  20. bl_idname = "mesh.addon_set_vertex_color"
  21. bl_label = "Set Vertex Color..."
  22. bl_options = {'REGISTER', 'UNDO'}
  23.  
  24. #R = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 0.0, 0.0])
  25. #G = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 1.0, 0.0])
  26. #B = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 0.0, 1.0])
  27. #yellow = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 1.0, 0.0])
  28. #magenta = bpy.props.FloatVectorProperty(subtype='COLOR', default=[1.0, 0.0, 1.0])
  29. #teal = bpy.props.FloatVectorProperty(subtype='COLOR', default=[0.0, 1.0, 1.0])
  30.  
  31. #easingItems = [
  32. #("EASE_IN", "Ease In", "", "", 0),
  33. #("EASE_OUT", "Ease Out", "", "", 1),
  34. #("EASE_IN_OUT", "Ease In-Out", "", "", 2)]
  35.  
  36. # prop defined in the addon/node class
  37. #easingType = bpy.props.EnumProperty(
  38. # name="Easing",
  39. # default="EASE_IN",
  40. # items=easingItems)
  41.  
  42. nicecolor = bpy.props.FloatVectorProperty(
  43. name="Color",
  44. description="Color",
  45. subtype='COLOR',
  46. min=0.0,
  47. max=1.0,
  48. )
  49. updated = False
  50. lastcolor = bpy.props.FloatVectorProperty(
  51. name="Color",
  52. description="Color",
  53. subtype='COLOR',
  54. min=0.0,
  55. max=1.0,
  56. )
  57. lastpalette = bpy.props.FloatVectorProperty(
  58. name="Color",
  59. description="Color",
  60. subtype='COLOR',
  61. min=0.0,
  62. max=1.0,
  63. )
  64. open_popup = bpy.props.BoolProperty(name="open popup")
  65.  
  66.  
  67. @classmethod
  68. def poll(cls, context):
  69. return context.mode == 'EDIT_MESH'
  70.  
  71. def execute(self, context):
  72. print("execute")
  73. finalcolor = (self.nicecolor.r, self.nicecolor.g, self.nicecolor.b, 1)
  74.  
  75. bm = bmesh.from_edit_mesh(context.object.data)
  76.  
  77. faces = [f for f in bm.faces if f.select]
  78.  
  79. if faces:
  80. colors = bm.loops.layers.color.active
  81. if not colors:
  82. colors = bm.loops.layers.color.new("Col")
  83.  
  84. for f in faces:
  85. for loop in f.loops:
  86. loop[colors] = finalcolor
  87. #loop[colors] = (1,0,0,0)
  88. bmesh.update_edit_mesh(context.object.data)
  89.  
  90. return {'FINISHED'}
  91.  
  92. def check(self, context):
  93. print("check")
  94. active_color = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
  95.  
  96. print("lastcolor: " + str(self.lastcolor))
  97. print("nicecolor: " + str(self.nicecolor))
  98. print("activecolor: " + str(active_color))
  99. if (self.lastcolor.r != active_color.r) or (self.lastcolor.g != active_color.g) or (self.lastcolor.b != active_color.b):
  100. if (self.lastcolor.r == self.nicecolor.r) and (self.lastcolor.g == self.nicecolor.g) and (
  101. self.lastcolor.b == self.nicecolor.b):
  102. if self.lastpalette != active_color:
  103. self.lastpalette = active_color
  104. self.nicecolor = active_color
  105. print("palette changed")
  106. else:
  107. print("same")
  108. else:
  109. self.lastcolor = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
  110. self.nicecolor = self.lastcolor #WORK!
  111.  
  112.  
  113. #if self.nicecolor != bpy.context.tool_settings.vertex_paint.palette.colors.active.color:
  114. # self.nicecolor = bpy.context.tool_settings.vertex_paint.palette.colors.active.color #NOWORK
  115. return True
  116.  
  117. #bug: if click color and then don't click in window, color gets set to old color
  118.  
  119. def invoke(self, context, event):
  120. print("invoke")
  121. pal = bpy.data.palettes.get("CustomPalette")
  122. if pal is None:
  123. pal = bpy.data.palettes.new("CustomPalette")
  124. # add a color to that palette
  125. R = pal.colors.new()
  126. R.color = (1, 0, 0)
  127. G = pal.colors.new()
  128. G.color = (0, 1, 0)
  129. B = pal.colors.new()
  130. B.color = (0, 0, 1)
  131. yellow = pal.colors.new()
  132. yellow.color = (1, 1, 0)
  133. magenta = pal.colors.new()
  134. magenta.color = (1, 0, 1)
  135. teal = pal.colors.new()
  136. teal.color = (0, 1, 1)
  137. bpy.context.tool_settings.vertex_paint.palette = pal
  138.  
  139. loc = event.mouse_region_x, event.mouse_region_y #Get mouse coordinates
  140. bpy.ops.object.mode_set(mode="VERTEX_PAINT")
  141. bpy.ops.paint.sample_color(location=loc) #Sample colors under the cursor
  142. bpy.ops.object.mode_set(mode="EDIT")
  143. self.nicecolor = bpy.data.brushes['Draw'].color #Set default color to the sampled color (of the brush)
  144.  
  145.  
  146. self.lastcolor = self.nicecolor
  147. self.lastpalette = bpy.context.tool_settings.vertex_paint.palette.colors.active.color
  148. #return context.window_manager.invoke_props_dialog(self)
  149. #self.execute(context)
  150.  
  151. return context.window_manager.invoke_props_popup(self, event)
  152.  
  153. #return context.window_manager.popup_menu_pie()
  154.  
  155.  
  156.  
  157.  
  158.  
  159. def draw(self, context):
  160. layout = self.layout
  161.  
  162. ts = bpy.context.tool_settings
  163. if ts.vertex_paint.palette:
  164. layout.template_palette(ts.vertex_paint, "palette", color=True)
  165. #layout.prop(ts.image_paint,"palette")
  166. layout.prop(self, "nicecolor")
  167. #layout.prop(self, "easingType")
  168.  
  169.  
  170. #def draw(self, context):
  171.  
  172.  
  173. # nicecolor = bpy.context.tool_settings.image_paint.brush
  174.  
  175. # layout = self.layout
  176. # ts = context.tool_settings
  177.  
  178. # pie = layout.menu_pie()
  179. # row = pie.row()
  180. # # add a colour swatch that can popup a colour picker
  181. # row.prop(nicecolor,"color")
  182. # box = pie.box()
  183. # #show the colour picker directly
  184. # box.template_color_picker(nicecolor, 'color', value_slider=True)
  185.  
  186.  
  187. def register():
  188. bpy.utils.register_class(MESH_xOT_SetVertexColor)
  189.  
  190. def unregister():
  191. bpy.utils.unregister_class(MESH_xOT_SetVertexColor)
  192.  
  193. if __name__ == "__main__":
  194. register()
Add Comment
Please, Sign In to add comment