有没有办法在Maya的参考编辑器中自动更改未解析的名称?

时间:2019-10-29 01:28:54

标签: python maya

我不确定这是不是一个初学者的问题,但是我一直在互联网上搜索,似乎没有什么可以解决我的问题。

我正在一个项目中,该项目需要将场景中引用对象的unresolved name更改为相对名称的形式。假设如果我的场景所在的文件夹中有一个球体,并且我将该球体引用到场景中,那么当我打开参考编辑器时,该球体的“未解析名称”可能类似于path\to\the\sphere。我只需要将其更改为sphere,现在我是手动进行的。有没有办法通过Python自动执行此过程?

我以前曾经处理过编辑纹理的路径,但是这很容易,因为我可以使用fileTextureName属性来更改路径并直接设置路径。如果参考节点具有这样的属性,那就太好了

我希望unresolved name的结果只能是从path\to\the\refref之类的

2 个答案:

答案 0 :(得分:2)

这是我前一段时间使用的一种方法,可以将与我们的镜头一起传播的ABC文件重定向到相对路径,因此即使将其推送到内部资产服务器也可以解析。

您的问题有点含糊,但是如果您浏览以下方法并忽略不需要的检查,并且显然重写以处理.mb或您使用的任何内容,我认为您可以让它做您需要的事情。

请忽略您不需要的正在使用的模块,例如consoleLogconfig

def repathAlembicsRelative():
    '''Repath all alembics in scene, residing in subfolder MISC, to a relative path'''

    out = classes.output()
    out.warnings = []

    consoleLog('Repathing Alembic caches (sims, etc. Not assets)...')

    localFile = cmds.file(query=True, l=True)[0]
    localFolder = os.path.dirname(localFile).replace('\\', '/')

    for obj in cmds.ls(type='reference'):
        try:
            filename = cmds.referenceQuery(obj, filename=True, withoutCopyNumber=True)
            filename = filename.replace('\\', '/').replace('//', '/')
            base = os.path.basename(filename)
            dir = os.path.dirname(filename)

            # Ref is NOT alembic
            if not filename.lower().endswith(config.EXTENSIONS[config.KEY_ALEMBIC]):
                consoleLog('Reference {} is NOT an alembic file. Skipping'.format(obj))
                continue

            # Ref is already on ASSETDIR
            if filename.startswith(config.ASSETDIR):
                consoleLog('Reference {} resides on ASSETDIR ({}). Skipping'.format(obj, config.ASSETDIR))
                continue

            # Ref is NOT in subfolder MISC
            miscPath = '{}/{}'.format(localFolder, config.KEY_MISC)
            miscPathFull = '{}/{}'.format(miscPath, base)
            if not filename == miscPathFull:
                consoleLog('Reference {} is local, but NOT in the MISC folder. Collecting file before repathing'.format(obj))

                try:
                    if not os.path.isdir(miscPath):
                        os.makedirs(miscPath)

                    shutil.copy(filename, miscPathFull)
                    consoleLog('Copied file {} to {}'.format(filename, miscPathFull))
                except Exception as ex:
                    warning = 'Unable to collect file {}: {}'.format(filename, ex)
                    consoleLog(warning)
                    out.warnings.append(warning)
                    continue

            # Failsafe
            if not os.path.isfile(miscPathFull):
                warning = 'File {} passed all checks, but somehow the file does not exist. This is not good.'.format(miscPathFull)
                consoleLog(warning)
                out.warnings.append(warning)
                continue

            # Skip repath if the UNRESOLVED path is already the same as what we're intending to set it to
            relPath = '{}/{}'.format(config.KEY_MISC, base)
            try:
                unresolved = cmds.referenceQuery(obj, filename=True, unresolvedName=True)
                if unresolved == relPath:
                    consoleLog('Unresolved filename for {} ({}) is already correct. Skipping'.format(obj, unresolved))
                    continue
            except Exception as ex:
                consoleLog('Unable to read unresolved filename for {}. Proceeding with pathmapping'.format(obj))

            # Passed all checks, repath to relative
            consoleLog('Repathing {} to {}'.format(filename, relPath))
            cmds.file(relPath, loadReference=obj, type='Alembic', options='v=0')

        except Exception as e:
            consoleLog('Unable to process reference node {}: {}'.format(obj, e))
            continue

    out.success = True # This method is always successful, but may produce warnings
    consoleLog('Done!')
    return out

答案 1 :(得分:0)

非常感谢@itypewithmyhands !!!我不知道我们可以使用maya file命令直接从相对路径加载(或重新加载)引用。因此,我想出了一个简单的解决方案(所有我之前在上面的代码中捕获的异常)

for i in sel:
    charRefPath = cmds.referenceQuery(i, filename = 1)

    #REPATH UNRESOLVED REFERENCE NAME WITH RELATIVE NAME
    refNode = cmds.referenceQuery(i, referenceNode = True)
    relPath = charRefPath.split('/')[-1]
    cmds.file(relPath, loadReference = refNode)