1 from __future__
import division
4 Custom file resolver derived from MPxFileResolver that handles
6 When this resolver is active, URI file paths using the 'http://<domain>/'
7 scheme will be processed using methods on this class.
8 Refer to MPxFileResolver for more information about custom file resolvers.
10 To use, make sure that adskHttpSchemeResolver.py is in
11 your MAYA_PLUG_IN_PATH then do the following:
15 maya.cmds.loadPlugin("adskHttpSchemeResolver.py")
16 # Once loaded, Maya will call the resolver methods in this plug-in when
17 # a URI file path is encountered during file resolution processing
18 # (file open, setAttr, etc.)
20 maya.cmds.unloadPlugin("adskHttpSchemeResolver")
21 # Maya will no longer have access to this file
22 # resolver to handle URI file paths using the 'http:///' scheme
26 from future
import standard_library
27 standard_library.install_aliases()
29 import maya.OpenMaya
as OpenMaya
30 import maya.OpenMayaMPx
as OpenMayaMPx
31 import maya.cmds
as cmds
32 import urllib.request, urllib.parse, urllib.error
37 kTempDir = cmds.internalVar(userTmpDir=
True)
40 kWantStatusOutput =
True;
42 def downloadProgress(data, dataSize, fileSize):
43 """ Display download progress - callback invoked by urllib.urlretrieve """
44 percent = 100.0*data*dataSize/fileSize
47 printStatus(
'Download progress: %.2f%%' % (percent))
50 """ Print status output for diagnostic purposes (when enabled) """
51 if (kWantStatusOutput):
52 sys.stderr.write(
'%s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
54 def printWarning(msg):
55 """ Print warning messages """
56 sys.stderr.write(
'Warning %s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
61 class adskHttpSchemeResolver(OpenMayaMPx.MPxFileResolver):
63 This custom plug-in resolver handles the 'http' uri scheme.
64 This resolver will copy the file from its 'http' location
65 using standard url library to a temporary location. This temporary location
66 of the file is the fully qualified resolved path.
67 It also implements a crude caching system.
70 kPluginURIScheme =
"http"
71 kPluginResolverName =
"adskHttpSchemeResolver"
74 OpenMayaMPx.MPxFileResolver.__init__(self)
77 return(self.kPluginURIScheme)
79 def resolveURI(self,URI,mode):
82 tempFile = kTempDir + URI.getFileName()
89 if mode & OpenMayaMPx.MPxFileResolver.kNone:
95 elif mode & OpenMayaMPx.MPxFileResolver.kInput:
96 if not os.path.exists(tempFile):
104 printStatus(
'Downloading URI: %s to location: %s' % (uri, tempFile))
106 data = urllib.request.urlretrieve(uri, tempFile, downloadProgress)
107 if os.path.exists(tempFile):
108 printStatus(
'Download complete')
110 printWarning(
'Download failed for URI: %s to location: %s'
114 printStatus(
'Download skipped, using cached version of URI: %s at location: %s'
120 printWarning(
'Unexpected resolve mode encountered: %s' % str(mode))
126 def performAfterSaveURI(self,URI,resolvedFullPath):
128 printStatus(
'Uploading local file %s to URI location %s'
129 % (resolvedFullPath, uri))
134 return OpenMayaMPx.asMPxPtr( adskHttpSchemeResolver() )
138 return 'adskHttpSchemeResolver'
143 def initializePlugin(plugin):
144 pluginFn = OpenMayaMPx.MFnPlugin(plugin)
146 pluginFn.registerURIFileResolver( adskHttpSchemeResolver.kPluginResolverName,
147 adskHttpSchemeResolver.kPluginURIScheme,
148 adskHttpSchemeResolver.theCreator )
150 sys.stderr.write(
"Failed to register custom resolver: %s for scheme: %s\n" %
151 (adskHttpSchemeResolver.kPluginResolverName,
152 adskHttpSchemeResolver.kPluginURIScheme ))
156 def uninitializePlugin(plugin):
157 pluginFn = OpenMayaMPx.MFnPlugin(plugin)
159 pluginFn.deregisterURIFileResolver(adskHttpSchemeResolver.kPluginResolverName)
162 "Failed to deregister custom file resolver: %s\n" %
163 adskHttpSchemeResolver.kPluginResolverName)