1
$\begingroup$

I'm developing an addon on Blender 2.91 and I can't wrap my head around something:

I have a dynamic (uses a callback for items) EnumProperty inside of a Operator:

class DMX_Fixture_AddEdit(Operator):
    manufacturer_list_items = []
    def manufacturerListItems(self, context):
        if (not len(DMX_Fixture_AddEdit.manufacturer_list_items)):
            DMX_Fixture_AddEdit.manufacturer_list_items = DMX_GDTF.getManufacturerList()
        return DMX_Fixture_AddEdit.manufacturer_list_items

    manufacturer: EnumProperty(
        name = "Manufacturer",
        description = "Fixture GDTF Manufacturer",
        items=manufacturerListItems
    )

Then, I try to acess it's enum_items throught the rna_type:

self.rna_type.properties['manufacturer'].enum_items

And the result is an empty collection.

However, if I define the items manually (not through a callback), the enum_items show up as expected.

The documentation is not clear on this point, however the enum_items_static description makes me think the enum_items should read the dynamic items.

Any thoughts?

EDIT

Minimal sample:

import bpy

class DMX_OT_Test(bpy.types.Operator):
    bl_label = "DMX > Test"
    bl_idname = "dmx.test"

    entries: bpy.props.EnumProperty(
        name = "Entries",
        description = "Entries to be loaded into enum_items",
        items = (('id0','',''),('id1','',''),('id2','',''))
    )
    
    dyn_entry_items = []
    def entryListItems(self, context):
        if (not len(DMX_OT_Test.dyn_entry_items)):
            DMX_OT_Test.dyn_entry_items = (('dyn_id0','',''),('dyn_id1','',''),('dyn_id2','',''))
        return DMX_OT_Test.dyn_entry_items
    
    dyn_entries: bpy.props.EnumProperty(
        name = "Dynamic Entries",
        description = "Dynamic entries to be loaded into enum_items",
        items = entryListItems
    )

    def execute(self, context):
        print('---\n')
        
        print(self.rna_type)
        print(self.rna_type.properties)
        for prop in self.rna_type.properties:
            print("\t",prop)
            
        print("\nself.entries:", self.entries)
        print(self.rna_type.properties['entries'].enum_items)
        for item in self.rna_type.properties['entries'].enum_items:
            print("\t",item)

        print("\nself.dyn_entries:", self.dyn_entries)
        print(self.rna_type.properties['dyn_entries'].enum_items)
        for item in self.rna_type.properties['dyn_entries'].enum_items:
            print("\t",item)
            print("\t",item.identifier)
        return {'FINISHED'}
    
def register():
    bpy.utils.register_class(DMX_OT_Test)


def unregister():
    bpy.utils.unregister_class(DMX_OT_Test)

if __name__ == "__main__":
    
    register()

# test call
bpy.ops.dmx.test()
unregister()
```
$\endgroup$

2 Answers 2

1
$\begingroup$

Calling enum_items on a dynamically generated EnumProperty currently isn`t supported according to this thread: enum_items not working with dynamic EnumProperty

$\endgroup$
1
$\begingroup$

Here's a fancy way relying on annotations to get the enum items from a dynamic enum property :

items_callback = self.__annotations__.get("dyn_entries").keywords.get("items")
print(items_callback(self, context))

This will print out a list containing the formatted items, not instances of bpy.types.EnumPropertyItem.

(('dyn_id0', '', ''), ('dyn_id1', '', ''), ('dyn_id2', '', ''))

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .