Maya Python:如何引用文件并立即 select 来自该引用文件的子项

Maya Python: how to reference a file and immediately select a child from that referenced file

我需要编写一个 python 自动化脚本,我需要执行以下操作:

例如:参考文件的结构是这样的:

我的尝试是这样的:

import maya.cmds as cmds

cmds.file("M:/.../rig.ma", r=True, ignoreVersion = True, namespace = "Rig")

以上代码引用了rig文件,我的问题是:如何在导入rig文件后立即select control_grp

大多数情况下,参考文件内容将作为命名空间的一部分出现(用名称和名称前面的冒号标识,例如“reference:pCube1”。如果您在控制命名空间时您在其中引用该文件,您将能够在命名空间内搜索而不是创建集合——但是,根据您或您的用户在引用对话框中设置选项的方式,您可能无法提前知道命名空间。

如果你有命名空间,那很简单:

rig_namespace = "rig" # or whatever you call it
control_grp = "control_grp")  # name of the object you want
cmds.select(rig_namespace + ":" + control_grp)

如果你不确定要搜索什么命名空间你可以在引用加载之前保存场景的内容到一个pythonset()然后再制作一个新的set()出来引用进来后的内容。使用 set difference() 函数,您可以从 post-load set 中减去 pre-load set,得到引用文件中的所有内容.然后,您可以使用 cmds.select 从文件中获取您要查找的项目。

正在获取导入的文件内容

import maya.cmds as cmds
before = set(cmds.ls(type='transform'))
cmds.file(r"path/to/file.ma", reference=True)
after = set(cmds.ls(type='transform'))
imported = after - before
print imported 

从导入的文件中获取控制装置

controls = set(cmds.ls("*control_grp*", type = transform)) # wildcards in 
case maya has added numbers or prefixes
imported_controls = controls & imported # this gets only controls just added
cmds.select(*imported_controls) # you need the asterisk to unpack the set