(Maya Python) Is there a way to run a command from a menuItem inside a objectMenu

305 views
Skip to first unread message

Kat Patterson

unread,
Dec 20, 2022, 7:09:19 PM12/20/22
to Python Programming for Autodesk Maya
I am in need of some help, not sure how active this forum is anymore.

I am looking to see if there is any way to make a optionMenu run a command for each option in the drop down menu? I am pretty new to maya python so I do not know very much, but google is less helpful than finding everything out myself.

Here is the code that i currently have, I just am not sure how to connect the commands to the actual window ui;


creating objects.py

Kat Patterson

unread,
Dec 20, 2022, 7:11:05 PM12/20/22
to Python Programming for Autodesk Maya

Justin Israel

unread,
Dec 20, 2022, 7:14:28 PM12/20/22
to python_in...@googlegroups.com
Hi Kat,


"Note that commands attached to menu items will not get called. Attach any commands via the -cc/changedCommand flag."

And specifically the changeCommand:

The example at the bottom of the page actually shows you how to use the callback, which will receive the value of the menuItem when it is changed in the optionMenu.

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/16e3c45b-8fba-4542-83a9-750fbc0e1ce4n%40googlegroups.com.

Kat Patterson

unread,
Dec 20, 2022, 7:19:13 PM12/20/22
to Python Programming for Autodesk Maya
def printNewMenuItem( item ):
    print item

This code alone ALWAYS produces a invalid syntax on my end, so I am unable to check if that even works, or how it even works. I have looked at the documentation and am unable to find a workaround while looking at either of those documentation sites.

Any ideas?

Justin Israel

unread,
Dec 20, 2022, 7:25:09 PM12/20/22
to python_in...@googlegroups.com
On Wed, Dec 21, 2022 at 1:19 PM Kat Patterson <katier...@gmail.com> wrote:
def printNewMenuItem( item ):
    print item

This code alone ALWAYS produces a invalid syntax on my end, so I am unable to check if that even works, or how it even works. I have looked at the documentation and am unable to find a workaround while looking at either of those documentation sites.

Are you running a version of Maya using python3? If so, 

def printNewMenuItem( item ):
    print(item)
 

Any ideas?

On Tuesday, December 20, 2022 at 5:14:28 PM UTC-7 justin...@gmail.com wrote:
Hi Kat,


"Note that commands attached to menu items will not get called. Attach any commands via the -cc/changedCommand flag."

And specifically the changeCommand:

The example at the bottom of the page actually shows you how to use the callback, which will receive the value of the menuItem when it is changed in the optionMenu.

Justin


On Wed, Dec 21, 2022 at 1:11 PM Kat Patterson <katier...@gmail.com> wrote:
https://pastebin.com/SfLxjFmG

On Tuesday, December 20, 2022 at 5:09:19 PM UTC-7 Kat Patterson wrote:
I am in need of some help, not sure how active this forum is anymore.

I am looking to see if there is any way to make a optionMenu run a command for each option in the drop down menu? I am pretty new to maya python so I do not know very much, but google is less helpful than finding everything out myself.

Here is the code that i currently have, I just am not sure how to connect the commands to the actual window ui;


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/16e3c45b-8fba-4542-83a9-750fbc0e1ce4n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Kat Patterson

unread,
Dec 20, 2022, 7:42:10 PM12/20/22
to Python Programming for Autodesk Maya
Okay so that did help, but now I am back to how do I add it to each individual command (i posted my code, it is a menu that creates an object, and another option to duplicate that object said amount of times) and how do I add the button into the mix to make said dropdown options run

Justin Israel

unread,
Dec 20, 2022, 7:51:01 PM12/20/22
to python_in...@googlegroups.com
On Wed, Dec 21, 2022 at 1:42 PM Kat Patterson <katier...@gmail.com> wrote:
Okay so that did help, but now I am back to how do I add it to each individual command (i posted my code, it is a menu that creates an object, and another option to duplicate that object said amount of times) and how do I add the button into the mix to make said dropdown options run

Well like the document that I had included says, when you use optionMenu, it no longer respects the command attached to each menuItem. You would control it all through the single callback that fires when the option changes. You can either use an if/else like this:
def printNewMenuItem(item):
    if item == "Sphere":
        printSphere()
    elif item == "Cube":
        printCube()

