I just started to understand properties, and I find it pretty awkward. But ok, here I do my best to explain it. (So it could be I am rambling).
first you can do some imports llke:
import bpy
from bpy.props import EnumProperty, PointerProperty
from bpy.type import Panel, Operator, PropertyGroup
A property group is a group of properties, a class. You have only 1 property, but if you use more, then you use a PropertyGroup. And a PropertyGroup looks like this:
class TemplProperties(bpy.types.PropertyGroup):
my_enum : bpy.props.EnumProperty(name = "Dimensions", description = "Choose format",
items = (
('Profile Picture', "400x400", "Profile Picture"), # value, dropdown-value, tiptool
('Header', "1500x500", "Header"), # instead value try operator
('Post Image', "1200 x 675", "Post Image")
) )
You can make this enum dynamic, I guess, by using python. Or, you can use also a UIList but that could be quite mind-boggling to use. Better start like this first.
Then in the register/unregister functions, you add this property to an object, winman, scene (or other things I am not aware off). Here I use the scene to store the enum:
def register():
for cls in classes:
bpy.utils.register_class(cls)
bpy.types.Scene.scene_propname = PointerProperty(type=TemplProperties)
def unregister():
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
del bpy.types.Scene.scene_propname
Some explanation:
scene_propname: I could name it anything I want. But we will use this name elsewhere, in the operator, and in the panel class. Notice that it is called here a PointerProperty (don’t ask me why, but it seems that is how it works). TemplProperties is the name of the PropertyGroup class.
How to use the enum in an operator?
class TEMPL_OT_operator(Operator):
""" tooltip goes here """
bl_idname = "nmssm.operator"
bl_label = "Label of operator"
bl_options = {"REGISTER", "UNDO"}
@classmethod
def poll(cls, context):
return context.mode == "OBJECT"
def execute(self, context):
print(context.scene.scene_propname.my_enum)
return {'FINISHED'}
You see here scene_propname followed by .my_enum.
How to use the enum in the panel?
class TEMPL_PT_rendersettings(Panel):
""" Tooltip """
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
bl_label = "Templlabel"
bl_category = "Templ"
bl_options = {"DEFAULT_CLOSED"}
def draw(self, context):
layout = self.layout
scene = context.scene
prop_one = scene.scene_propname
layout.prop(prop_one, "my_enum")
box = layout.box()
row = box.row()
row.operator("nmssm.operator", icon="PREVIEW_RANGE")
row = box.row()
# Recap (panel)
# - Make a convenience variable:
# scene = context.scene
# prop_one = scene.scene_propname
# - layout.prop(prop_one, "my_enum")
To complete the script, don’t forget to add the classes list:
classes = [
TemplProperties,
TEMPL_OT_operator,
TEMPL_PT_rendersettings,
]
And the code to run it as script at the bottom:
if __name__ == '__main__':
register()