I wrote an Example for you, while keeping it as simple as i can
To Achieve the effect that you describe, first you will need a Property Group and Collection Property
First thing you need is Property Group to Store the Number, this is something to use to store multiple properties in blender, in our case right now, we only need to store a number
This class will need to be registered in the register function below
class Test_Number(bpy.types.PropertyGroup):
number: bpy.props.IntProperty()
Now you have the property type, however, you cannot use this yet, to access this, you can use either pointerproperty, or collectionproperty, in our case, we need collectionproperty, because we need multiple of it to create the enum
bpy.types.Scene.test_collection = bpy.props.CollectionProperty(type=Test_Number)
The Collection type will use the property group class we created earlier, so now, you have a list of that object type you created.
Lets create our Enum Property
To create a Dynamic Property, we creates a function that returns the items that we want.
the items that we need to return is in this format
[(identifier, name, description),(identifier, name, description)]
This code will grab the collectionproperty, and loop through it like a list, each of the item are the propertygroup that we defined, and as what I did previously, it have number property
im not too sure, but I remember enum can only accept string, so I convert them into string and just feed identifier, name and description with the number, of course, you can give it anything, but when you grab the data later for use, it will use the identifier
def test_items(self, context):
Enum_items = []
for test_number in context.scene.test_collection:
data = str(test_number.number)
item = (data, data, data)
Enum_items.append(item)
return Enum_items
######################
###REGISTER############
bpy.types.Scene.test_number = bpy.props.EnumProperty(items=test_items)
To Add new Object, just simply run the add function in the collection property and set the number to what you want, you can just put this in your operator
Test_Number = context.scene.test_collection.add()
Test_Number.number = random.randint(1, 4)
Below are the full code of the example, I hope it helps
import bpy
import random
class Example_Add_Operator(bpy.types.Operator):
bl_idname = "example.add"
bl_label = "Add"
def execute(self, context):
Test_Number = context.scene.test_collection.add()
Test_Number.number = random.randint(1, 4)
return {'FINISHED'}
class Example_Remove_Operator(bpy.types.Operator):
bl_idname = "example.remove"
bl_label = "Remove"
def execute(self, context):
if len(context.scene.test_collection) > 0:
context.scene.test_collection.remove(0)
return {'FINISHED'}
class EXAMPLE_Panel(bpy.types.Panel):
bl_label = "Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = "Example"
def draw(self, context):
layout = self.layout
row = layout.row(align=True)
row.prop(context.scene, "test_number", text="")
row.operator("example.add", text="", icon="ADD")
row.operator("example.remove", text="", icon="REMOVE")
def test_items(self, context):
Enum_items = []
for test_number in context.scene.test_collection:
data = str(test_number.number)
item = (data, data, data)
Enum_items.append(item)
return Enum_items
class Test_Number(bpy.types.PropertyGroup):
number: bpy.props.IntProperty()
def register():
bpy.utils.register_class(EXAMPLE_Panel)
bpy.utils.register_class(Example_Add_Operator)
bpy.utils.register_class(Example_Remove_Operator)
bpy.utils.register_class(Test_Number)
bpy.types.Scene.test_number = bpy.props.EnumProperty(items=test_items)
bpy.types.Scene.test_collection = bpy.props.CollectionProperty(type=Test_Number)
def unregister():
del bpy.types.Scene.test_number
del bpy.types.Scene.test_collection
bpy.utils.unregister_class(EXAMPLE_Panel)
bpy.utils.unregister_class(Example_Add_Operator)
bpy.utils.unregister_class(Example_Remove_Operator)
bpy.utils.unregister_class(Test_Number)
if __name__ == "__main__":
register()
Example.py (2.2 KB)