1

我才刚刚开始编码,所以我想我会尝试做一些简单的事情,但是,我无法从我的对象中选择对象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')

抛出的错误是

# Error: coercing to Unicode: need string or buffer, list found
# Traceback (most recent call last):
#   File "<maya console>", line 16, in attrLockT
# TypeError: coercing to Unicode: need string or buffer, list found 
4

2 回答 2

2

这行得通吗?

myList[0] + sat

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

If myListis just a list of stringthenmyList[0]将只是类型的一个元素,string它会产生错误。

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

 

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

如果您需要总结两个列表:

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

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

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

[ myList[0] + a for a in sat ]

这将导致['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)
于 2013-09-12T10:00:51.667 回答
1

两个问题:

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

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

其次,也许更重要的是,您的功能范围可能会遇到问题。在您输入的表单中,函数可以通过闭包访问 myList 和 sat 等变量- 如果您在侦听器中执行所有这些,它将起作用,但是如果您打破闭包(例如,如果到被另一个函数调用的函数中)事情不会起作用 - myList 将被卡在定义函数时选择的任何内容。

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

def lockTranslate(*args):
    for obj in cmds.ls(sl=True, type = 'transform'):
        for attr in ['.tx', '.ty', '.tz']: 
            cmds.setAttr(obj + "." + attr, l=True)
于 2013-09-13T20:56:04.687 回答