1
$\begingroup$

I'm using the Gorgeous example as it is what I want to use and it's a great example ;)

see How to show limited enum items?

The problem when I run the script, is if I select in first list the item at index 4 and I switch to the last list, which have only one item here, the fact that the index is at 4 it won't select by default the first item of the last list, it will try to select the 5th item and because there's only one item, this item is not selected

So is it possible to do so ? at least select the first item when switching to the last list ?

Thanks

import bpy class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Test" def draw(self, context): layout = self.layout layout.use_property_split = True layout.use_property_decorate = False col = layout.column() col.prop(context.scene, 'enum_type', expand=True) text = 'Enum 1-5' if context.scene.enum_type == "FIRST" else 'Enum 6-10' col.prop(context.scene, 'enum', text=text, expand=True) enum_type = ( ('FIRST', 'First', ''), ('LAST', 'Last', ''), ) def get_enum_items(self, context): get_enum_items.items.clear() if context.scene.enum_type == "FIRST": get_enum_items.items = [ ('1', '1', ''), ('2', '2', ''), ('3', '3', ''), ('4', '4', ''), ('5', '5', ''), ] elif context.scene.enum_type == "LAST": get_enum_items.items = [ ('6', '6', ''), ] return get_enum_items.items get_enum_items.items = [] def register(): bpy.utils.register_class(HelloWorldPanel) bpy.types.Scene.enum = bpy.props.EnumProperty(name='Enums', items=get_enum_items) bpy.types.Scene.enum_type = bpy.props.EnumProperty(name='Types', items=enum_type) if __name__ == "__main__": register() 
$\endgroup$

1 Answer 1

2
$\begingroup$

Use the EnumProperty's update callback.

import bpy class HelloWorldPanel(bpy.types.Panel): """Creates a Panel in the Object properties window""" bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Test" def draw(self, context): layout = self.layout layout.use_property_split = True layout.use_property_decorate = False col = layout.column() col.prop(context.scene, 'enum_type', expand=True) text = 'Enum 1-5' if context.scene.enum_type == "FIRST" else 'Enum 6-10' col.prop(context.scene, 'enum', text=text, expand=True) enum_type = ( ('FIRST', 'First', ''), ('LAST', 'Last', ''), ) items_1 = [ ('1', '1', ''), ('2', '2', ''), ('3', '3', ''), ('4', '4', ''), ('5', '5', ''), ] items_2 = [ ('6', '6', ''), ('7', '7', ''), ] def get_enum_items(self, context): if self.enum_type == "FIRST": return items_1 elif self.enum_type == "LAST": return items_2 else: raise Exception(f"Unexpected enum_type: {self.enum_type}") def update_enum_type(self, context): index = self['enum'] items = get_enum_items(self, context) if len(items) - 1 < index: self['enum'] = 0 def register(): bpy.utils.register_class(HelloWorldPanel) bpy.types.Scene.enum = bpy.props.EnumProperty(name='Enums', items=get_enum_items) bpy.types.Scene.enum_type = bpy.props.EnumProperty(name='Types', items=enum_type, update=update_enum_type) if __name__ == "__main__": register() 
$\endgroup$
2
  • $\begingroup$ is there a way to access or set an item with index outside of update_enum_type ? using bpy.context.scene.enum = '6' to set the item is ok but it depends on the value. Thanks $\endgroup$ Commented Aug 29, 2024 at 9:50
  • $\begingroup$ self is bpy.context.scene in this case as you assign enum to bpy.types.Scene. So bpy.context.scene['enum'] = 0. $\endgroup$ Commented Aug 29, 2024 at 11:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.