0
$\begingroup$

I want to show enums up to 5 items.

Is it possible?

enter image description here

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)
        
        # I want like this:
        if context.scene.enum_type == 'FIRST':
            col.prop(context.scene, 'enum', text='Enum 1-5', expand=True)
        elif context.scene.enum_type == 'LAST':
            col.prop(context.scene, 'enum', text='Enum 6-10', expand=True)


enum_type = (
    ('FIRST', 'First', ''),
    ('LAST', 'Last', ''),
)


enum_items = (
    ('1', '1', ''),
    ('2', '2', ''),
    ('3', '3', ''),
    ('4', '4', ''),
    ('5', '5', ''),
    (None),
    ('6', '6', ''),
    ('7', '7', ''),
    ('8', '8', ''),
    ('9', '9', ''),
    ('10', '10', ''),
)
        


def register():
    bpy.utils.register_class(HelloWorldPanel)
    bpy.types.Scene.enum = bpy.props.EnumProperty(name='Enums', items=enum_items)
    bpy.types.Scene.enum_type = bpy.props.EnumProperty(name='Types', items=enum_type)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    
    del bpy.types.Scene.enum
    del bpy.types.Scene.enum_type 


if __name__ == "__main__":
    register()
$\endgroup$
3
  • $\begingroup$ Is items=enum_items[:5] enough for you? Or do you want to show all enum items until None is encountered, wherever it may be placed in your tuple? $\endgroup$
    – taiyo
    Jul 8, 2023 at 14:00
  • $\begingroup$ All enum items until None, I updated my code. $\endgroup$
    – Karan
    Jul 8, 2023 at 14:06
  • $\begingroup$ What does the index property do in layout.prop()? $\endgroup$
    – Karan
    Jul 8, 2023 at 14:07

3 Answers 3

2
$\begingroup$

Maybe like this? But need some tweak/hack with UI

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)

        # I want like this:
        row = layout.row(align=True)
        row.label(text='')
        col = row.column(align=True)
        col.scale_x = 1.15
        col.use_property_split = False
        if context.scene.enum_type == 'FIRST':
            col.prop_enum(context.scene, 'enums', value='1')
            col.prop_enum(context.scene, 'enums', value='2')
            col.prop_enum(context.scene, 'enums', value='3')
            col.prop_enum(context.scene, 'enums', value='4')
            col.prop_enum(context.scene, 'enums', value='5')            
        elif context.scene.enum_type == 'LAST':
            col.prop_enum(context.scene, 'enums', value='6')
            col.prop_enum(context.scene, 'enums', value='7')
            col.prop_enum(context.scene, 'enums', value='8')
            col.prop_enum(context.scene, 'enums', value='9')
            col.prop_enum(context.scene, 'enums', value='10')

enum_type = (
    ('FIRST', 'First', ''),
    ('LAST', 'Last', ''),
)

enum_items = (
    ('1', '1', ''),
    ('2', '2', ''),
    ('3', '3', ''),
    ('4', '4', ''),
    ('5', '5', ''),
    ('6', '6', ''),
    ('7', '7', ''),
    ('8', '8', ''),
    ('9', '9', ''),
    ('10', '10', ''),
)

def register():
    bpy.utils.register_class(HelloWorldPanel)
    
    bpy.types.Scene.enums  = bpy.props.EnumProperty(name='Enums', items=enum_items)
    bpy.types.Scene.enum_type = bpy.props.EnumProperty(name='Types', items=enum_type)

def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    
    del bpy.types.Scene.enums
    del bpy.types.Scene.enum_type


if __name__ == "__main__":
    register()

enter image description here

