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 replaceRefUI(): createdRefs, savedSources = replaceRefs() if (createdRefs is None) and (savedSources is None): log.logShow() return pathsToReload = set() rhToReload = [] for rh in getActiveRefHandles(): path = rh.getRefFilename() if path in pathsToReload: rhToReload.append(rh) continue if expandPath(path) in savedSources: pathsToReload.add(path) rhToReload.append(rh) for refHandle in rhToReload: refHandle.deactivate() maintenanceProcedure() rhToReload.extend(createdRefs) for refHandle in rhToReload: refHandle.activate() m.select([rh.refLocator.transform for rh in createdRefs], r=True) log.logShow()