一半君的总结纸

听话只听一半君

#02 怎样才能知道当前场景文件里有哪些参考文件(references),怎样知道他们现在是loaded还是unloaded状态,如何load或者unload他们?

下面的命令可以返回当前场景文件中所有的参考文件(referenced files):

string $references[] = `file -q -r`;
references = pm.getReferences()
references = cmds.file(q=True,r=True)

下面的proc会返回现在已经是loaded状态的references(注:,在打开.ma文件以后, 那些以 file -r 开头的行里没有出现 -dr的 ,有-dr的就是deferred状态,你打开这个文件后在Reference Editor里可以看到他们是没有勾选的):

proc string[] referencesLoaded()
{
   string $referencesLoaded[];
   int $numLoaded = 0;

   string $refs[] = `file -q -r`;

   for ( $ref in $refs )
   {
      // If Reference is not deferred, then it is loaded
      if ( !`file -q -dr $ref` )
         $referencesLoaded[$numLoaded++] = $ref;
   }
   return $referencesLoaded;
}
def referencesLoaded():
    allReferences = pm.getReferences()
    # dict括号里面的部分是一个generator object, allReferences是一个dict,他的key是namespace,value是FileReference Object
    # 这是比较pythonic的写法,下面的python的示例中可以看到故意不pythonic的时候是什么样的
    return dict((k, v) for k, v in allReferences.iteritems() if v.isLoaded())

如果你运行上面的pymel代码,结果看起来可能是这样的

referencesLoaded()
# Result: {u'cube1': FileReference(u'C:/Users/oglop/Documents/maya/projects/default/scenes/cube1.ma', refnode=u'cube1RN'),
 u'cube2': FileReference(u'C:/Users/oglop/Documents/maya/projects/default/scenes/cube2.ma', refnode=u'cube2RN')} #
def referencesLoaded():
    allReferences = cmds.file(q=True,r=True)
    loadedRef = []
    for ref in allReferences:
        if not cmds.file(ref,q=True,dr=True):
            loadedRef.append(ref)
    return loadedRef

unload所有的references (这并不会把他们从Reference Editor里删掉):

proc unloadReferences()
{
   string $referencesLoaded[] = referencesLoaded();
   string $refNode;

   for ( $refFile in $referencesLoaded )
   {
      // Need to get node that identifies reference
      $refNode = `file -q -rfn $refFile`;

      // Now you can unload it
      file -ur $refNode $refFile;
   }
}
}
def unloadReferences():
    for ref in pm.getReferences().values():
        ref.unload()
def unloadReferences():
    allReferences = cmds.file(q=True,r=True)
    for refFile in allReferences:
        # 得到reference node的名字
        refNode = cmds.file(refFile,q=True,rfn=True)

        # 现在通过refNode来unload这个reference
        cmds.file(ur=refNode)

如果想确实从Reference Editor里删掉reference:

proc deleteLoadedReferences()
{
   string $referencesLoaded[] = referencesLoaded();

   for ( $refFile in $referencesLoaded )
   {
      file -rr $refFile;
   }
}
def deleteLoadedReferences():
    allReferences = pm.getReferences()
    # 此处没有用generator
    for k,v in allReferences.items():
        if v.isLoaded():
            v.remove()
def deleteLoadedReferences():
    loadedRef = referencesLoaded()
    for refFile in loadedRef:
        cmds.file(refFile,rr=True)

删除所有的reference:

proc deleteAllReferences()
{
   string $references = `file -q -r`;

   for ( $refFile in $references )
   {
      file -rr $refFile;
   }
}
}
def deleteAllReferences():
    allReferences = pm.getReferences()
    for ref in allReferences.values():
        ref.remove()
def deleteAllReferences():
    allReferences = cmds.file(q=True,r=True)
    for refFile in allReferences:
        cmds.file(refFile,rr=True)

加强版:

  • 当你reference同一个.ma文件两次的时候,用file -q -r查询的返回结果将会是这样
    // Result: /pathToRef/cube1.ma /pathToRef/cube1.ma{1} // 
  • 大部分file命令的argument在pymel里都变成了独立的function, 可以在doc的pymel.core.system module里找到他们
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: