python maya:影响节点组

时间:2014-05-02 00:33:08

标签: python maya

我不太确定这个脚本中的问题在哪里。我做的是这.... 我有一个有2条曲线的场景。每条曲线都有三个与之相连的球体。

我选择曲线并运行脚本。它疯了,说我有匹配名字的对象?

Setup

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 :(得分:1)

如果没有看到您的场景或错误消息,我的假设是您有多个同名节点。由于Maya使用字符串,因此无法区分pSphere1和... pSphere1

listRelatives上的文档中,使用参数fullPath

  

返回完整路径名而不是对象名。

像这样:

    childrenTransforms = maya.cmds.listRelatives(children, type='transform', parent=True, fullPath=True)

假设错误发生在最后cmds.xform,这应该使这些转换明确无误(即|group1|pSphere1

答案 1 :(得分:0)

如果没有指定任何内容,

listRelatives将对所选对象起作用,因此你可以得到这样的子节点(带有完整路径):

descendants = cmds.listRelatives(ad=True, ni=True, f=True) # f=True = long paths

如果您尝试使用' dagNode'要在形状和几何形状之间进行过滤,它不会在work:dagNode将返回变换和形状。您可以使用' geometryShape'只获取形状:

descendant_shapes = cmds.listRelatives(ad=True, ni = True, f=True)

但在你的情况下也会返回curveShape。您可以使用以下方法过滤出曲线:

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])