Message 1 of 1
Python - to create dynamic buttons with commands?

Not applicable
11-04-2012
11:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone,
I am definitely in over my head here. The original goal was to dynamically create a window of buttons that is createdh from the quick selects menu. Each button would select the given selection set and key all. I have borrowed some code from a guy who is smarter than I that has a lot of cool button options. I have included a dummy scene that has 3 pSperes and should have each sphere in a quick select set named ballSet1,ballSet2 and ballSet3. I would love any suggestions.
Thanks.
Don
Original code. THANKS RYAN!
http://www.rtrowbridge.com/blog/2010/02/maya-python-ui-example/
Here is my attempts to loop through list and dynamically create buttons. I want to dynamically create the code that goes along with the buttons as well
import maya.cmds as cmds
from functools import partial
def myfunc(inst=None, thing=None, arg=None):
print 'arg: ', arg
print 'inst: ', inst
print 'thing: ', thing
data = cmds.button(inst.btnA, query=True, label=True)
print data
## This is my addition.
def myfunc2(inst=None, thing=None, arg=None):
print 'arg: ', arg
print 'inst: ', inst
print 'thing: ', thing
data = cmds.button(inst.btnA, query=True, label=True)
print data
## what I want to do here is via a loop (IE ballSet1 then ballSet2 ect)
## is to not just get the buttons but with code select the corrisponding selection set.
## and then use the KEY ALL command.
class ui():
def __init__(self, winName="winTheWindow"):
self.winTitle = "The Window"
self.winName = winName
def create(self,setList):
if cmds.window(self.winName, exists=True):
cmds.deleteUI(self.winName)
cmds.window(self.winName, title=self.winTitle)
self.mainCol = cmds.columnLayout( adjustableColumn=True )
#this is my addition to loop through and get the buttons named and created dynamically.
for i in setList.split(","):
print i
self.btnA = cmds.button( label= i + " Key ALL", c=partial(myfunc2, self, i) ) #see myfunc2 above for my issues.
#self.btnA = cmds.button( label= i + " Key ALL", c=myfunc2(i))
# This is the original dummy code from the original author.
#self.btnA = cmds.button( label='Press Me - External Func', c=partial(myfunc, self, 'say...') )
#self.btnB = cmds.button( label='Press Me - Internal Func', c=partial(self.a, 'something...') )
#self.btnC = cmds.button( label='Press Me - Internal Func No Args', c=self.b)
cmds.showWindow( self.winName )
cmds.window(self.winName, edit=True, widthHeight=)
def a(self, myarg=None, arg=None):
print 'myarg: ', myarg
def b(self, arg=None):
print 'buttons require an argument'
print 'the argument passed in will always be the last argument'
# I already the code to get the sets to save space I just created a list.
setList = "ballSet1,ballSet2,ballSet3"
# create the window
inst = ui()
inst.create(setList)
I am definitely in over my head here. The original goal was to dynamically create a window of buttons that is createdh from the quick selects menu. Each button would select the given selection set and key all. I have borrowed some code from a guy who is smarter than I that has a lot of cool button options. I have included a dummy scene that has 3 pSperes and should have each sphere in a quick select set named ballSet1,ballSet2 and ballSet3. I would love any suggestions.
Thanks.
Don
Original code. THANKS RYAN!
http://www.rtrowbridge.com/blog/2010/02/maya-python-ui-example/
Here is my attempts to loop through list and dynamically create buttons. I want to dynamically create the code that goes along with the buttons as well
import maya.cmds as cmds
from functools import partial
def myfunc(inst=None, thing=None, arg=None):
print 'arg: ', arg
print 'inst: ', inst
print 'thing: ', thing
data = cmds.button(inst.btnA, query=True, label=True)
print data
## This is my addition.
def myfunc2(inst=None, thing=None, arg=None):
print 'arg: ', arg
print 'inst: ', inst
print 'thing: ', thing
data = cmds.button(inst.btnA, query=True, label=True)
print data
## what I want to do here is via a loop (IE ballSet1 then ballSet2 ect)
## is to not just get the buttons but with code select the corrisponding selection set.
## and then use the KEY ALL command.
class ui():
def __init__(self, winName="winTheWindow"):
self.winTitle = "The Window"
self.winName = winName
def create(self,setList):
if cmds.window(self.winName, exists=True):
cmds.deleteUI(self.winName)
cmds.window(self.winName, title=self.winTitle)
self.mainCol = cmds.columnLayout( adjustableColumn=True )
#this is my addition to loop through and get the buttons named and created dynamically.
for i in setList.split(","):
print i
self.btnA = cmds.button( label= i + " Key ALL", c=partial(myfunc2, self, i) ) #see myfunc2 above for my issues.
#self.btnA = cmds.button( label= i + " Key ALL", c=myfunc2(i))
# This is the original dummy code from the original author.
#self.btnA = cmds.button( label='Press Me - External Func', c=partial(myfunc, self, 'say...') )
#self.btnB = cmds.button( label='Press Me - Internal Func', c=partial(self.a, 'something...') )
#self.btnC = cmds.button( label='Press Me - Internal Func No Args', c=self.b)
cmds.showWindow( self.winName )
cmds.window(self.winName, edit=True, widthHeight=)
def a(self, myarg=None, arg=None):
print 'myarg: ', myarg
def b(self, arg=None):
print 'buttons require an argument'
print 'the argument passed in will always be the last argument'
# I already the code to get the sets to save space I just created a list.
setList = "ballSet1,ballSet2,ballSet3"
# create the window
inst = ui()
inst.create(setList)