enum_items not working with dynamic EnumProperty #86803

Open
opened 2021-03-21 22:34:03 +01:00 by Hugo Aboud · 13 comments

System Information
Operating system: Windows 10

Blender Version
Broken: 2.91.2, 5be9ef4177, master, 2021-01-19

Short description of error

When using a callback to populate a (dynamic) EnumProperty, the enum_items property available for that rna_type returns an empty collection.

Exact steps for others to reproduce the error

Minimal script 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()

Output:

<bpy_struct, Struct("DMX_OT_test")>
<bpy_collection[3], Struct.properties>
  <bpy_struct, PointerProperty("rna_type")>
  <bpy_struct, EnumProperty("entries")>
  <bpy_struct, EnumProperty("dyn_entries")>

self.entries: id0
<bpy_collection[3], EnumProperty.enum_items>
  <bpy_struct, EnumPropertyItem("id0")>
  <bpy_struct, EnumPropertyItem("id1")>
  <bpy_struct, EnumPropertyItem("id2")>

self.dyn_entries: dyn_id0
  <bpy_collection[0], EnumProperty.enum_items>
**System Information** Operating system: Windows 10 **Blender Version** Broken: 2.91.2, 5be9ef417703, master, 2021-01-19 **Short description of error** When using a callback to populate a (dynamic) `EnumProperty`, the `enum_items` property available for that `rna_type` returns an empty collection. **Exact steps for others to reproduce the error** Minimal script 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() ``` Output: ``` <bpy_struct, Struct("DMX_OT_test")> <bpy_collection[3], Struct.properties> <bpy_struct, PointerProperty("rna_type")> <bpy_struct, EnumProperty("entries")> <bpy_struct, EnumProperty("dyn_entries")> self.entries: id0 <bpy_collection[3], EnumProperty.enum_items> <bpy_struct, EnumPropertyItem("id0")> <bpy_struct, EnumPropertyItem("id1")> <bpy_struct, EnumPropertyItem("id2")> self.dyn_entries: dyn_id0 <bpy_collection[0], EnumProperty.enum_items> ```
Author

Added subscriber: @Hugo-Aboud

Added subscriber: @Hugo-Aboud

Added subscriber: @JaumeBellet

Added subscriber: @JaumeBellet

Issue here is that RNA_property_enum_items_ex function is called with NULL as context.

When calling the callback (to get the value for example), is scaled up from a BPY_context_get() call on bpy_rna.c

Issue here is that RNA_property_enum_items_ex function is called with NULL as context. When calling the callback (to get the value for example), is scaled up from a BPY_context_get() call on bpy_rna.c
Member

Added subscriber: @lichtwerk

Added subscriber: @lichtwerk
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

I can confirm the behavior.

Trying to understand the usecase a bit better though (since these are 'expanded' correctly for the UI and elsewhere):
Why do you need access to these like demonstrated above?
While not as convenient as from 'proper expanded' collection of EnumPropertyItem, you could get them directly through the callback, no?

See
#86803.blend

I can confirm the behavior. Trying to understand the usecase a bit better though (since these are 'expanded' correctly for the UI and elsewhere): Why do you need access to these like demonstrated above? While not as convenient as from 'proper expanded' collection of `EnumPropertyItem`, you could get them directly through the callback, no? See [#86803.blend](https://archive.blender.org/developer/F9904044/T86803.blend)
Author

The issue came up while I was trying to setup dynamic nested Menus using context_pointer_set, as described here.

Since the data argument must be of AnyType I need acess to the EnumPropertyItem references.

The solution for now has been populating the enum items when the annotations are evaluated. However, this can only be done once, not allowing for dynamic content changes.

manufacturers: EnumProperty(
    name = "Manufacturers",
    description = "Fixture GDTF Manufacturers",
    items = DMX_GDTF.getManufacturerList()
)
The issue came up while I was trying to setup dynamic nested Menus using `context_pointer_set`, as described [here](https://blender.stackexchange.com/a/45855/107464). Since the `data` argument must be of `AnyType` I need acess to the `EnumPropertyItem` references. The solution for now has been populating the enum items when the annotations are evaluated. However, this can only be done once, not allowing for dynamic content changes. ``` manufacturers: EnumProperty( name = "Manufacturers", description = "Fixture GDTF Manufacturers", items = DMX_GDTF.getManufacturerList() ) ```
Member

Changed status from 'Needs User Info' to: 'Needs Triage'

Changed status from 'Needs User Info' to: 'Needs Triage'
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Member

Interesting usecase, confirming then.

Interesting usecase, confirming then.

Added subscriber: @ideasman42

Added subscriber: @ideasman42

Currently enum_items only works for static items. - That is a fixed array of items.

For C/C++ code both can be set, for Python passing the item as a function makes it impossible

Marking as a TODO since this isn't an error in the code.

Currently `enum_items` only works for static items. - That is a fixed array of items. For C/C++ code both can be set, for Python passing the item as a function makes it impossible Marking as a TODO since this isn't an error in the code.
Philipp Oeser removed the
Interest
Python API
label 2023-02-10 09:04:39 +01:00
Contributor

Found some other use case for this trying to make a general method to check wheither current enum value is valid to polluting console with errors like below:

WARN (bpy.rna): C:\Users\blender\git\blender-v360\blender.git\source\blender\python\intern\bpy_rna.c:1343 pyrna_enum_to_py: current value '17' matches no enum in 'BIMModelProperties', '', 'relating_type_id'

Idea of the method was:

def has_valid_enum_value(props, enum_property_name):
    # assuming the default value is fine
    if enum_property_name not in props:
        return True
    cur_value = props[enum_property_name]
    return cur_value < len(props.bl_rna.properties[enum_property_name].enum_items)

I guess it still can be done manually by keeping track of each dynamic items generator in the code.

Found some other use case for this trying to make a general method to check wheither current enum value is valid to polluting console with errors like below: `WARN (bpy.rna): C:\Users\blender\git\blender-v360\blender.git\source\blender\python\intern\bpy_rna.c:1343 pyrna_enum_to_py: current value '17' matches no enum in 'BIMModelProperties', '', 'relating_type_id'` Idea of the method was: ```python def has_valid_enum_value(props, enum_property_name): # assuming the default value is fine if enum_property_name not in props: return True cur_value = props[enum_property_name] return cur_value < len(props.bl_rna.properties[enum_property_name].enum_items) ``` I guess it still can be done manually by keeping track of each dynamic items generator in the code.
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#86803
No description provided.