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