Monday, August 22, 2011

Finding out what button you clicked and how to get rid of it

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):

win = cmds.window()
fullPathName = cmds.button(btn, q=True, fpn=True)
cmds.button(btn, e=True, c=partial(delFunction, fpn))


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.

No comments:

Post a Comment