bpy.types.Object.my_swatch = EnumProperty(
items = theSwatches,
name = "Swatch")
setSwatches()
bpy.types.Object.my_red = FloatProperty(
name = "Red", default = 0.5,
min = 0, max = 1)
bpy.types.Object.my_green = FloatProperty(
name = "Green", default = 0.5,
min = 0, max = 1)
bpy.types.Object.my_blue = FloatProperty(
name = "Blue", default = 0.5,
min = 0, max = 1)
def findSwatch(key):
for n,swatch in enumerate(theSwatches):
(key1, name, colors) = swatch
if key == key1:
return n
raise NameError("Unrecognized key %s" % key)
# Панель образцов
class SwatchPanel(bpy.types.Panel):
bl_label = "Swatches"
#bl_idname = "myPanelID"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "material"
def draw(self , context):
layout = self.layout
ob = context.active_object
layout.prop_menu_enum(ob, "my_swatch")
layout.operator("swatches.set").swatch=True
layout.separator()
layout.prop(ob, "my_red")
layout.prop(ob, "my_green")
layout.prop(ob, "my_blue")
layout.operator("swatches.set").swatch=False
layout.operator("swatches.add")
layout.operator("swatches.delete")
# Установка кнопки
class OBJECT_OT_SetButton(bpy.types.Operator):
bl_idname = "swatches.set"
bl_label = "Set color"
swatch = bpy.props.BoolProperty()
@classmethod
def poll(self, context):
if context.object and context.object.type == 'MESH':
return len(context.object.data.materials)
def execute(self, context):
global theSwatches
ob = context.object
if self.swatch:
n = findSwatch(ob.my_swatch)
(key, name, colors) = theSwatches[n]
words = colors.split()
color = (float(words[0]), float(words[1]), float(words[2]))
else:
color = (ob.my_red, ob.my_green, ob.my_blue)
ob.data.materials[0].diffuse_color = color
return{'FINISHED'}
# Добавление кнопки
class OBJECT_OT_AddButton(bpy.types.Operator):
bl_idname = "swatches.add"
bl_label = "Add swatch"
def execute(self, context):
global theSwatches
ob = context.object
colors = "%.2f %.2f %.2f" % (ob.my_red, ob.my_green, ob.my_blue)
theSwatches.append((colors, colors, colors))
setSwatches()
return{'FINISHED'}
# Удаление кнопки
class OBJECT_OT_DeleteButton(bpy.types.Operator):
bl_idname = "swatches.delete"
bl_label = "Delete swatch"
def execute(self, context):
global theSwatches
n = findSwatch(context.object.my_swatch)
theSwatches.pop(n)
setSwatches()
return{'FINISHED'}
# Регистрация
bpy.utils.register_module(__name__)
Операторы, которые нам до сих пор попадались, были простыми кнопками. В этой программе мы делаем более сложный оператор, который создаёт искривленный цилиндр.