pymel 对象上的 addAttr 方法

addAttr method on pymel objects

我似乎无法正确使用 addAttr 方法。我使用的参数与我从 pymel.core 调用时使用的参数相同,但结果不同。

我正在尝试添加自定义消息属性,以便以后可以轻松搜索某些类型的对象。当我从 pymel.core 开始并包含相同的对象引用作为参数时,它工作正常。

#get object reference
test_object = pm.ls(sl=1)[0]

#this one spits out an error
test_object.addAttr(longName = 'custom', attributeType = 'message')

#this one works fine
pm.addAttr(test_object, longName = 'custom', attributeType = 'message')

我一直收到这个错误 错误:类型错误:文件第 1 行:addAttr() 正好接受 2 个参数(给定 1 个) 当我以这种方式使用它时,它在寻找什么额外的参数?我显然遗漏了一些关于方法如何工作的明显信息,但我无法弄清楚。

来自 cgsociety 线程

pCube.addAttr('timeBasedAttr', keyable=True, attributeType='float', min=0.0, max=1.0)

你应该写:

test_object.addAttr('custom', attributeType = 'message')

我试过了,没有输出错误。

在 Maya PyMel 中为 DG 节点公开的 addAttr 方法具有以下签名。

addAttr(attr, **kwargs)

这里attr是表示属性名的位置参数。 kwargs 可以与 pm.addAttr() 方法中使用的所有其他相关标志一起提供。所以你必须将属性名称作为第一个参数传递。

node.addAttr('custom', attributeType='message')

希望这会有所帮助。