获取和设定多值属性

 
 
 

某些节点具有包含多个值的属性。Maya 存储值的方式并不与 MEL 数据类型相对应。

例如:

在联机节点文档中,会将这些属性的类型作为类似于 3float 的内容列出,指示该属性存储 3 个浮点值。

获取多值

与从数组获取单个元素的方式类似,可以使用索引从多值属性获取单个值:

getAttr nurbsSphere2.translate[1];
getAttr nurbsSphereShape2.cv[0][2];

也可以将多个值指定给数组:

// Put the three values in the translate
// attribute in an array:
float $trans[] = getAttr("nurbsSphere2.translate");
// Result: -2.76977 0 0 //
// Put the X, Y, and Z positions of cv #1 of curveShape1
// in an array:
float $cvXYZ[] = getAttr("curveShape1.cv[1]");
// Result: -2.367282 0 2.491355 //
// Put the X, Y, and Z positions of cv U=1,V=2
// of nurbsSphereShape2 in an array:
float $cvXYZ_2[] = getAttr("nurbsSphereShape2.cv[1][2]");
// Result: -2.367282 0 2.491355 //

设定多值

如上所示,虽然可以作为一个数组一次获取所有多值,但是反过来则行不通:不能将数组指定给多值属性:

setAttr("nurbsSphere2.translate",{1.0, 1.2, 3.4});
// ERROR

可以改为将多个参数传递给 setAttr:

setAttr("nurbsSphere1.translate", 1.0, 1.2, 3.4);
setAttr("curveShape1.cv[1]", 1.0, 1.2, 3.4);
setAttr("nurbsSphereShape1.cv[1][2]", 5.5, -2.3, 0);

若要仅更改一个多值的一部分,可以将多值放到一个数组中,然后修改数组的内容,并将这些内容放回到多值属性中:

// Change only the second part of the translate multi-value
float $trans = getAttr("nurbsSphere.translate");
$trans[1] += 2;
setAttr("nurbsSphere.translate",$trans[0],$trans[1],$trans[2]);

但是,实际不会出现这种情形,因为多值属性具有单一等效项(如 translate 以及 translateXtranslateYtranslateZ)以及一个简单的等效命令,在这种情况下:

move -relative 0 2 0 "nurbsSphere1";

通配符

可以在多值属性上的索引中使用字符串“*”来代表每个值。

例如:

// Get the translation of every CV along U=1
getAttr nurbsPlaneShape1.cv[1]["*"];
// Result: 0 0 0 0 0.456295 0 0 0.456295 0 0 0 0 //
// Get the translation of every CV.
getAttr nurbsPlaneShape1.cv["*"]["*"];
// Result: 0 -0.520965 0 0 0 0 0 0 0 0 -0.520965 0 0 0 0 0 0.456295 0 0 0.456295 0 0 0 0 0 0 0 0 0.456295 0 0 0.456295 0 0 0 0 0 -0.520965 0 0 0 0 0 0 0 0 0.702647 0 //
// Select every CV of a surface:
select -r nurbsSphere1.cv["*"]["*"];
// Select every CV of a curve:
select -r curve1.cv["*"] ;