Это метод, который я некоторое время назад использовал для повторения файлов ABC, которые перемещаются вместе с нашими снимками по относительным путям, поэтому они разрешаются даже при отправке на внутренний сервер ресурсов.
Ваш вопрос немного расплывчатый, но если вы посмотрите метод ниже и проигнорируете любые проверки, которые вам не нужны, и, очевидно, перепишете, чтобы иметь дело с .mb
или с тем, что вы используете, я думаю, вы можете заставить его делать то, что вам нужно.
Пожалуйста, игнорируйте используемые модули, которые вам не нужны, например, consoleLog
и config
.
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