Python Maya:影响节点组
我不太确定这个脚本的问题出在哪里。我做的事情是这样的……
我有一个场景,里面有两条曲线。每条曲线都有三个小球和它们关联在一起。
我选择了这些曲线,然后运行脚本。结果出错了,提示我有一些物体的名字是重复的?
import maya.cmds as cmds
selection = cmds.ls(selection=True, type='dagNode')
# groups of ![enter image description here][1]nodes to be exported out
nodeBundles = []
for n in selection:
# list the children nodes
children = cmds.listRelatives(n, allDescendents=True, noIntermediate=True, fullPath=True, type="dagNode", path=True)
# list the transform nodes to each child node
childrenTransforms = maya.cmds.listRelatives(children, type='transform', parent=True)
# append each set of children to a unique array and then append to main array
nodeBundles.append(childrenTransforms)
# select the transform nodes
# cmds.select(childrenTransforms)
# MXS cache out each bundle of nodes
for n in nodeBundles:
cmds.select(clear=True)
cmds.xform(n, absolute=True, t=[0,0,10])
print n
修复后的代码:
import maya.cmds as cmds
selection = cmds.ls(selection=True, type='dagNode')
# groups of ![enter image description here][1]nodes to be exported out
nodeBundles = []
for n in selection:
# list the children nodes
children = cmds.listRelatives(n, allDescendents=True, noIntermediate=True, fullPath=True, type="dagNode", path=True)
# list the transform nodes to each child node
# childrenTransforms = maya.cmds.listRelatives(children, type='transform', parent=True)
childrenTransforms = maya.cmds.listRelatives(children, type='transform', parent=True, fullPath=True)
# append each set of children to a unique array and then append to main array
nodeBundles.append(childrenTransforms)
# select the transform nodes
# cmds.select(childrenTransforms)
# MXS cache out each bundle of nodes
for n in nodeBundles:
cmds.select(clear=True)
cmds.xform(n, r=True, t=[0,0,10])
print n
通过在一个列表里面再加一个列表,我可以根据子物体的组来进行迭代。这种做法是正确的吗?
nodes = []
for item in cmds.ls(sl=True, type = 'transform'):
descendants = cmds.listRelatives(ad=True, ni=True, f=True) or []
# nodes += descendants # append the list, not insert it
nodes.append(descendants)
val = 1
for grp in nodes:
for n in grp:
cmds.select(clear=True)
offset = val * 10
print offset
cmds.xform(n, r=True, t=[0,0,offset])
val += 1
2 个回答
0
如果你不指定任何内容,listRelatives会对选中的对象起作用,这样你就可以像这样获取子节点(带完整路径):
descendants = cmds.listRelatives(ad=True, ni=True, f=True) # f=True = long paths
如果你试图用'dagNode'来区分形状和几何体,这样是行不通的:dagNode会返回变换和形状两者。你可以使用'geometryShape'来只获取形状:
descendant_shapes = cmds.listRelatives(ad=True, ni = True, f=True)
不过在你的情况下,这样做也会返回曲线形状。你可以通过以下方式过滤掉曲线:
descendants = cmds.ls(descendants, type= surfaceShape, l=True) # l=True keeps long paths
另外:在你的代码中,你把列表的列表传给了nodeBundles,Maya是不喜欢这样的。你应该通过一次添加一个项目来扁平化这个列表:
nodes = []
for item in cmds.ls(sl=True, type = 'transform'):
descendants = cmds.listRelatives(ad=True, ni=True, f=True) or []
nodes += descendants # append the list, not insert it
for n in nodes:
cmds.select(clear=True)
cmds.xform(n, r=True, t=[0,0,10])
1
在没有看到你的场景或错误信息的情况下,我猜测你可能有多个同名的节点。因为Maya使用字符串来区分对象,所以它无法区分 pSphere1
和... pSphere1
。
根据关于 listRelatives
的文档,你可以使用参数 fullPath
:
返回完整路径名,而不是对象名称。
像这样:
childrenTransforms = maya.cmds.listRelatives(children, type='transform', parent=True, fullPath=True)
假设错误出现在最后的 cmds.xform
,这样可以让这些变换变得明确(也就是说 |group1|pSphere1
)。