selected menu name how to ?

i defined an operator for a menu

and call it with

col.operator_menu_enum(‘view_3d.my_color1’, ‘colormenu1’, ‘Menu Selection’)

now after you select a menu item
it does not show the selected name ?!

is there a way to show the selected item name in the menu ?

i can show the operator if needed !


thanks

Put a little more effort into your question (and title), including the code and explaining the problem in detail, and you might find that someone is willing to help.

Like this:

import bpy

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    myprop = bpy.props.EnumProperty(items=(('1','1','1'),('2','2','2')))

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        self.__class__.bl_label = self.myprop
        return {'FINISHED'}

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        layout.operator_menu_enum(SimpleOperator.bl_idname, "myprop", text=SimpleOperator.bl_label)


def register():
    bpy.utils.register_class(LayoutDemoPanel)
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()


here is my operator

class FIRSTOperator(bpy.types.Operator): # When an option in the operator menu is clicked, this is called

‘’‘Operator’’’
bl_idname = ‘view_3d.my_color1’

bl_idname = ‘FIRSTOperator’

bl_label = ‘Operator’

Define possible operations

colormenu1 = EnumProperty(items=(
(‘1’, ‘Woods’, ‘The first item’),
(‘2’, ‘Metals’, ‘The second item’),
(‘3’, ‘Plastics’, ‘The Third item’),
(‘4’, ‘Glass’, ‘The Fourth item’),
(‘5’, ‘Stones’, ‘The Fith item’),
(‘6’, ‘Car Paint’, ‘The Fith item’)

       ))

def execute(self, context):

global last_menu1 # Access global Var so we can later draw it in the panel

last_menu1 = self.properties.colormenu1 # Store the choosen operation / Selection
print (‘selection shade=’,self.properties.colormenu1[0])

print (’ $$$$$$$$$$$$$ in myoperatormenu1 Selection =’,last_menu1)

return {‘FINISHED’}

1 - your enum use prop
mine use local enum
is it important ?

2 - calling operator

col.operator_menu_enum(‘view_3d.wood1’, ‘woodmenu1’, ‘Menu Selection’)

print (’ selected Woods menu menu value =’,wood1_menu1)

layout.operator_menu_enum(SimpleOperator.bl_idname, “myprop”, text=SimpleOperator.bl_label)

wondering if my operator can be modified to get the text will test later on

3 - your are calling it with label name
i use the bl ID name
can mine be also be call with bl name?

4 - can you elaborate on the exe

self.class.bl_label = self.myprop

is it instanciated class var?

5 - self.properties.colormenu1[0])

i use a global var to pass the first item in enum list to identify the selection
but having problem with passing the value to panel
will work more on that bug

now is there a way to extract other item from enum list
i tried to print the name in
(‘1’, ‘Woods’, ‘The first item’)

but it did not work!

i can print self.properties.colormenu1[0]
but if i try self.properties.colormenu1[1]
it does not work

anyway to get other item in enum list ?

in your example you use a prop enum so i guess it might not need to be declare as global to get item inside may be!

thanks

1 - no clue what you mean, looks the same.

2 - change “SimpleOperator” to your operator’s class name

3 - huh? I’m passing the bl_idname to operator_menu_enum, and set the text to the operator’s bl_label

4 - no, it’s the operator class itself, it’s the same as SimpleOperator.bl_label = …

5 - not sure what you mean but your approach seems wrong, there’s no need for global var. See my example code.

got this from my notes

enum list

The first item of the tuple is the value of the property,
the second one is what will be displayed at the screen
and the third one is a small description that appears in some help pop-ups

The “items” parameter to EnumProperty is a series of string triplets
the selection is only the sequence number of the list and is a string not a number!

but how can i get the 1 2 and 3 parts for a selection from enum list ?
it should be possible i hope

like for the 2 part of enum list i use a specific meaningfull name which i could re use somewhere else!

will try to make a new panel with this new operator with name in button

thanks

i was hoping to somehow access the data inside the enum prop
but was not able !

so i change the data in enum prop like this

