2 Custom file resolver derived from MPxFileResolver that handles 
    4 When this resolver is active, URI file paths using the 'http://<domain>/' 
    5 scheme will be processed using methods on this class. 
    6 Refer to MPxFileResolver for more information about custom file resolvers. 
    8 To use, make sure that adskHttpSchemeResolver.py is in 
    9 your MAYA_PLUG_IN_PATH then do the following: 
   13 maya.cmds.loadPlugin("adskHttpSchemeResolver.py") 
   14 # Once loaded, Maya will call the resolver methods in this plug-in when 
   15 # a URI file path is encountered during file resolution processing 
   16 # (file open, setAttr, etc.) 
   18 maya.cmds.unloadPlugin("adskHttpSchemeResolver") 
   19 # Maya will no longer have access to this file 
   20 # resolver to handle URI file paths using the 'http:///' scheme   
   25 import maya.OpenMaya 
as OpenMaya
 
   26 import maya.OpenMayaMPx 
as OpenMayaMPx
 
   27 import maya.cmds 
as cmds
 
   33 kTempDir = cmds.internalVar(userTmpDir=
True)
 
   36 kWantStatusOutput = 
True; 
 
   38 def downloadProgress(data, dataSize, fileSize):
 
   39     """ Display download progress - callback invoked by urllib.urlretrieve """ 
   40     percent = 100.0*data*dataSize/fileSize
 
   43     printStatus(
'Download progress: %.2f%%' % (percent))
 
   46     """ Print status output for diagnostic purposes (when enabled) """ 
   47     if (kWantStatusOutput):
 
   48         sys.stderr.write(
'%s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
 
   50 def printWarning(msg):
 
   51     """ Print warning messages """ 
   52     sys.stderr.write(
'Warning %s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
 
   57 class adskHttpSchemeResolver(OpenMayaMPx.MPxFileResolver):
 
   59     This custom plug-in resolver handles the 'http' uri scheme. 
   60     This resolver will copy the file from its 'http' location  
   61     using standard url library to a temporary location. This temporary location 
   62     of the file is the fully qualified resolved path. 
   63     It also implements a crude caching system. 
   66     kPluginURIScheme = 
"http" 
   67     kPluginResolverName = 
"adskHttpSchemeResolver" 
   70         OpenMayaMPx.MPxFileResolver.__init__(self)
 
   73         return(self.kPluginURIScheme)
 
   75     def resolveURI(self,URI,mode):
 
   78         tempFile = kTempDir + URI.getFileName()
 
   85         if mode & OpenMayaMPx.MPxFileResolver.kNone:
 
   91         elif mode & OpenMayaMPx.MPxFileResolver.kInput: 
 
   92             if not os.path.exists(tempFile):
 
  100                 printStatus(
'Downloading URI: %s to location: %s' % (uri, tempFile))
 
  102                 data = urllib.urlretrieve(uri, tempFile, downloadProgress)
 
  103                 if os.path.exists(tempFile):
 
  104                     printStatus(
'Download complete')
 
  106                     printWarning(
'Download failed for URI: %s to location: %s' 
  110                 printStatus(
'Download skipped, using cached version of URI: %s at location: %s' 
  116             printWarning(
'Unexpected resolve mode encountered: %s' % str(mode))
 
  122     def performAfterSaveURI(self,URI,resolvedFullPath):
 
  124         printStatus(
'Uploading local file %s to URI location %s' 
  125                         % (resolvedFullPath, uri))
 
  130         return OpenMayaMPx.asMPxPtr( adskHttpSchemeResolver() )
 
  134         return 'adskHttpSchemeResolver' 
  139 def initializePlugin(plugin):
 
  140     pluginFn = OpenMayaMPx.MFnPlugin(plugin)
 
  142         pluginFn.registerURIFileResolver( adskHttpSchemeResolver.kPluginResolverName,
 
  143                           adskHttpSchemeResolver.kPluginURIScheme,
 
  144                           adskHttpSchemeResolver.theCreator )
 
  146         sys.stderr.write( 
"Failed to register custom resolver: %s for scheme: %s\n" %
 
  147                   (adskHttpSchemeResolver.kPluginResolverName,
 
  148                    adskHttpSchemeResolver.kPluginURIScheme ))
 
  152 def uninitializePlugin(plugin):
 
  153     pluginFn = OpenMayaMPx.MFnPlugin(plugin)
 
  155         pluginFn.deregisterURIFileResolver(adskHttpSchemeResolver.kPluginResolverName)
 
  158             "Failed to deregister custom file resolver: %s\n" %
 
  159             adskHttpSchemeResolver.kPluginResolverName)