2
$\begingroup$

I created a drop-down menu, with a operator_menu_enum referring to an EnumProperty.

When I select an entry in this menu, it closes, and display the default text string, instead of displaying the selected entry's string.

How to keep the selected entry displayed in the menu's head after selection ?

Edit : Here is an example script of the way I make the menu :

import bpy

class STUFF_PANEL(bpy.types.Panel):
    bl_idname = "SCENE_PT_stuffSelect"
    bl_label = "Stuff selection"
    bl_space_type = "PROPERTIES"
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.operator_menu_enum("stuff_list.menu", 'stuffList', text="Stuff list")
        if bpy.props.selectedStuff:
            row = layout.row()
            row.label("Selected stuff :")
            row.label(str(bpy.props.selectedStuff[1]))


class STUFF_LIST_OT_Menu(bpy.types.Operator):
    bl_idname = "stuff_list.menu"
    bl_description = "select something here"
    bl_label = "Menu"

    def get_stuff(self, context):
        return bpy.props.config_stuff_list

    stuffList = bpy.props.EnumProperty(
        items = get_stuff, name = "Stuff", description = "The stuff list")

    def execute(self, context):
        bpy.props.selectedStuff = [
            n for i, n in enumerate(bpy.props.config_stuff_list)\
            if n[0] == self.stuffList][0]
        return{'FINISHED'}


def register():
    bpy.props.selectedStuff = False
    bpy.props.config_stuff_list = [
        ("1","stuff 1","0"),("2","stuff 2","0"),("3","stuff 3","0")]
    bpy.utils.register_module(__name__)


def unregister():
    del bpy.props.config_stuff_list
    del bpy.props.selectedStuff
    bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()
$\endgroup$
  • $\begingroup$ Could you pose a simple example script? $\endgroup$ – ideasman42 Sep 25 '13 at 1:53
  • $\begingroup$ Ok, edited my question with an example of how I use the APi to make the menu. $\endgroup$ – Polosson Sep 25 '13 at 17:04
  • $\begingroup$ NameError: name 'bpy' is not defined, please test the example and make sure it works. $\endgroup$ – ideasman42 Sep 25 '13 at 17:28
  • $\begingroup$ Checked the example, looks like you may be better off not to use the API like this, can you explain what this is for? $\endgroup$ – ideasman42 Sep 26 '13 at 1:31
  • $\begingroup$ This is part of a script which allows user to modify a configuration file, within the UI. (using python 'configparser' and so on). More precisely, this part correspond to a section of the config file which contains a list of json values, so the user have to choose a value to be able to modify the json content. $\endgroup$ – Polosson Sep 26 '13 at 11:19
4
$\begingroup$

This feature isn't supported by operator_menu_enum, the purpose of this is to be a submenu containing options. Not a persistent drop-down selection box, you may be better off to add an update callback to an enum property, then selecting an option can run some python code.

What you wan't can be done, but I think you're looking in the wrong place.

Simple answer:

You could replace the row.operator_menu_enum line with this.

    if bpy.props.selectedStuff:
        label = str(bpy.props.selectedStuff[1])
    else:
        label = "Stuff list"
    row.operator_menu_enum("stuff_list.menu", 'stuffList', text=label)

Longer answer:

it looks like this is not using the API correctly. You can't easily access operator properties from a panel (when the operator isn't running), you're better off to add a property-group to the scene that contains all your tool properties, then have the operators invoke function initialize from these settings (this is how most Blender operators do this).

Note:

  • Setting attributes in bpy.props is bad practice, just store this in the script. (as a class attribute for example)
$\endgroup$
  • $\begingroup$ Your 'simple answer' works great at the moment, if I have enough time I'll go more in-depth with the other one. Thx! $\endgroup$ – Polosson Sep 26 '13 at 11:34

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.