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 loadFromRefLocatorShape(self, refLocatorShape): self.refFilename = cleanupPath(m.getAttr('{}.{}'.format(refLocatorShape, ATTR_REF_FILENAME))) self.idString = self.generateIdString(self.refFilename) self.refShortName = self.generateShortName(self.refFilename) self.refLocator = TransformHandle(shape=refLocatorShape) self.setAnnotation(self.refShortName) self.refNode = self.idString + REF_NODE_SUFFIX self.instanceSource = '|{}|{}'.format(INSTANCES_SOURCE_GROUP, self.idString) self.importSource = '|{}|{}'.format(IMPORT_SOURCE_GROUP, self.idString) self.active = self.getActiveStateFromMaya()
def onAddClicked(self): # noinspection PyCallByClass newRootDir = cleanupPath( QtGui.QFileDialog.getExistingDirectory(self, 'References Root Dir', self.getLastBrowsedDir())) if not newRootDir: return if newRootDir not in self.roots: self.roots[newRootDir] = False self.fillRootsList()
def browseForSource(fileMode): globalPrefsHandler.loadPrefs() lastBrowsed = globalPrefsHandler.getValue(globalPrefsHandler.KEY_LAST_BROWSED_SOURCE) or '' filename = m.fileDialog2( dialogStyle=1, caption='Choose Reference Scene', fileFilter='Maya Scenes (*.mb; *.ma);;Maya Binary (*.mb);;Maya ASCII (*.ma);;All Files (*.*)', fileMode=fileMode, returnFilter=False, startingDirectory=lastBrowsed ) if not filename: return None filename = cleanupPath(filename[0]) globalPrefsHandler.setValue(globalPrefsHandler.KEY_LAST_BROWSED_SOURCE, cleanupPath(os.path.dirname(filename))) globalPrefsHandler.savePrefs() return getRelativePath(filename)
def onAddClicked(self): # noinspection PyCallByClass newRootDir = cleanupPath(QtWidgets.QFileDialog.getExistingDirectory( self, 'References Root Dir', self.getLastBrowsedDir() )) if not newRootDir: return if newRootDir not in self.roots: self.roots[newRootDir] = False self.fillRootsList()
def browseDir(): globalPrefsHandler.loadPrefs() lastBrowsed = globalPrefsHandler.getValue(globalPrefsHandler.KEY_LAST_BROWSED_DIR) or '' directory = m.fileDialog2( dialogStyle=1, caption='Choose Directory', # fileFilter='Maya Scenes (*.mb; *.ma);;Maya Binary (*.mb);;Maya ASCII (*.ma);;All Files (*.*)', fileMode=3, returnFilter=False, startingDirectory=lastBrowsed ) if not directory: return None directory = cleanupPath(directory[0]) globalPrefsHandler.setValue(globalPrefsHandler.KEY_LAST_BROWSED_DIR, cleanupPath(os.path.dirname(directory))) globalPrefsHandler.savePrefs() return directory
def browseDir(): globalPrefsHandler.loadPrefs() lastBrowsed = globalPrefsHandler.getValue( globalPrefsHandler.KEY_LAST_BROWSED_DIR) or '' directory = m.fileDialog2( dialogStyle=1, caption='Choose Directory', # fileFilter='Maya Scenes (*.mb; *.ma);;Maya Binary (*.mb);;Maya ASCII (*.ma);;All Files (*.*)', fileMode=3, returnFilter=False, startingDirectory=lastBrowsed) if not directory: return None directory = cleanupPath(directory[0]) globalPrefsHandler.setValue(globalPrefsHandler.KEY_LAST_BROWSED_DIR, cleanupPath(os.path.dirname(directory))) globalPrefsHandler.savePrefs() return directory
def getRelativePath(path): pathWorking = cleanupPath(path) refRootValueLower = getRefRootValue().lower() if not refRootValueLower: return pathWorking if isPathRelative(pathWorking): return pathWorking pathLower = pathWorking.lower() if pathLower.startswith(refRootValueLower): return REF_ROOT_VAR_NAME_P + pathWorking[len(refRootValueLower):] return path
def getRefRootValue(): return cleanupPath(os.environ.get(REF_ROOT_VAR_NAME, ''))
def getRefFilename(self): return cleanupPath(self.refFilename)
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()