Create functions in Maya optionMenuGrp

Is it possible to pass functions into Maya optionMenuGrp (combobox-alike)?

I have done up a simple UI where I have this combobox (maya cmds not PyQt) and there are 2 options in it.

I tried out using the following code but I was prompted with errors in the menuItem statements:

# Error: non-keyword arg after keyword arg
#   File "<maya console>", line 9
# SyntaxError: non-keyword arg after keyword arg #

Then I tried playing around with the placement of test1_func() or test2_func, it print out the statement as the code is executed but not executing it anymore if I tried to select the second menu item…

My code:


import maya.cmds as cmds

cmds.window(w=150, h=100)
cmds.columnLayout(adjustableColumn=True)
form = cmds.formLayout(numberOfDivisions=100)

exportSelection = cmds.optionMenuGrp(label=' Selection Options ', columnWidth=(1, 90))
test1 = cmds.menuItem(label='Export using item selection range', c = test1_func())
test2 = cmds.menuItem(test2_func(), label='Export using time slide range', c = test2_func())

startLbl = cmds.text( label='Start' )
start = cmds.textField(startLbl, w = 100, h = 20)

endLbl = cmds.text( label='End' )
end = cmds.textField(endLbl, w = 100, h = 20)


cmds.formLayout(form, edit=True, attachForm=[\
(exportSelection, 'top', 5),\
(exportSelection, 'left', 5),\

(startLbl, 'top', 40),\
(startLbl, 'left', 7),
(start, 'top', 35),\
(start, 'left', 45),

(endLbl, 'top', 60),\
(endLbl, 'left', 7),
(end, 'top', 60),\
(end, 'left', 45)])


def test1_func(*args):
    print "option 1 is selected"

def test2_func(*args):
    print "option 2 is selected"

cmds.showWindow()


you can use lambda or functools.partial.

either can be used to pass arguments to the function hooked up to the button. Theodox has written extensively on this topic over at his blog.

I answered your SO question with this already, but for lurkers:

  1. Menu items in optionMenu / OptionMenuGrp don’t get triggered automatically
  2. Hook the change event in the optionMenu itself with a callback
  3. The callback will get the selected menu item as an argument, so you can use that to query the callbacks attached to the menu item and fire them manually

Here’s a stripped down version:


def optionMenuCallback(*args):
    fn = cmds.menuItem (args[0], q=True, c=True)
    if fn:
        fn()

def menu1Callback():
    cmds.polySphere(n='created_by_item1')
    print 'menu 1 fired'

def menu2Callback():
    cmds.polyCube(n='created_by_item_2')
    print 'menu 2 fired'

w = cmds.window(w=150, h=100)
cmds.columnLayout(adjustableColumn=True)
form = cmds.formLayout(numberOfDivisions=100)
exportSelection = cmds.optionMenuGrp(label='example', cc=optionMenuCallback)
test1 = cmds.menuItem('item1', c = menu1Callback)
test1 = cmds.menuItem('item2', c= menu2Callback)
cmds.showWindow(w)

and here’s the equivalentin mGui:


class ActiveOptionMenu(OptionMenu):
    """
    A variant of the default OptionMenu which will call the command attached to
    the menuItem.  This allows for dropdown menus  which behave like regular
    menus rather than like pure dropdown selectors

    """

    def __init__(self, key, *args, **kwargs):
        super(ActiveOptionMenu, self).__init__(key, *args, **kwargs)
        self.changeCommand += self.fire_menu_callback

    def fire_menu_callback(self, *args, **kwargs):
        """
        this ensures that the command attached to the selected MenuItem is fired when that menu is selected
        """
        selected = self.Controls[self.select - 1]
        selected.command()