colormenu1 = bpy.props.EnumProperty(items=(
(‘1 Woods’, ‘Woods’, ‘The first item’),

then i can extract and split the data and see the data in the menu button!

it is not very elegant but at least menu is working now with proper name in menu

unless there i a better way

thanks

ah well, in this case, define the enum items in global scope and set it like EnumProperty(items=enum_items)

however, you could also access a global enum entry’s name like

bpy.types.RenderSettings.bl_rna.properties[‘pixel_filter_type’].enum_items[‘MITCHELL’].name # MITCHELL is the id

your loosing me here!

can you show small example
or modify the given example to show this

i tought the enum was like a tuple but seems it does not work like that
i was unable to access it that way!

at least i was able to make my multi levels menu in panel work ok
but might work better with this new method!

it is not very elegant but i extracted the index number and did my if select to run some function and see the item name in menu button!

thanks

variant 1:

import bpy

enum_items = (('ONE','One','First'),('TWO','Two','Second'))

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    myprop = bpy.props.EnumProperty(items=enum_items)

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        self.__class__.bl_label = (e for e in enum_items if e[0] == self.myprop).__next__()[1]
        return {'FINISHED'}

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        layout.operator_menu_enum(SimpleOperator.bl_idname, "myprop", text=SimpleOperator.bl_label)


def register():
    bpy.utils.register_class(LayoutDemoPanel)
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

Variant 2:

import bpy

enum_items = (('ONE','One','First'),('TWO','Two','Second'))
enum_items_dict = {id: name for id, name, desc in enum_items}

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    myprop = bpy.props.EnumProperty(items=enum_items)

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        self.__class__.bl_label = enum_items_dict[self.myprop]
        return {'FINISHED'}

class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        layout.operator_menu_enum(SimpleOperator.bl_idname, "myprop", text=SimpleOperator.bl_label)


def register():
    bpy.utils.register_class(LayoutDemoPanel)
    bpy.utils.register_class(SimpleOperator)


def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)
    bpy.utils.unregister_class(SimpleOperator)


if __name__ == "__main__":
    register()

Variant 3:

import bpy

enum_items = (('ONE','One','First'),('TWO','Two','Second'))
enum_items_dict = {id: name for id, name, desc in enum_items}


class LayoutDemoPanel(bpy.types.Panel):
    """Creates a Panel in the scene context of the properties editor"""
    bl_label = "Layout Demo"
    bl_idname = "SCENE_PT_layout"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "scene"

    def draw(self, context):
        layout = self.layout

        layout.prop(context.scene, "myprop", text="")

def enum_cb(self, context):
    run_some_code(self.myprop)
    
def run_some_code(prop_id):
    print(prop_id, "->", enum_items_dict.get(prop_id))

def register():
    bpy.utils.register_class(LayoutDemoPanel)
    bpy.types.Scene.myprop = bpy.props.EnumProperty(items=enum_items, update=enum_cb)


def unregister():
    bpy.utils.unregister_class(LayoutDemoPanel)
    del bpy.types.Scene.myprop


if __name__ == "__main__":
    register()

first one looks interesting
clean name in the menu button

but then how do you select item number for doing an if statement

may be should wait a little i’ll open a new WIP for the script i began later on
first have to find some I/O names for new nodes methods for cycles!

thanks

first one looks interesting

All 3 show up the same.

but then how do you select item number for doing an if statement

Still self.myprop ?!

i was thinking more in terms of coding
first is simple to understand
the 2 others are way more complicated and use some news commands
which i don’t understand how to for now!

so will try to stick with first one for now
but need to clean up the script on which i work then i’ll be able to upload it in a WIP
then you might be able to see the overall looping between 2 levels of menus

i had not seen before a multilevel menu for panel but now i got one working

thanks

variant 2 is deffo the simplest, it uses a dict comprehension. variant 1 uses a generator with filter expression and returns the first element using the magic method next, that’s non-trivial and hard to understand if you don’t know generators well. My personal favorite is variant 3, as it leaves labeling up to blender and performs an action when the property callback is called. It can’t show an initial text however, but using text="" for a label in front of the dropdown isn’t that bad either.