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 General. Show all posts
Showing posts with label General. 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.
Wednesday, June 30, 2010
It Begins
This was created to be a place where talented character and beginner TD's can share tips with the community, discuss the goings on in the industry, and keep up to date on latest techniques. Also to help keep the info in a semi-local location on the vast internet, so we don't waste any more time than we have trying to re-invent the wheel. There will tutorial video links posted from time to time as well as links to short films, other TD blogs and other interesting news that shows up. I hope to do some interviews with other TDs as well.
Subscribe to:
Posts (Atom)