Difficulty Dynamically Updating EnumProperty Items Stored in Addon Preferences

Hello dear Blender devs,
Summary: We are developing a Blender add-on that aims to create a dynamic combobox, allowing users to select items that can change based on user interactions or external data. These items are stored in the add-on preferences. However, I’m encountering challenges in dynamically updating the items displayed in the combobox.
The code below seems coherent but Pylance is uncapable of locating the register EnumNftsPropertyGroup

# Define a PropertyGroup to structure the data stored in AddonPreferences
class MyAddonPreferences(bpy.types.AddonPreferences):
    bl_idname = __name__

    # Define a CollectionProperty to store nfts for the combobox
    nfts_collection: bpy.props.CollectionProperty(
        items=[("default", "default", "Choose your nft")],
        name="My Nfts",
        default="default",        
        description='All loaded Nfts by identifier',
        type=EnumNftsPropertyGroup
    )


# Define an Operator to update the nfts in the CollectionProperty
class UpdateNftsOperator(bpy.types.Operator):
    bl_idname = "my.update_nfts_operator"
    bl_label = "Update Nfts"

    def execute(self, context):
        # Dynamic logic to update items in the CollectionProperty
        addon_prefs = self.addon_prefs(context) # some operation occur here ...

        

# Define a PropertyGroup to represent items in the combobox
class EnumNftsPropertyGroup(bpy.types.PropertyGroup):
    identifier: bpy.props.StringProperty()
    name: bpy.props.StringProperty()
    url: bpy.props.StringProperty()

# Define a Panel to display the combobox
class MyPanel(bpy.types.Panel):
    bl_idname = "my.panel"
    bl_label = "My Panel"
    bl_category = "My Addon"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"

    def draw(self, context):
        layout = self.layout

        # Access the items in AddonPreferences and populate the combobox
        preferences = context.preferences.addons[__name__].preferences
        nfts_collection = preferences.nfts_collection
        
        # Create a list of tuples for the items in the combobox
        nfts = [("default", "Default", "Choose your nft")]  # Default item

        # Add items based on your nfts_collection
        for nfts in nfts_collection:
            nfts.append((nfts.identifier, nfts.description, nfts.url))  # Use the data from your collection

        # Display the combobox
        layout.label(text="Select an item:")
        layout.prop(context.scene, "my_selected_item", text="NFT Items", icon='QUESTION')  # Use your scene property

def register():
    bpy.utils.register_class(MyAddonPreferences)
    bpy.utils.register_class(UpdateNftsOperator)
    bpy.utils.register_class(EnumNftsPropertyGroup)
    bpy.utils.register_class(MyPanel)

def unregister():
    bpy.utils.unregister_class(MyAddonPreferences)
    bpy.utils.unregister_class(UpdateNftsOperator)
    bpy.utils.unregister_class(EnumNftsPropertyGroup)
    bpy.utils.unregister_class(MyPanel)

The challenge I’m facing is how to effectively update the nfts_collection dynamically within the UpdateNftsOperator so that the combobox in the MyPanel accurately reflects the updated list of items. Maybe the registration of the EnumNftsPropertyGroup is not correct… but I can’t find out the reason.

Environment:

  • Blender version: 3.6.3
  • Operating system: Linux ubuntu 22.04

Hopefully, this post receive positive feedback for us to progress on our adventure in Blender, I would greatly appreciate any assistance in resolving this issue. Thank you for your support!
Johnny Christmas aka jnxmas

For questions about how to use Blender or its Python API, please use one of the user community websites, as this forum is for developing Blender itself.

Stackexchange has a number of answers for this, like:

1 Like

thanks you for you reply, my mistake, I saw the categorie addon, but it must be for the official addon