def printSphere():
    print("It's a sphere!")

def printCube():
    print("It's a cube!")
Or you can define it in a map to make it easier:
menuCallbacks = {
    "Sphere": printSphere,
    "Cube": printCube,
}

def printNewMenuItem(item):
    cbk = menuCallbacks.get(item)
    if cbk:
        cbk()
    else:
        print("Oops. I didnt define a callback for {}".format(item))
 

Kat Patterson

unread,
Dec 20, 2022, 8:24:12 PM12/20/22
to Python Programming for Autodesk Maya
This helped greatly! Thank you so very much!!

I do have one last question, do you know of a way to make the the changes of the dropdown menus only run off of a press of a button? (as in i have a 'create' button next to each dropdown menu). As in if i change the option to 'sphere' it doesnt change it when i press the option, it changes if i press the create button? 

It is alright if there is not an option like this, I am very greatful for you helping me ♥

Justin Israel

unread,
Dec 20, 2022, 9:31:44 PM12/20/22
to python_in...@googlegroups.com


On Wed, 21 Dec 2022, 2:24 pm Kat Patterson, <katier...@gmail.com> wrote:
This helped greatly! Thank you so very much!!

I do have one last question, do you know of a way to make the the changes of the dropdown menus only run off of a press of a button? (as in i have a 'create' button next to each dropdown menu). As in if i change the option to 'sphere' it doesnt change it when i press the option, it changes if i press the create button? 

It is alright if there is not an option like this, I am very greatful for you helping me ♥

Yes just don't set the callback on the optionMenu. Instead you can set a callback on your button that will query the current value from the optionMenu. 


You will need to save the name of the optionMenu that is returned when you created it. Then you can query the value of it:

Note that the value supports the query mode, so you can do:

opt = cmds.optionMenu(savedOptionsName, q=True, value=True) 

DGFA DAG's GRAPHIC & FOTO ART

unread,
Apr 1, 2023, 6:50:13 PM4/1/23
to Python Programming for Autodesk Maya
Hi, I wrote a menu in pymel, but it does not work in Maya 2024 (no more pymel), but in 2023 it works fine.
Hope it helps. Here a short of this whole menu:


import pymel.core as pm

def dags_menu():
# call menu objects
main_window = pm.language.melGlobals['gMainWindow']
menu_obj = 'DagsMenu'
menu_label = 'Dags Tools'

if pm.menu(menu_obj, label=menu_label, parent=main_window, exists=True):
pm.deleteUI(pm.menu(menu_obj, deleteAllItems=True, e=True))

dags_menu = pm.menu(menu_obj, label=menu_label, parent=main_window, tearOff=True)


project_menu(dags_menu)
polygon_menu(dags_menu)
object_menu(dags_menu)
script_menu(dags_menu)
material_menu(dags_menu)
rigging_menu(dags_menu)
camera_menu(dags_menu)
light_menu(dags_menu)
render_menu(dags_menu)


def polygon_menu(dags_menu):
    # Main Submenu Polgon
# pm.setParent('..', menu=True)
# dags_menu = pm.menu(menu_obj, label=menu_label, parent=main_window, tearOff=True)
pm.menuItem(label='Polygon', subMenu=True, parent=dags_menu, tearOff=True)
    # pm.menuItem(divider=True)
# Quaddraw
pm.menuItem(
label='Quad Draw',
command='QuadDrawTool',
image='quadDraw_NEX32.png',
sourceType='mel',
optionBox=False,
visible=True,
)
# Sphere
pm.menuItem(
label='Sphere',
command='polySphere -r 1 -sx 20 -sy 20 -ax 0 1 0 -cuv 2 -ch 1; objectMoveCommand;',
image='polySphere.png',
sourceType='mel',
optionBox=False,
visible=True,
)
# Cube
pm.menuItem(
label='Cube',
command='polyCube -w 1 -h 1 -d 1 -sx 1 -sy 1 -sz 1 -ax 0 1 0 -cuv 4 -ch 1; objectMoveCommand;',
image='polyCube.png',
sourceType='mel',
optionBox=False,
visible=True,
)
Reply all
Reply to author
Forward
0 new messages