PDA

View Full Version : Help with dynamic Buttons


AaronSnyder88
03-13-2011, 08:23 AM
Hey Guys,

I am fairly new to python and have been working on a script for saving poses.
I have it working to save out and load the poses, but I cannot figure out how to actually link a dynamic button to a function since it is not a globally declared name.

Is there a way to call a button name on click or to plug the name into the function for each?
I have the posers function creating a button for each pose so I need it to run the same function but only for that specific name.

def posers(*args):
poseName=cmds.textField('Pox', q=True, text=True)
cmds.setParent('Posie')
b=cmds.button(poseName, l=poseName, command=poop)

def poop(*args):
awesome=cmds.button(poseName, q=True, l=True)

ginodauri
03-13-2011, 11:57 PM
If i understand maybe this will help:


...
buttonLabel="pose"
button(l= buttonLabel, c=printLabel(buttonLabel))
...

def printLabel(buttonLabel):
print buttonLabel

AaronSnyder88
03-14-2011, 01:00 AM
I feel like that is the right idea, but am unsure of how to actually have the button
run based on its label's name....


# Function gets all attrs from current pose
def posers(buttonLabel):
theData = []
global o
iconName=cmds.textField('iconors', q=True, text=True)
poseName=cmds.textField('Pox', q=True, text=True)
myFileObject=open('/Users/Asnyder/Library/Preferences/Autodesk/maya/'+poseName+'.txt', 'w')
cmds.select(iconName)
y=cmds.ls(sl=True)
ylength=len(y)-1
for i in y:
i=0
while i < ylength:
x=cmds.listAttr(y[i], keyable=True, locked=False, )
xlength=len(x)-1
x.remove(x[0])
for each in x:
o.append(y[i]+"."+each)
i=i+1
for sel in o:
a=cmds.getAttr(sel)
theData.append(sel + ' ' +str(a)+' \n')
for lines in theData:
myFileObject.writelines(lines)
myFileObject.close()
cmds.setParent('Posie')
cmds.button(poseName, l=poseName, command=poop(buttonLabel))

#Function for setting back to saved pose
def poop(buttonLabel):
print buttonLabel myFileObject=open('/Users/Asnyder/Library/Preferences/Autodesk/maya/'+buttonLabel+'.txt', 'r')
theLines = myFileObject.readlines()
count = 0
for line in theLines:
theLine = theLines[count]
theSplit = str.split(theLine)
Namer = theSplit[0]
Valers = theSplit[1]
Valer = float(Valers)
cmds.setAttr(Namer, Valer)
count +=1
myFileObject.close()

# I need to plug in the name or label of the button into the MyFileObject inside the poop
function so that it loads the file based on that pose name

AaronSnyder88
03-14-2011, 01:02 AM
Btw, my code apparently got all tabbed over to the same point in each line so
there are actually no identation errors

NaughtyNathan
03-14-2011, 10:58 AM
Btw, my code apparently got all tabbed over to the same point in each line
that's what the board's tags are for...

AaronSnyder88
03-15-2011, 04:40 PM
Thanks for the help nathan, but I figured it out anyways
:p

uiron
03-15-2011, 05:44 PM
best way is to utilize the fact that python version of UI handlers accept python callable as an argument (function pointer, object with __call__ defined, etc). like this:


class MyButtonHandler:
def __init__(self,name=None):
"""
this is constructor; we can accept execution parameters here, or
receive later through object configuration
"""
self.name = name

def __call__(self,*args):
"""
this gets executed when button is pushed. note that we take all parameters
for execution (in this case, name) from the instance, not from function arguments.
"""
print "hi, I'm", self.name

cmds.window()
cmds.columnLayout()
cmds.button(c=MyButtonHandler('instance 1'))
cmds.button(c=MyButtonHandler('instance 2'))

# can also do this:
handler3 = MyButtonHandler()
handler3.name = 'configured later'
cmds.button(c=handler3)

cmds.showWindow()

CGTalk Moderation
03-15-2011, 05:44 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.