6
$\begingroup$

I have an EnumProperty set up on the Scene in the register function:

def register():
    bpy.types.Scene.my_enum = bpy.props.EnumProperty(
        name = "My enum",
        description = "My enum description",
        items = [
            ("FIRST" , "First one" , "Description..."),
            ("SECNOD", "Second one", "Some other description")
        ]
    )

Now, elsewhere in the code I have a Panel, which is to display the 'enum' for selection:

class SomePanel(bpy.types.Panel):
    bl_idname      = "some_panel"
    bl_label       = "A panel"
    bl_space_type  = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context     = "render"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layout.prop(scene, "my_enum")

So far so good, the control draws correctly, but not the way I want:

Enum dropdown


Thing is - I wanted a different appearance - I didn't want a dropdown field, instead I want a radio-button like field, as the one found for example here:

enter image description here

... where I can choose one of the options regarding the color mode (BW , RGB or RGBA).

I've found that I can put the "ENUM_FLAG" in the options field of the EnumProperty definition:

def register():
        bpy.types.Scene.my_enum = bpy.props.EnumProperty(
            name = "My enum",
            description = "My enum description",
            items = [
                ("FIRST" , "First one" , "Description..."),
                ("SECNOD", "Second one", "Some other description")
            ],
            options = {"ENUM_FLAG"}
        )

This too worked fine, the result I got now is this:

enter image description here

But now I can activate both buttons at once using Shift, and that's what I want to avoid:

enter image description here

So How can make the selection exclusive? How to force Blender to switch from one option to the other (or in other words: How to disable the Shift functionality)?

$\endgroup$
  • $\begingroup$ I struggle with the title of this question, it seems to me it should include " giving visibility to all items at once" (like a radio button array). $\endgroup$ – zeffii Dec 25 '15 at 13:06
  • $\begingroup$ @zeffii Perhaps it could sound this way, but I've considered putting all the context in the title to be an overkill. $\endgroup$ – luke1985 Dec 25 '15 at 13:39
  • $\begingroup$ @zeffii Is it better now? $\endgroup$ – luke1985 Dec 25 '15 at 13:43
  • $\begingroup$ Remove the options = {'ENUM_FLAG'} to remove multi-select. You can then set a default.,, eg default='FIRST', $\endgroup$ – batFINGER Dec 25 '15 at 15:55
  • $\begingroup$ @luke1985 I don't know if there's a 'better', only time and upvotes will tell. I agree context-overkill could happen, but if you don't mention options={'ENUM_FLAG'} then non-exclusivity isn't even in the realm of possibilities for the enum. hence my thoughts about the title. $\endgroup$ – zeffii Dec 25 '15 at 20:50
3
$\begingroup$

Booleans have toggle=True, and Enums have expand=True

layout.prop(scene, "my_enum", expand=True)

enter image description here

The most extensive UI cookbook is still this one though the layout docs are definitely useful after you've struggled a little. Other than those two sources I find it best to read through the draw functions of the add-ons supplied with Blender (you probably did this already.. if not, do)

$\endgroup$
  • $\begingroup$ Almost all UI layout that you can interact with is described in Python, so if you see a UI feature you like dig through its source. $\endgroup$ – zeffii Dec 24 '15 at 14:22
  • 1
    $\begingroup$ Oh, thanks for that link - for now I've been using only the official reference guide and this site plus the existing addons source (and to be honest with you, I already found the answer while writing the question - in the netrender addon source - but I wanted to share the knowledge for such a, hopefully, common case). $\endgroup$ – luke1985 Dec 24 '15 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.