PDA

View Full Version : Dynamic Buttons to function Python


AaronSnyder88
03-12-2011, 05:45 PM
Hey guys, I am fairly new to Python and am working on a script to save out poses.
I have it currently able to save and load based on a textField, but am struggling on hooking up the actual button to the corresponding file(since I am having them named by a textfield, they will be the same name).
Is there any way I can get the button name or some way to tell the button to only pull the name of it's own label? Me love you long time if you help me :twisted:
This is the code for what I have so far:

# Function gets all attrs from current pose
def posers(*args):
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')
print theData
for lines in theData:
myFileObject.writelines(lines)
myFileObject.close()
cmds.setParent('Posie')
b=cmds.button(poseName, l=poseName, command=poop)

#Function for setting back to saved pose
def poop(*args):
awesome=cmds.button(poseName, q=True, l=True)
myFileObject=open('/Users/Asnyder/Library/Preferences/Autodesk/maya/'+awesome+'.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)
print Namer
print Valer
cmds.setAttr(Namer, Valer)
count +=1
myFileObject.close()

mduvekot
03-14-2011, 01:15 AM
I have a snippet that shows how to pass arguments to buttons here: http://www.thnkr.com/wiki/index.php?title=Pass_arguments_to_a_button_command

AaronSnyder88
03-14-2011, 09:56 AM
Well I think I got it working where it is giving me the name back, so thank you....
However, it is only repeating the name of the last button made. How can I get this to work so it can tell each button separately from one another?
This is basically just an adaptation of your snippet, but that creates a new button for each time it is clicked. Which is essentially what I need for my own script.

from maya import cmds as cmds
class simpleUI(object):
def __init__(self):
cmds.window()
cmds.columnLayout('shitty')
cmds.textField('poop')
self.button = cmds.button(label='Apply')
funco = lambda arg : self.buttonCallback(self.button)
cmds.button(self.button, edit=True, command=funco)
cmds.showWindow()

def buttonCallback(shit, *args):
cmds.setParent('shitty')
poop=cmds.textField('poop', q=True, text=True)
shit.button = cmds.button(poop, label=poop)
func = lambda arg : shit.poop(shit.button)
cmds.button(shit.button, edit=True, command=func)

def poop(shit, *args):
print 'You pressed:', args

ui = simpleUI()

CGTalk Moderation
03-14-2011, 09:56 AM
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.