在Maya python中将重复列表“L”重命名为“R”

时间:2017-07-25 07:30:48

标签: python python-2.7 maya

我想要做的是将控制器复制到另一侧,并将_L重命名/替换为_R。所以我只需选择控制器,它将创建一个组,然后另一个组在右侧镜像它并将其他组重命名为_R。然后将第一组改为世界。这就是我想做的一切。但我一直坚持重命名。我知道我必须按相反顺序对列表进行排序才能重命名,但每当我这样做时,Maya都说:

More than one object matches name

重复的对象具有不同的父名称和相同的子名称。请告诉我该怎么做以及我缺少什么。

import maya.cmds as cmds

list = cmds.ls(sl=1)
grp = cmds.group(em=1, name=("grp" + list[0]))

# creating constraint to match transform and deleting it
pc = cmds.pointConstraint(list, grp, o=[0,0,0], w=1)
oc = cmds.orientConstraint(list, grp, o=[0,0,0], w=1)
cmds.delete(pc, oc)

# parenting it to controller
cmds.parent(list, grp)

# creating new group to reverse it to another side
Newgrp = cmds.group(em=1)
cmds.parent(grp, Newgrp)
Reversedgrp = cmds.duplicate(Newgrp)

cmds.setAttr(Reversedgrp[0] +'.sx', -1)

selection = cmds.ls(Reversedgrp, long=1)
selection.sort(key=len, reverse=1)

2 个答案:

答案 0 :(得分:2)

在Maya中重命名非常烦人,因为名称是对象本身的唯一句柄。

通常的伎俩基本上是:

  1. 使用rr标记复制项目,因此您只能获得顶级节点
  2. listRelativesadfull标记一起使用,以获取重复的顶级节点的所有子节点,如|Parent|Child|Grandchild。在此表单中,名称上方的整个层次结构按顺序列出(您可以在对象上使用cmds.ls(l=True)获取此表单)
  3. 对该列表进行排序,然后将其反转。这将首先放置最长的路径名,因此您可以从叶节点开始向上工作
  4. 现在循环浏览项目并应用重命名模式
  5. 所以这样的事情,虽然你可能想用你控制的东西替换这里的选择:

    import maya.cmds as cmds
    dupes = cmds.duplicate(cmds.ls(sl=True), rr=True)   # duplicate, return only roots
    dupes += cmds.listRelatives(dupes, ad=True, f=True) # add children as long names
    longnames = cmds.ls(dupes, l=True)                 # make sure we have long name for root
    longnames.sort()     # usually these sort automatically, but's good to be safe
    for item in longnames[::-1]:   # this is shorthand for 'walk through the list backwards'
        shortname = item.rpartition("|")[-1]  # get the last bit of the name
        cmds.rename(item, shortname.replace("r","l"))   # at last, rename the item
    

答案 1 :(得分:1)

谢谢“正统”,这非常有用。但在排序,长名,短名称和.rpartition方面仍然有点困惑......但无论如何我终于创建了这个脚本。

import maya.cmds as cmds
_list = cmds.ls(sl=1)
grp = cmds.group(em=1, name=("grp_"+ _list[0]))

#creating constraint to match transfor and deleting it.
pc=cmds.pointConstraint( _list, grp, o=[0,0,0],w=1 )
oc=cmds.orientConstraint( _list, grp, o=[0,0,0],w=1 )
cmds.delete(pc,oc)


cmds.parent( _list, grp )
Newgrp=cmds.group(em=1)
cmds.parent(grp,Newgrp)

#duplicating new group and reversing it to negative side
dupes = cmds.duplicate(cmds.ls(Newgrp,s=0), rr=True)   # duplicate,     return only roots

cmds.setAttr( dupes[0] +'.sx', -1 )

#renaming
dupes += cmds.listRelatives(dupes, ad=True, f=True) # add children as long names
longnames = cmds.ls(dupes, l=True,s=0)                 # make sure we have long name for root
longnames.sort()     # usually these sort automatically, but's good to be safe
print longnames
for item in longnames[::-1]:   # this is shorthand for 'walk through the list backwards'
    shortname = item.rpartition("|")[-1]  # get the last bit of the name
    cmds.rename(item, shortname.replace("_L","_R"))   # at last, rename the item

#ungrouping back to world and delting unused nodes
cmds.parent( grp, world=True )
duplicatedGrp=cmds.listRelatives(dupes[0], c=True)
cmds.parent( duplicatedGrp, world=True )

cmds.delete(dupes[0],Newgrp)

任何人都可以使用此代码镜像控制器只需在重命名命令中更改“l”,“r”。

谢谢。