使用按钮更改 textFieldButtonGrp 中的文本

Changing the text in a textFieldButtonGrp by using the button

我正在尝试找到一种更好的方法来在按下按钮时更改多个 textFieldButtongGrps 中的 textfield

目前,我在这里的工作正常,但是有了这个,我必须在 select_Object() 中为我在 UI 中制作的任何额外的 textFieldButtongGrps 添加额外的行。这是因为我必须在 select_Object()

中声明名称

有没有办法改变这个,如果我做 6 textFieldButtongGrps,我可以让 select_Object() 函数使用类似于 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))

我想达到的目标:

def select_Object():
    cmds.textFieldButtonGrp(**self**, edit = True, tx ='' .join(sel),buttonLabel='IK OK',backgroundColor = (.5,.8,.2))

所以如果你想让你的函数更通用,你可以指定它有一个参数,我们将传递任何 textFieldButtonGrp 到。而不是在创建按钮时定义按钮的命令,我们可以先创建它,获取小部件的最终名称的变量,然后使用该变量定义函数。这样您就可以创建任意数量的 textFieldButtonGrp 并将其传递给该函数而无需执行其他代码:

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)

为了好玩,下面是一个创建随机数量按钮的示例,以进一步说明它:

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)