def revealInFileManager( path ): """Reveals the specified folder or file in file manager.""" path = _Path( path ).normpath() #if len(path) > 4: #if the path is something other than the drive if path.endswith('/') or path.endswith('\\'): path = _Path(path[:-1]) # -- If the path doesn't exist, try walking up attempts = 2 while not path.exists(): path = path.parent attempts -= 1 if attempts == 0: assert False, 'Invalid path specified.' print '# Revealing: %s' % path if platform.system() == 'Darwin': subprocess.call(["open", "-R", path]) elif platform.system() == 'Windows': subprocess.call(["explorer", "/select,"+path]) else: raise NotImplementedError("Not yet implemented for current OS.")
def _updateFileHistory( control, optionName, validate=False, basename=False ): historySize = pm.optionVar.get( 'pathHistory_size', 6 ) pathStr = control.getText().replace( '\\', '/' ) if pathStr.endswith( '\\' ) or pathStr.endswith( '/' ): if len( pathStr ) > 4: pathStr = pathStr[:-1] path = _Path( pathStr ) if basename: path = path.basename() control.setFileName( path ) valid = True if validate: if basename: pm.mel.warning( "basename and validate args cannot be combine (%s)" % control, False ) elif path.isdir(): valid = True elif '.' in path.basename() and path.parent.isdir(): valid = True else: valid = False if valid: historyList = pm.optionVar.get( 'pathHistory_%s' % optionName, [] ) if isinstance( historyList, pm.OptionVarList ): historyList = list( historyList ) elif historyList: historyList = [historyList] if path in historyList: historyList.remove( path ) historyList.insert( 0, unicode( path ) ) pm.optionVar['pathHistory_%s' % optionName] = historyList[ 0:historySize ]
def _updateFileHistoryPopup( control, popup, optionName, basename=False ): historyList = pm.optionVar.get( 'pathHistory_%s' % optionName, [] ) if isinstance( historyList, pm.OptionVarList ): historyList = list( historyList ) elif historyList: historyList = [historyList] pm.popupMenu( popup, edit=True, deleteAllItems=True ) if historyList: for path in historyList: pm.setParent( popup, menu=True ) pm.menuItem ( label=path.replace( '/', '\\' ), command=pm.Callback( control.setText, ( path ) ) ) if not basename: pm.setParent( popup, menu=True ) pm.menuItem( divider=True ) pm.menuItem( label='go to folder', command=lambda *args: utils.revealInFileManager( _Path( control.getText() ) ) )
def safePath( file_path ): return _Path( _Path(file_path).realpath().replace('\\','/') )