I'm trying to find a better way to change the textfield
in multiple textFieldButtongGrps
when I press their buttons.
Currently, what I have here works, but with this, I have to make additional lines in select_Object()
for any additional textFieldButtongGrps
I make in my UI. This is because I have to declare the name in select_Object()
Is there a way to change this so that if I make 6 textFieldButtongGrps
, I can just have the select_Object()
function use something similar to self?
import maya.cmds as cmds
window = cmds.window()
cmds.columnLayout()
tsL0 =cmds.textFieldButtonGrp(ed=False, adj=1,cal=(1,"left"),cw3=(10,100,25), cl3=("left","left","left") ,
buttonLabel='Root FK',bc = 'select_Object()' )
gtF0 = tsL0
tsL1 =cmds.textFieldButtonGrp(ed=False, adj=1,cal=(1,"left"),cw3=(10,100,25), cl3=("left","left","left") ,
buttonLabel='Root FK',bc = 'select_Object()' )
gtF1 = tsL1
cmds.showWindow( window )
def select_Object():
cmds.textFieldButtonGrp(**gtF0**, edit = True, tx ='' .join(sel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
What I'd like to achieve:
def select_Object():
cmds.textFieldButtonGrp(**self**, edit = True, tx ='' .join(sel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))
So if you want your function to be more generic you can specify it to have one parameter where we will pass any textFieldButtonGrp
to. And instead of defining the button's command as it is created, we can first create it, get a variable of the widget's final name, THEN define the function with that variable. That way you can create as many textFieldButtonGrp
as you want and pass it to that function without doing additional code:
import maya.cmds as cmds
def select_Object(txt_btn_grp): # Define parameter for the `textFieldButtonGrp`
# Get selection.
sel = cmds.ls(sl=True)
if not sel:
raise RuntimeError("No objects are selected!")
cmds.textFieldButtonGrp(txt_btn_grp, edit=True, tx=''.join(sel), buttonLabel='IK OK', backgroundColor=(.5, .8, .2))
window = cmds.window()
cmds.columnLayout()
tsL0 = cmds.textFieldButtonGrp(
ed=False,
adj=1,
cal=(1, "left"),
cw3=(10, 100, 25),
cl3=("left", "left", "left"),
buttonLabel='Root FK'
)
cmds.textFieldButtonGrp(tsL0, e=True, bc='select_Object(tsL0)') # Now define the function with its variable.
tsL1 = cmds.textFieldButtonGrp(
ed=False,
adj=1,
cal=(1, "left"),
cw3=(10, 100, 25),
cl3=("left", "left", "left"),
buttonLabel='Root FK'
)
cmds.textFieldButtonGrp(tsL1, e=True, bc='select_Object(tsL1)')
cmds.showWindow(window)
And just for fun, here's an example of creating a random amount of buttons to illustrate it even more:
import maya.cmds as cmds
def select_Object(txt_btn_grp): # Define parameter for the `textFieldButtonGrp`
print txt_btn_grp
# Get selection.
sel = cmds.ls(sl=True)
if not sel:
raise RuntimeError("No objects are selected!")
cmds.textFieldButtonGrp(txt_btn_grp, edit=True, tx=''.join(sel), buttonLabel='IK OK', backgroundColor=(.5,.8,.2))
window = cmds.window()
cmds.columnLayout()
buttons = []
for i in range(10):
buttons.append(cmds.textFieldButtonGrp(
ed=False,
adj=1,
cal=(1, "left"),
cw3=(10, 100, 25),
cl3=("left", "left", "left"),
buttonLabel='Button {}'.format(i)
))
cmds.textFieldButtonGrp(buttons[i], e=True, bc='select_Object(buttons[{}])'.format(i))
cmds.showWindow(window)