如何锁定这些属性?

2024-06-02 09:10:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我才刚开始编码,所以我想我应该试着做一些简单的事情,但是,我不能从我的ls中选择对象,我知道错误在我的def attrLockT中,我想知道是否有人能帮我解决这个问题,并理解我做错了什么?在

import maya.cmds as cmds

#Selects the attributes

sat = ['.tx', '.ty', '.tz']
sar = ['.rx', '.ry', '.rz']
sas = ['.sx', '.sy', '.sz']

#Creates the list of currently selected objects

myList = cmds.ls(sl = True)

#Lock the translate attributes of the selected objects

def attrLockT(*args):
    checkAttr=cmds.getAttr (myList[0] + sat)

    if (checkAttr == 0):
        cmds.setAttr(myList[0] + sat, lock = 1)

#Delete window if it is already open
if cmds.window('animationCtrl', exists=True):
    cmds.deleteUI('animationCtrl', window=True)

#Setup the window
cmds.window(
    'animationCtrl',
    title = "Animation Controls",
    widthHeight = (300, 500),
    s = False)

form = cmds.formLayout()
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout(
    form, 
    edit=True,
    attachForm=(
        (tabs, 'top', 0),
        (tabs, 'left', 0),
        (tabs, 'bottom', 0),
        (tabs, 'right', 0)))


#Layout for the first tab
child1 = cmds.gridLayout( numberOfRowsColumns=(4, 3) , cwh = (100, 50))
cmds.text(label = "")
cmds.text(label = "Lock", align = "center", h = 20, w = 250)
cmds.text(label = "")
cmds.button(label = "Translate", h = 300, w = 250, c = attrLockT)
cmds.button(label = "Rotate", h = 50, w = 250)
cmds.button(label = "Scale", h = 50, w = 250)
cmds.text(label = "")
cmds.text(label = "Unlock", align = "center", h = 20, w = 250)
cmds.text(label = "")
cmds.button(label = "Translate", h = 50, w = 250)
cmds.button(label = "Rotate", h = 50, w = 250)
cmds.button(label = "Scale", h = 50, w = 250)
cmds.setParent( '..' )

#Layout for the second tab
child2 = cmds.rowColumnLayout(numberOfColumns=3)
cmds.button()
cmds.button()
cmds.button()
cmds.setParent( '..' )

cmds.tabLayout(
    tabs,
    edit=True,
    tabLabel=((child1, 'Lock/Unlock'), (child2, 'Keyable/Unkeyable')))

cmds.showWindow('animationCtrl')

抛出的错误是

^{pr2}$

Tags: thetexttruelockifbuttonwindowsat
2条回答

这行吗?在

myList[0] + sat

myList[0]类型是list?因为sat变量肯定是list。在

如果myList只是string的列表,那么myList[0]将只是string类型的一个元素,它将产生一个错误。在

简化您的程序,只需保留锁定例程和带有按钮的窗口,以查看将发生什么,请确保将object+属性的正确名称传递给getAttr-只需string,就像'obj.attrib'。在

函数的一些特定于python的线索

如果需要对两个列表求和: 在

[ objName + attName for objName, attName in zip(myList, sat) ]

例如,这将导致['obj1.tx', 'obj2.ty', 'obj3.tz']

如果需要将属性列表应用于对象:

^{pr2}$

这将导致['obj1.tx', 'obj1.ty', 'obj1.tz']

如果需要对所有对象应用相同的属性列表:

[ objName + attName for objName in myList for attName in sat ]

将导致['obj1.tx', 'obj1.ty', 'obj1.tz', 'obj2.tx', ..., 'obj3.tz']

然后可以在结果列表上调用锁定函数:

def locker(a):
    checkAttr = cmds.getAttr(a)
    if (checkAttr == 0):
        cmds.setAttr(a, lock = 1)

最后应该是:

def attrLockT(*args):
    atrlist = [ ..... ]
    for a in atrlist:
        locker(a)

两个问题:

首先,要循环遍历各个属性并将它们与对象名称连接起来:

def lockTranslate(*args):
    for obj in mylist:
        for attr in ['.tx', '.ty', '.tz']: 
            cmds.setAttr(obj + "." + attr, l=True)

第二,也许更重要的是,你可能会在你的职能范围上遇到问题。在您输入的表单中,函数可以通过closure访问myList和sat等变量—如果您在侦听器中执行所有这些操作,那么它就可以工作,但是如果您破坏了闭包(例如,如果这进入一个被另一个函数调用的函数中,事情就不起作用了myList将一直指向函数被定义时所选择的对象。在

在这种特殊情况下,您可能希望只对选择进行操作,而不是继承myList:

^{pr2}$

相关问题 更多 >