$\endgroup$
4
  • $\begingroup$ It is useful for enum with fewer items, but not practical with lots of items. I have 45 items. $\endgroup$
    – Karan
    Jul 9, 2023 at 18:23
  • $\begingroup$ As far as I'm concerned this answer answers your original question. You can't keep adding stuff to your question after someone takes their time to answer it. You should really consider stopping this behaviour mate. Cheers $\endgroup$
    – Gorgious
    Jul 10, 2023 at 5:38
  • $\begingroup$ @Gorgious, I already knew that solution. that is why I said it is not practical for lots of enum items. I don't want to hardcode all the enum items because the enum will grow in size later. $\endgroup$
    – Karan
    Jul 10, 2023 at 13:35
  • $\begingroup$ @Karan It doesn't matter what you already know or don't know if you don't write it in your question. Nobody can assume anything about what's not in your base question. Again, it would avoid wasting both your time and the time of several benevolent answerers in a lengthy back-and-forth. $\endgroup$
    – Gorgious
    Jul 11, 2023 at 6:54
2
$\begingroup$

After a bit too much back-and-forth, I think this is what you're looking for. You'll need dynamic enum items :

enter image description here

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', ''),
            ('7', '7', ''),
            ('8', '8', ''),
            ('9', '9', ''),
            ('10', '10', ''),
        ]
    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()

If you're wondering about the weird get_enum_items.items, you can refer to the warning in the docs or Is there a workaround for the known bug in dynamic EnumProperty?

$\endgroup$
1
$\begingroup$

I edited your script to show only the wanted parts of your enum_items, depending on where the separator (None) is placed in your tuple.

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)

        # I want like this:
        if context.scene.enum_type == 'FIRST':
            col.prop(context.scene, 'enum_first', text='Enum 1-5', expand=True) # take first enum items
        elif context.scene.enum_type == 'LAST':
            col.prop(context.scene, 'enum_last', text='Enum 6-10', expand=True) # take last enum items

enum_type = (
    ('FIRST', 'First', ''),
    ('LAST', 'Last', ''),
)

enum_items = (
    ('1', '1', ''),
    ('2', '2', ''),
    ('3', '3', ''),
    ('4', '4', ''),
    ('5', '5', ''),
    (None),
    ('6', '6', ''),
    ('7', '7', ''),
    ('8', '8', ''),
    ('9', '9', ''),
    ('10', '10', ''),
)

def register():
    bpy.utils.register_class(HelloWorldPanel)
    
    # create two enum items parts with None separator
    ni = enum_items.index(None)
    bpy.types.Scene.enum_first = bpy.props.EnumProperty(name='Enums', items=enum_items[ : ni])
    bpy.types.Scene.enum_last  = bpy.props.EnumProperty(name='Enums', items=enum_items[ni+1 : ])

    bpy.types.Scene.enum_type = bpy.props.EnumProperty(name='Types', items=enum_type)

def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    
    del bpy.types.Scene.enum
    del bpy.types.Scene.enum_type 


if __name__ == "__main__":
    register()

enter image description here

I think the index property of layout will not help you here, as the docs say:

index (int in [-2, inf], (optional)) – The index of this button, when set a single member of an array can be accessed, when set to -1 all array members are used

but you want some of your array.

$\endgroup$
6
  • $\begingroup$ I want to get enums from one EnumProperty $\endgroup$
    – Karan
    Jul 8, 2023 at 15:04
  • $\begingroup$ I dont think thats possible. Basically you have one enum property enum_items and just prepare it for presentation with helper enum properties. Any technical reason why you need to enforce only one enum property? $\endgroup$
    – taiyo
    Jul 8, 2023 at 15:25
  • $\begingroup$ because my code depends on one EnumProperty, I can't make two EnumProperty it will break my script. I want to just show the items depending on another EnumProperty e.g. enum_type $\endgroup$
    – Karan
    Jul 8, 2023 at 15:42
  • $\begingroup$ Well, then show more surrounding code. I cant understand why the script breaks. $\endgroup$
    – taiyo
    Jul 8, 2023 at 21:00
  • $\begingroup$ my code depends on scene.enum so I can't make two enums e.g. scene.enum_first and scene.enum_last $\endgroup$
    – Karan
    Jul 9, 2023 at 9:50

You must log in to answer this question.

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