Python API 2.0 Reference
adskHttpSchemeResolver.py
1 from __future__ import division
2 
3 """
4 Custom file resolver derived from MPxFileResolver that handles
5 the URI 'htpp' scheme.
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.
9 
10 To use, make sure that adskHttpSchemeResolver.py is in
11 your MAYA_PLUG_IN_PATH then do the following:
12 
13 # Load the plug-in
14 import maya.cmds
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.)
19 # Unload the plug-in
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
23 
24 """
25 
26 from future import standard_library
27 standard_library.install_aliases()
28 import sys
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
33 import os.path
34 
35 
36 #Specify the location for downloaded file
37 kTempDir = cmds.internalVar(userTmpDir=True)
38 
39 # Set status messages on or off
40 kWantStatusOutput = True;
41 
42 def downloadProgress(data, dataSize, fileSize):
43  """ Display download progress - callback invoked by urllib.urlretrieve """
44  percent = 100.0*data*dataSize/fileSize
45  if percent > 100:
46  percent = 100
47  printStatus('Download progress: %.2f%%' % (percent))
48 
49 def printStatus(msg):
50  """ Print status output for diagnostic purposes (when enabled) """
51  if (kWantStatusOutput):
52  sys.stderr.write('%s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
53 
54 def printWarning(msg):
55  """ Print warning messages """
56  sys.stderr.write('Warning %s: %s\n' % (adskHttpSchemeResolver.className(), msg) )
57 
58 
59 
60 # 'http' scheme resolver
61 class adskHttpSchemeResolver(OpenMayaMPx.MPxFileResolver):
62  """
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.
68  """
69 
70  kPluginURIScheme = "http"
71  kPluginResolverName = "adskHttpSchemeResolver"
72 
73  def __init__(self):
74  OpenMayaMPx.MPxFileResolver.__init__(self)
75 
76  def uriScheme(self):
77  return(self.kPluginURIScheme)
78 
79  def resolveURI(self,URI,mode):
80  # Determine temporary location that will be used for this file
81  uri = URI.asString()
82  tempFile = kTempDir + URI.getFileName()
83  result = u''
84 
85  # Check resolver mode, since not all modes require the file
86  # to be downloaded.
87 
88  # When mode is kNone, simply return the resolved path
89  if mode & OpenMayaMPx.MPxFileResolver.kNone:
90  result = tempFile;
91 
92  # When mode is kInput, Maya is expecting to use this
93  # file for input. Download the file to the temporary storage
94  # area if it hasn't already been done.
95  elif mode & OpenMayaMPx.MPxFileResolver.kInput:
96  if not os.path.exists(tempFile):
97  # If the file does not exist in the cache then go and
98  # download it. At this point we assume we have a well
99  # formed URI and that it exists on the web somewhere
100  # Any error here and the resolved file is simply not
101  # found. Would need to code for all the errors that
102  # could go wrong here in a production case (lost
103  # connections, server down, etc.)
104  printStatus('Downloading URI: %s to location: %s' % (uri, tempFile))
105 
106  data = urllib.request.urlretrieve(uri, tempFile, downloadProgress)
107  if os.path.exists(tempFile):
108  printStatus('Download complete')
109  else:
110  printWarning('Download failed for URI: %s to location: %s'
111  % (uri, tempFile))
112  result = tempFile
113  else:
114  printStatus('Download skipped, using cached version of URI: %s at location: %s'
115  % (uri, tempFile))
116  result = tempFile
117 
118  # Unexpected mode - simply return the resolved path
119  else:
120  printWarning('Unexpected resolve mode encountered: %s' % str(mode))
121  result = tempFile
122 
123  # Return the resolved path
124  return result
125 
126  def performAfterSaveURI(self,URI,resolvedFullPath):
127  uri = URI.asString()
128  printStatus('Uploading local file %s to URI location %s'
129  % (resolvedFullPath, uri))
130 
131  @staticmethod
132  # Creator for the proxy instance
133  def theCreator():
134  return OpenMayaMPx.asMPxPtr( adskHttpSchemeResolver() )
135 
136  @staticmethod
137  def className():
138  return 'adskHttpSchemeResolver'
139 
140 
141 
142 # Initialize the script plug-in
143 def initializePlugin(plugin):
144  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
145  try:
146  pluginFn.registerURIFileResolver( adskHttpSchemeResolver.kPluginResolverName,
147  adskHttpSchemeResolver.kPluginURIScheme,
148  adskHttpSchemeResolver.theCreator )
149  except:
150  sys.stderr.write( "Failed to register custom resolver: %s for scheme: %s\n" %
151  (adskHttpSchemeResolver.kPluginResolverName,
152  adskHttpSchemeResolver.kPluginURIScheme ))
153  raise
154 
155 # Uninitialize the script plug-in
156 def uninitializePlugin(plugin):
157  pluginFn = OpenMayaMPx.MFnPlugin(plugin)
158  try:
159  pluginFn.deregisterURIFileResolver(adskHttpSchemeResolver.kPluginResolverName)
160  except:
161  sys.stderr.write(
162  "Failed to deregister custom file resolver: %s\n" %
163  adskHttpSchemeResolver.kPluginResolverName)
164  raise
165 
166 #-
167 # ==========================================================================
168 # Copyright (C) 2012 Autodesk, Inc. and/or its licensors. All
169 # rights reserved.
170 #
171 # The coded instructions, statements, computer programs, and/or related
172 # material (collectively the "Data") in these files contain unpublished
173 # information proprietary to Autodesk, Inc. ("Autodesk") and/or its
174 # licensors, which is protected by U.S. and Canadian federal copyright
175 # law and by international treaties.
176 #
177 # The Data is provided for use exclusively by You. You have the right
178 # to use, modify, and incorporate this Data into other products for
179 # purposes authorized by the Autodesk software license agreement,
180 # without fee.
181 #
182 # The copyright notices in the Software and this entire statement,
183 # including the above license grant, this restriction and the
184 # following disclaimer, must be included in all copies of the
185 # Software, in whole or in part, and all derivative works of
186 # the Software, unless such copies or derivative works are solely
187 # in the form of machine-executable object code generated by a
188 # source language processor.
189 #
190 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
191 # AUTODESK DOES NOT MAKE AND HEREBY DISCLAIMS ANY EXPRESS OR IMPLIED
192 # WARRANTIES INCLUDING, BUT NOT LIMITED TO, THE WARRANTIES OF
193 # NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR
194 # PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE, OR
195 # TRADE PRACTICE. IN NO EVENT WILL AUTODESK AND/OR ITS LICENSORS
196 # BE LIABLE FOR ANY LOST REVENUES, DATA, OR PROFITS, OR SPECIAL,
197 # DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES, EVEN IF AUTODESK
198 # AND/OR ITS LICENSORS HAS BEEN ADVISED OF THE POSSIBILITY
199 # OR PROBABILITY OF SUCH DAMAGES.
200 #
201 # ==========================================================================
202 #+