Skip to main content
Make example script consistent
Source Link
Polosson
  • 6.6k
  • 8
  • 34
  • 61

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.selected_stuffselectedStuff = [
            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
 return{'FINISHED'}   bpy.utils.unregister_module(__name__)

if __name__ == "__main__":
    register()

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 :

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")
        # (...)


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.selected_stuff = [
            n for i, n in enumerate(bpy.props.config_stuff_list)\
            if n[0] == self.stuffList][0]
        # (...)
        return{'FINISHED'}

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()
Adding an example script
Source Link
Polosson
  • 6.6k
  • 8
  • 34
  • 61

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 :

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")
        # (...)


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.selected_stuff = [
            n for i, n in enumerate(bpy.props.config_stuff_list)\
            if n[0] == self.stuffList][0]
        # (...)
        return{'FINISHED'}

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 ?

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 :

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")
        # (...)


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.selected_stuff = [
            n for i, n in enumerate(bpy.props.config_stuff_list)\
            if n[0] == self.stuffList][0]
        # (...)
        return{'FINISHED'}
Tweeted twitter.com/#!/StackBlender/status/382661119133110273
Source Link
Polosson
  • 6.6k
  • 8
  • 34
  • 61

how to make "operator_menu_enum" keep the selected value displayed

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 ?