若要同时修改多个属性,可以使用“属性总表”(Attribute Spreadsheet)(“窗口 > 常规编辑器 > 属性总表”(Window > General Editors > Attribute Spreadsheet))。
或者,可以通过脚本执行此操作。
以下示例演示如何研究和使用 MEL 脚本以禁用场景中所有选定对象的可见性。
查询“可见性”(Visibility)属性的名称。
要修改属性,必须了解其准确的属性名称。
选择对象。在其形状节点(例如 pPlaneShape1)中,展开属性编辑器的“对象显示”(Object Display)部分,然后禁用“可见性”(Visibility)属性。
在脚本编辑器的顶部窗格中将显示与您的操作关联的 MEL 命令;例如:
setAttr "pPlaneShape1.visibility" 0;
如果在脚本中包括此命令,则它将执行的操作与您刚才通过用户界面执行的完全相同。
string $nodes[] = `ls -selection`;
由于可见性属性是形状节点的一部分,因此必须首先从变换节点获取形状节点。使用 listRelatives 命令。请参见如何获取(选定)形状节点的名称。
最终脚本如下所示:
{ //Lists the transform nodes of all selected objects string $nodes[] = `ls -selection`; for ($node in $nodes) { //Loop through each object and obtain its shape node string $shapes[] = `listRelatives -shapes $node`; //Set the visibility attribute of each shape node to 0 //The shape node is saved to the 1st (or 0th) element of the $shape array setAttr ($shapes[0] + ".visibility") (0); } }
如果计划经常运行自定义脚本,则可以将其保存到工具架。
在脚本编辑器中,选择要添加的脚本行,然后使用鼠标中键将选择拖动到工具架。
将显示一个对话框,要求您选择脚本的语言。
选择语言后,表示您的自定义脚本的图标现在已添加到“工具架”(Shelf)。
可以使用此编辑器自定义添加到“工具架”(Shelf)的新项目。例如,在“工具架”(Shelves)选项卡中自定义图标,或者在“命令”(Command)选项卡中修改命令。
除了通过 Maya 界面执行操作并检查在脚本编辑器的顶部窗格中输出的结果外,还可以通过以下方式查询属性的名称:
例如,在脚本编辑器中输入以下内容:
listAttr polySphere1
在顶部窗格中将输出以下结果:
// Result: message caching isHistoricallyInteresting nodeState binMembership frozen output axis axisX axisY axisZ paramWarn uvSetName radius subdivisionsAxis subdivisionsHeight texture createUVs //
polysphere1 是多边形的历史节点,listAttr 返回用于创建球体的属性;例如:细分、半径等。
以下命令将返回更多的属性,如 rotateX、scaleX 等:
listAttr pSphere1
pSphere1 是多边形的变换节点。这是包含用于变换对象的属性的节点。
如果 listAttr 返回太多属性,使文本变得难以阅读,也可以在节点文档中查找属性。
在技术文档的“节点”部分中,导航到“变换”(例如)以访问变换节点文档,或者导航到 geometryShape 以访问图形节点文档。在此页面上,将找到可以在脚本中修改的属性的名称。
node.attributeName 会直接添加到“脚本编辑器”(Script Editor)中。
在“大纲视图”(Outliner)中,启用“显示 > 形状”(Display > Shapes)和“显示 > 属性(通道)”(Display > Attributes (Channels))。选择属性时,将在“脚本编辑器”(Script Editor)中回显选择命令,以允许您找出属性名称。例如:
select -r pSphereShape1.castsShadows ;
右键单击“大纲视图”(Outliner),然后选择“属性顺序 > 按字母升序排列顺序”(Attribute Order > Alphabetical, ascend)以对属性进行排序,使其变得更易于查找。
若要获取和设置属性,请使用 getAttr 和 setAttr 命令。
您可能希望更改选定节点、特定类型的所有节点或场景中所有节点的属性。
使用 ls 命令及其关联的标志,可列出上述任何内容并将结果存储在数组中。
例如,列出所有节点:
ls
列出特定类型的节点,例如 polySphere:
ls -type polySphere
以下示例脚本列出了类型为 polySphere 的所有节点,然后将每个球体的半径加 2。
{ string $nodes[] = `ls -type polySphere`; for ($node in $nodes) { int $r = `getAttr ($node + ".radius")`; setAttr ($node + ".radius") ($r + 2); } }
列出场景中所有选定对象的变换节点:
ls -selection
以下示例脚本按 2 缩放选择列表中的所有对象。
{ string $nodes[] = `ls -selection`; for ($node in $nodes) { int $sX = `getAttr ($node + ".scaleX")`; int $sY = `getAttr ($node + ".scaleY")`; int $sZ = `getAttr ($node + ".scaleZ")`; setAttr ($node + ".scaleX") ($sX * 2); setAttr ($node + ".scaleY") ($sY * 2); setAttr ($node + ".scaleZ") ($sZ * 2); } }