Recently I was coding a tool and thought it would be cool to add a feature that allowed the user to right click on a button and then select a menu option to delete it. The question that arose was this: How do I figure out what button was pressed?(since there was a whole bunch of them and they were being created dynamically). There is most likely a better way to tackle this but since there is no immediate answer and i'm slightly retarded here is what I came up with as a solution.
Here is the code:
import maya.cmds as cmds
from functools import partial
def delFunction(btn, *args):
cmds.deleteUI(btn)
win = cmds.window()
cmds.rowColumnLayout()
btn=cmds.button()
fullPathName = cmds.button(btn, q=True, fpn=True)
cmds.button(btn, e=True, c=partial(delFunction, fpn))
cmds.showWindow(win)
Basically what I did was create the button then grab the fullPathName of the button in the UI, this gives me what button it is. Then I pass that button into the function that deletes it using the 'partial' function from functools. Partial allows one to pass in an argument(s) to a function while in the command field. Then I simply use the deleteUI command and pass in the button I want to get rid of. Easy really, it's just a matter of figuring it out.
Showing posts with label User Interface. Show all posts
Showing posts with label User Interface. Show all posts
Monday, August 22, 2011
Right-click Menu in Maya using Python
This might be a little obscure but thought I would throw this out there for anyone who searches this with Google. I know it was frustrating to find any kind of real resource on the subject so here it is. You can add a right-click pop-up menu to pretty much any UI element with Mel/Python in Maya. Here is a link to the docs about this menu:
http://download.autodesk.com/us/maya/2011help/CommandsPython/popupMenu.html
Take a look at the example. Not too informative but basically all you need is a parent to attach the menu to and set the button to be the RMB, and you are done!
Example:
import maya.cmds as cmds
win = cmds.window()
cmds.rowColumnLayout()
btn = cmds.button(l='Click Me')
popup = cmds.popupMenu(parent=btn, ctl=False, button=3)
item1 = cmds.menuItem(l='Item1', c='whatever and')
item2 = cmds.menutItem(l='Item2', c='whatever')
cmds.showWindow(win)
and there you go! That's really all there is to it.
http://download.autodesk.com/us/maya/2011help/CommandsPython/popupMenu.html
Take a look at the example. Not too informative but basically all you need is a parent to attach the menu to and set the button to be the RMB, and you are done!
Example:
import maya.cmds as cmds
win = cmds.window()
cmds.rowColumnLayout()
btn = cmds.button(l='Click Me')
popup = cmds.popupMenu(parent=btn, ctl=False, button=3)
item1 = cmds.menuItem(l='Item1', c='whatever and')
item2 = cmds.menutItem(l='Item2', c='whatever')
cmds.showWindow(win)
and there you go! That's really all there is to it.
Subscribe to:
Posts (Atom)