menuItem calling modules

20 views
Skip to first unread message

vilkdage

unread,
Nov 24, 2017, 5:54:30 AM11/24/17
to Python Programming for Autodesk Maya

Hi, 
I have a pm.menu in which holds menuItems. In one of them I would like to call a QtWidget which was written inside the class:

def CreateMenu():
   
...
    toolUI
= fileName.className()
    pm
.menuItem(label="Tool", command=toolUI.run)

menuItem does not accept a function which calls class as it considers class as an argument (as far as I understand). The function inside the class that I want to run does not have any arguments.

the main menu was written in function, not as a class. I cannot change that. Is there a way to run a module inside menuItem?

Marcus Ottosson

unread,
Nov 24, 2017, 7:51:21 AM11/24/17
to python_in...@googlegroups.com

Try this.

pm.menuItem(label="Tool", command=lambda _=None: toolUI.run())

What happens is, the menuItem will try and call your function with an argument (the index of the menu item itself? I forget), and this lambda will simply intercept and discard it. You could have written lambda: toolUI.run() but by having the lambda take an argument you allow the call to be repeated via the g key, like it normally would without the lambda.

Another option is to accept the argument in the run() function, something you discard.

Yet another option is to make a second “wrapper” function that you use solely for menus.

def run(_):
  return toolUI.run()

pm.menuItem(label="Tool", command=run)

--
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_maya+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b5ce5c79-4757-4d48-8229-a03fa0b447ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vilkdage

unread,
Nov 24, 2017, 9:03:54 AM11/24/17
to Python Programming for Autodesk Maya
Thank you Marcus, it worked!
Reply all
Reply to author
Forward
0 new messages