2
$\begingroup$

I have an enum property that has 6 items and what I want is to arrange the 3 items in one and the other 3 items in the second row. Is it possible to set them?

Here is the code:


class NEW_PT_CUSTOM_73B8D(bpy.types.Panel):
    bl_label = 'Custom'
    bl_idname = 'SNA_PT_CUSTOM_73B8D'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_context = ''
    bl_category = 'Sell'
    bl_order = 0
    bl_ui_units_x=0

    def draw(self, context):
        layout = self.layout
#        col = layout.column()
        layout.prop(bpy.context.scene, 'my_enum', expand=True)


def register():
    bpy.types.Scene.my_enum = bpy.props.EnumProperty(name='enum', description='', items=[('Rocket', 'Rocket', '', 0, 0), ('Jeep', 'Jeep', '', 0, 1), ('Truck', 'Truck', '', 0, 2), ('Car', 'Car', '', 0, 3), ('Bike', 'Bike', '', 0, 4), ('Plane', 'Plane', '', 0, 5)])
    try: bpy.utils.register_class(NEW_PT_CUSTOM_73B8D)
    except: pass


def unregister():
    del bpy.types.Scene.my_enum
    try: bpy.utils.unregister_class(NEW_PT_CUSTOM_73B8D)
    except: pass

if __name__ == "__main__":
    register()

enter image description here enter image description here

$\endgroup$
2
  • 2
    $\begingroup$ On an enum you will not be able to have a mixed organization of the items. If you really want that layout create 2 local enum properties in your custom panel and change your global property thanks to an override of the set and get functions of those local items. $\endgroup$
    – Werwack
    Commented Dec 6, 2022 at 10:47
  • $\begingroup$ You are going too far. I'm new if you could please write a code for me that would be great. $\endgroup$
    – Muzammil
    Commented Dec 6, 2022 at 13:56

1 Answer 1

5
$\begingroup$

With layout.prop_enum(context.scene, "my_enum", "Rocket") link you can adress single enum items in layout. Third data is the item.identifier. See script.

To be more flexible, for example because of dynamically generated enum properties, we can reach the single items with bl_rna.properties["my_enum"].enum_items. In second example the enum items are looped and placed in row. Next row after third element.

import bpy

class NEW_PT_CUSTOM_73B8D(bpy.types.Panel):
    bl_label = 'Custom'
    bl_idname = 'SNA_PT_CUSTOM_73B8D'
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_context = ''
    bl_category = 'Sell'
    bl_order = 0
    bl_ui_units_x=0

    def draw(self, context):
        layout = self.layout
#        col = layout.column()
        layout.prop(bpy.context.scene, 'my_enum', expand=True)

        # first example
        layout.separator(factor=2) 
        row = layout.row(align=True)
        row.prop_enum(context.scene, "my_enum", "Rocket")        
        row.prop_enum(context.scene, "my_enum", "Jeep")        
        row.prop_enum(context.scene, "my_enum", "Truck")        
        row = layout.row(align=True)        
        row.prop_enum(context.scene, "my_enum", "Car")        
        row.prop_enum(context.scene, "my_enum", "Bike")        
        row.prop_enum(context.scene, "my_enum", "Plane")          
        
        # second example  
        layout.separator(factor=2) 
        row = layout.row(align=True)
        for index, item in enumerate(context.scene.bl_rna.properties["my_enum"].enum_items):   
            row.prop_enum(context.scene, "my_enum", item.identifier)
            if index == 2: # new line after third item
                row = layout.row(align=True)            


def register():
    bpy.types.Scene.my_enum = bpy.props.EnumProperty(name='enum', description='', items=[('Rocket', 'Rocket', '', 0, 0), ('Jeep', 'Jeep', '', 0, 1), ('Truck', 'Truck', '', 0, 2), ('Car', 'Car', '', 0, 3), ('Bike', 'Bike', '', 0, 4), ('Plane', 'Plane', '', 0, 5)])
    try: bpy.utils.register_class(NEW_PT_CUSTOM_73B8D)
    except: pass


def unregister():
    del bpy.types.Scene.my_enum
    try: bpy.utils.unregister_class(NEW_PT_CUSTOM_73B8D)
    except: pass

if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Thank you very much, this is more than I was thinking. $\endgroup$
    – Muzammil
    Commented Dec 7, 2022 at 6:41
  • $\begingroup$ Thank you @relaxed, this use of prop_enum is very interesting (and new for me). It is sad that this one, all along with some other methods, is not properly documented. $\endgroup$
    – Werwack
    Commented Dec 7, 2022 at 14:39

You must log in to answer this question.

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