Pymel menu callbacks


#1

I have a menu created with pymel with each item being using:

pm.menuItem(p=parentMenu, label=labelName, command=pm.Callback(callback))

This works fine if I just click the menu items. But if I shift click it to create a shelf shortcut the code will be something like “<function callback at 0x00000000420EE198>”. This won’t work as it’s only a memory adress.

What can I do to make it create a shelf button with the actual contents of the callback?


#2

my menu items point to a RunTimeCommand.
When Maya starts up, I register all of my tools as RunTimeCommands. These get added to Menus and Shelves. And allows for menu items to be converted to shelves.

It also makes it easy for the artists to find any tool in the Hotkey Editor and assign their own hotkey to it.

As a bonus, to the developers, we can change the code under the RunTimeCommand at any time without disrupting the menus, shelves or hotkeys.


#3

Can you be more specific?

I created a runTimeCommand (“rtc_one”) and then create my menu like so:


def clickedMenuItem_1():
	import maya.mel as mel
	mel.eval('rtc_one')

pm.menuItem(p=MAIN_MENU, label='Menu Item 1', command=lambda *args:clickedMenuItem_1())

The shelf button still becomes ‘<function callback at 0x000000004431D518>’

Do I have to do everything in mel?


#4

Okay I ended up just defining each command as a string in a class

class commands:
  def __init__(self):
    self.clickedColorMod = "from MayaTools.Misc.Mel import runMel
runMel('mel/colorMod.mel','colorMod();')"
    self.clickedSetVertexNormals = "from  MayaTools.Misc.SSVN.SetVertexNormals import setVertexNormals
setVertexNormals()"


and then just let the menuItem callback be commands.clickColorMod etc.


#5

that will work, but then all Shelf Buttons made from the Menu will be invalid if you ever have to change the name of function or move it somewhere else.

look into the runTimeCommand command. Its only available in MEL, so you need to use maya.mel.eval() to execute it.