I’m working on a Python add-on, but it’s been a while since I’ve scripted in Python and I’m still wrapping my head around Blender’s API. Does anyone have any good tips for separating the functionality of various buttons?
For example, if I had a button that moved an object by 5 units, and another that scaled an object by a factor of 5, should those be executed as two different button classes, or should they be the same class containing two unique methods?
You can create one or two operator classes for that, depends on how generic you want to make them.
If you want to use a single operator, there needs to be a property to specify the desired action (scale or move). A value property for how far to translate or how much to scale can be used for both.
Okay. So, in this example, it could look like this:
class MyTransformButton(bpy.types.Operator):
transform_operation = bpy.props.StringProperty()
def move(self):
# move code here
def scale(self):
# scale code here
def execute(self, context):
if transform_operation == "move":
self.move()
elif transform_operation == "scale":
self.scale()
else:
# do nothing
return{'FINISHED'}
or this:
class MoveButton(bpy.types.Operator):
def execute(self, context):
# move code here
return{'FINISHED'}
class ScaleButton(bpy.types.Operator):
def execute(self, context):
# scale code here
return{'FINISHED'}
What’s the difference between enums and strings? I’ve been in countless programming classes but I don’t think I’ve ever learned the difference. Google seems to just send me to a bunch of Java support pages, too.
Enums are groups or sets of of things that can be in different locations in memory.
As to the specifics of what an enum is that depends on how you make it and use it and what environment you are in.
check out https://docs.python.org/3/library/enum.html
Strings are a sequence of characters that take up one continuous piece of memory.
I’m not talking about python enums, but the Blender property type EnumProperty. It is represented as dropdown in Blender UI, only one element can be “active” and the amount of available items is limited (unlike strings, which can be limited in length, but other than that, the user is free to enter whatever she wants).