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
27 import maya.OpenMaya
as OpenMaya
28 import maya.OpenMayaMPx
as OpenMayaMPx
29 import maya.cmds
as cmds
30 import urllib.request, urllib.parse, urllib.error
35 kTempDir = cmds.internalVar(userTmpDir=
True)
38 kWantStatusOutput =
True;
40 def downloadProgress(data, dataSize, fileSize):
41 """ Display download progress - callback invoked by urllib.urlretrieve """
42 percent = 100.0*data*dataSize/fileSize
45 printStatus(
'Download progress: %.2f%%' % (percent))
48 """ Print status output for diagnostic purposes (when enabled) """
49 if (kWantStatusOutput):
50 sys.stderr.write(
'%s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
52 def printWarning(msg):
53 """ Print warning messages """
54 sys.stderr.write(
'Warning %s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
59 class adskHttpSchemeResolver(OpenMayaMPx.MPxFileResolver):
61 This custom plug-in resolver handles the 'http' uri scheme.
62 This resolver will copy the file from its 'http' location
63 using standard url library to a temporary location. This temporary location
64 of the file is the fully qualified resolved path.
65 It also implements a crude caching system.
68 kPluginURIScheme =
"http"
69 kPluginResolverName =
"adskHttpSchemeResolver"
72 OpenMayaMPx.MPxFileResolver.__init__(self)
75 return(self.kPluginURIScheme)
77 def resolveURI(self,URI,mode):
80 tempFile = kTempDir + URI.getFileName()
87 if mode & OpenMayaMPx.MPxFileResolver.kNone:
93 elif mode & OpenMayaMPx.MPxFileResolver.kInput:
94 if not os.path.exists(tempFile):
102 printStatus(
'Downloading URI: %s to location: %s' % (uri, tempFile))
104 data = urllib.request.urlretrieve(uri, tempFile, downloadProgress)
105 if os.path.exists(tempFile):
106 printStatus(
'Download complete')
108 printWarning(
'Download failed for URI: %s to location: %s'
112 printStatus(
'Download skipped, using cached version of URI: %s at location: %s'
118 printWarning(
'Unexpected resolve mode encountered: %s' % str(mode))
124 def performAfterSaveURI(self,URI,resolvedFullPath):
126 printStatus(
'Uploading local file %s to URI location %s'
127 % (resolvedFullPath, uri))
132 return OpenMayaMPx.asMPxPtr( adskHttpSchemeResolver() )
136 return 'adskHttpSchemeResolver'
141 def initializePlugin(plugin):
142 pluginFn = OpenMayaMPx.MFnPlugin(plugin)
144 pluginFn.registerURIFileResolver( adskHttpSchemeResolver.kPluginResolverName,
145 adskHttpSchemeResolver.kPluginURIScheme,
146 adskHttpSchemeResolver.theCreator )
148 sys.stderr.write(
"Failed to register custom resolver: %s for scheme: %s\n" %
149 (adskHttpSchemeResolver.kPluginResolverName,
150 adskHttpSchemeResolver.kPluginURIScheme ))
154 def uninitializePlugin(plugin):
155 pluginFn = OpenMayaMPx.MFnPlugin(plugin)
157 pluginFn.deregisterURIFileResolver(adskHttpSchemeResolver.kPluginResolverName)
160 "Failed to deregister custom file resolver: %s\n" %
161 adskHttpSchemeResolver.kPluginResolverName)