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

Is there a way to automate changing the unresolved name in Maya's reference editor?

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

我在一个项目中工作,需要将场景中引用对象的 unresolved name 更改为相对名称的形式。假设我在场景所在的同一文件夹中有一个球体,并且我将球体引用到场景中,然后当我打开我的引用编辑器时,球体的 'unresolved name' 可能类似于 path\to\the\sphere。我只需要将它更改为 sphere,现在我正在手动进行。有没有办法通过 Python 自动执行此过程?

我以前也处理过编辑贴图的路径,但是很简单,因为我可以使用fileTextureName属性来改变路径并直接设置路径。如果参考节点有这样的属性就好了

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

这是我前段时间使用的一种方法,用于将与我们的镜头一起移动到相对路径的 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

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

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)