def doReplacement(replaceDB): notExistingSources = set() createdRefs = [] for tr, path in replaceDB.items(): if path in notExistingSources: continue if not os.path.exists(os.path.expandvars(path)): notExistingSources.add(path) log.logAppend('Path does not exists: {}. Replacement skipped.'.format(path)) continue refHandle = RefHandle() refHandle.createNew(path) createdRefs.append(refHandle) worldRP = m.xform(tr, q=True, rotatePivot=True, worldSpace=True) m.xform(refHandle.refLocator.transform, translation=worldRP, absolute=True, worldSpace=True) transformParent = getParent(tr) if transformParent: newRefLocTransform = getLongName(m.parent(refHandle.refLocator.transform, transformParent)[0]) refHandle.setRefLocator(TransformHandle(transform=newRefLocTransform)) rotation = m.xform(tr, q=True, rotation=True, objectSpace=True) scale = m.xform(tr, q=True, scale=True, objectSpace=True, relative=True) shear = m.xform(tr, q=True, shear=True, objectSpace=True, relative=True) m.xform(refHandle.refLocator.transform, rotation=rotation, scale=scale, shear=shear) if m.objExists(tr): m.delete(tr) return createdRefs
def saveRefsSources(replaceDB): processedPaths = set() for tr, path in replaceDB.items(): if not m.objExists(tr): continue expandedPath = expandPath(path) if expandedPath in processedPaths: continue shape = getShape(tr) childTransforms = getChildTransforms(tr) if not shape and not childTransforms: log.logAppend('Cannot save empty transform: {}. Source save skipped.'.format(tr)) continue else: oldParent = getParent(tr) tr2 = m.duplicate(tr, rr=True) if oldParent: newObject = m.parent(tr2, world=True)[0] else: newObject = tr2 worldRP = m.xform(newObject, q=True, rotatePivot=True, worldSpace=True) m.xform(newObject, relative=True, worldSpace=True, translation=[-x for x in worldRP]) m.xform(newObject, absolute=True, rotation=(0, 0, 0), scale=(1, 1, 1), shear=(0, 0, 0)) if shape: m.select(newObject, r=True) else: children = getChildTransforms(newObject) if children: newChildren = m.parent(children, world=True) m.select(newChildren, r=True) ext = os.path.splitext(path)[1] watch(path, 'saving path') makeWritable(expandPath(path)) m.file( path, exportSelected=True, force=True, typ='mayaBinary' if ext == '.mb' else 'mayaAscii', options='v=0;' ) if not shape: m.delete(newChildren) m.delete(newObject) processedPaths.add(expandedPath) return processedPaths
def retargetRefs(refHandles, targetDir): refFilenamesToRetarget = set([os.path.basename(rh.getRefFilename()).lower() for rh in refHandles]) duplicatesInTargetDir = set() targetsDb = {} for root, directories, files in os.walk(targetDir): for f in files: lowCaseFilename = f.lower() if lowCaseFilename not in refFilenamesToRetarget: continue fullPath = cleanupPath(os.path.join(root, f)) if lowCaseFilename in targetsDb: duplicatesInTargetDir.add(f) else: targetsDb[lowCaseFilename] = fullPath notFoundInTargetDir = set() rhToRetarget = [] for rh in refHandles: basename = os.path.basename(rh.getRefFilename()) basenameLower = basename.lower() if basenameLower in notFoundInTargetDir: continue if basenameLower not in targetsDb: notFoundInTargetDir.add(basename) continue rhToRetarget.append((rh, getRelativePath(targetsDb[basenameLower]))) saveSelection() activeRefHandles = [rh for rh, target in rhToRetarget if rh.active] # see activateRefs() for explanation why deactivate is not in activate() method for refHandle, target in rhToRetarget: refHandle.deactivate() for refHandle, target in rhToRetarget: refHandle.setRefFilename(target) for refHandle in activeRefHandles: refHandle.activate() maintenanceProcedure() restoreSelection() if duplicatesInTargetDir: log.logAppend('Duplicates found in target directory tree for following reference(s) (retargeted to first occurence):') log.logAppend(sorted(duplicatesInTargetDir)) if notFoundInTargetDir: log.logAppend('') log.logAppend('Following reference(s) was not found in target directory tree (leaved intact):') log.logAppend(sorted(notFoundInTargetDir)) log.logShow()
def replaceRefs(): nodes = m.ls(sl=True, l=True, typ='transform') if not nodes: return None, None if isBadSelection(nodes): log.logAppend("Invalid selection: you've selected both a parent and a child. Reference replacement aborted.") return None, None replaceDB = generateReplaceDB(nodes) shortcutsExists = any([p for p in replaceDB.values()]) anonymousExists = any([not p for p in replaceDB.values()]) shortcutsDuplicatesExists = False sDuplicates = set() for path in replaceDB.values(): p = os.path.expandvars(path).lower() if p in sDuplicates: shortcutsDuplicatesExists = True break sDuplicates.add(p) showDialog(shortcutsExists, shortcutsDuplicatesExists, anonymousExists) dlgResult = dlg.getDialogResult() if dlgResult == ReplaceDialog.RESULT_CANCEL: return None, None if anonymousExists: sourceFilename = browseForSource(0 if dlgResult == ReplaceDialog.RESULT_SAVE_REPLACE else 1) if not sourceFilename: return None, None else: for key in replaceDB: replaceDB[key] = sourceFilename savedSources = [] if dlgResult == ReplaceDialog.RESULT_SAVE_REPLACE: savedSources = saveRefsSources(replaceDB) createdRefs = doReplacement(replaceDB) return createdRefs, savedSources
def retargetRefs(refHandles, targetDir): refFilenamesToRetarget = set( [os.path.basename(rh.getRefFilename()).lower() for rh in refHandles]) duplicatesInTargetDir = set() targetsDb = {} for root, directories, files in os.walk(targetDir): for f in files: lowCaseFilename = f.lower() if lowCaseFilename not in refFilenamesToRetarget: continue fullPath = cleanupPath(os.path.join(root, f)) if lowCaseFilename in targetsDb: duplicatesInTargetDir.add(f) else: targetsDb[lowCaseFilename] = fullPath notFoundInTargetDir = set() rhToRetarget = [] for rh in refHandles: basename = os.path.basename(rh.getRefFilename()) basenameLower = basename.lower() if basenameLower in notFoundInTargetDir: continue if basenameLower not in targetsDb: notFoundInTargetDir.add(basename) continue rhToRetarget.append((rh, getRelativePath(targetsDb[basenameLower]))) saveSelection() activeRefHandles = [rh for rh, target in rhToRetarget if rh.active] # see activateRefs() for explanation why deactivate is not in activate() method for refHandle, target in rhToRetarget: refHandle.deactivate() for refHandle, target in rhToRetarget: refHandle.setRefFilename(target) for refHandle in activeRefHandles: refHandle.activate() maintenanceProcedure() restoreSelection() if duplicatesInTargetDir: log.logAppend( 'Duplicates found in target directory tree for following reference(s) (retargeted to first occurence):' ) log.logAppend(sorted(duplicatesInTargetDir)) if notFoundInTargetDir: log.logAppend('') log.logAppend( 'Following reference(s) was not found in target directory tree (leaved intact):' ) log.logAppend(sorted(notFoundInTargetDir)) log.logShow()