Rendering a Maya 2011 Scene with VRay


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ### Maya render execution
  2. ### by Robert Nederhorst
  3. ### http://www.throb.net
  4. ###
  5. ### This requires you use mayapy to exec.
  6. ### It will load the scene given and call the vrayUtils proc to export the frames to a VRay scene file.
  7. ### Then it will call the VRay executable to render the frames to the specified directory
  8. ###
  9. ### Usage :
  10. ### --scene file.mb --startframe 1 --endframe 100 --camera renderCam --layer renderLayer --output path/to/renderdir
  11. ### The layer is optional at this point. It will render the default render layer without a specified layer.
  12. ### It will name the resulting EXR the same name as the scene.
  13. ### EG. my_car.mb becomes my_car.0001.exr
  14.  
  15.  
  16. import sys, os, getopt, random, platform, datetime, re, subprocess
  17.  
  18. # We're going to use pymel.
  19. from pymel.core import *
  20. # this requires the vrayUtils.py I have written as well.
  21. from vrayUtils import *
  22. # this is for shot data insertion
  23. try:
  24. from getShotData import *
  25. shotData = True
  26. except:
  27. shotData = False
  28.  
  29. def fixWindows(inputPath):
  30. # Why does windows do stupid shit like put spaces in their dirnames?
  31. # Nobody likes it, windows. Just make that shit go away please.
  32. # Anyway, here is code to deal with that.
  33. for_cmd = 'for %I in ("' + inputPath + '") do echo %~sI'
  34. #print '%-16s%s' % ('for_cmd: ', for_cmd)
  35. p = os.popen(for_cmd)
  36. short_name = p.readlines()[-1] # last line from for command
  37. if p.close():
  38. print 'Error calling shell command "for"'
  39. else:
  40.  
  41. return short_name.strip()
  42.  
  43. # These are some simple error and info routines to make info nicer for debugging purposes
  44. def infoPrint(info):
  45. print ('\n[INFO]: %s\n') % (info)
  46. def errorPrint(error):
  47. print ('\n' + '='*20 + ' ERROR ' + '='*20 + '\n[ERROR] : ' + error + '\n' + '='*20 + ' ERROR ' + '='*20 + '\n')
  48.  
  49. def main():
  50.  
  51. #########################################################
  52. ##
  53. ## CHANGE PATHS HERE PLEASE
  54. ##
  55. #########################################################
  56.  
  57. mayaLocation = os.environ['MAYA_LOCATION']
  58. if mayaLocation == '':
  59. if sys.platform == 'win32':
  60. mayaLocation = fixWindows('C:/Program Files/Autodesk/Maya2011')
  61. if sys.platform == 'linux':
  62. mayaLocation = '/applications/Autodesk/maya'
  63. if sys.platform == 'darwin':
  64. print 'do mac code'
  65.  
  66. vrayLocation = mayaLocation + '/vray/bin/vray'
  67. if sys.platform == 'win32':
  68. vrayLocation = fixWindows(vrayLocation)
  69.  
  70.  
  71. windowsPath = 'z:/job'
  72. linuxPath = '/home/griffiti/shared/job'
  73. macPath = '/Volumes/job'
  74.  
  75. #########################################################
  76. ##
  77. ## END PATH CHANGES
  78. ##
  79. #########################################################
  80.  
  81. # Parse command line
  82. options = 'h'
  83. longOptions = ['scene=',
  84. 'startframe=',
  85. 'endframe=',
  86. 'camera=',
  87. 'layer=',
  88. 'usecameraname',
  89. 'output='
  90. ]
  91.  
  92. opts, pargs = getopt.getopt(sys.argv[1:], options, longOptions)
  93.  
  94. # Set defaults
  95. # We need to set this in case there is no render layer input so we don't get variable assignment errors
  96. renderLayer = ''
  97. useCameraName = False
  98.  
  99. # Extract command line options
  100. for opt in opts:
  101. if opt[0] == '--scene':
  102. sceneFile = opt[1]
  103. elif opt[0] == '--startframe':
  104. startFrame = opt[1]
  105. elif opt[0] == '--endframe':
  106. endFrame = opt[1]
  107. elif opt[0] == '--layer':
  108. renderLayer = opt[1]
  109. elif opt[0] == '--camera':
  110. renderCam = opt[1]
  111. elif opt[0] == '--usecameraname':
  112. useCameraName = True
  113. elif opt[0] == '--output':
  114. outputPath = opt[1]
  115.  
  116. # Check to make sure required options are there and if not we're going to have to tell the user
  117. missingOpts = []
  118. try:
  119. sceneFile
  120. except NameError:
  121. missingOpts.append('scene')
  122. try:
  123. startFrame
  124. except NameError:
  125. missingOpts.append('startframe')
  126. try:
  127. endFrame
  128. except NameError:
  129. missingOpts.append('endframe')
  130. try:
  131. renderCam
  132. except NameError:
  133. missingOpts.append('camera')
  134. try:
  135. outputPath
  136. except NameError:
  137. missingOpts.append('output')
  138.  
  139. if len(missingOpts) > 0 :
  140. usage(missingOpts)
  141.  
  142. # in case there is wierd shit with \ change em to /
  143. sceneFile = sceneFile.replace('\\','/')
  144. renderRange = '%s-%s' % (startFrame, endFrame)
  145. openFile (sceneFile, force=True)
  146.  
  147. # add job info here if the shot data module is loaded
  148. job = ''
  149. seq = ''
  150. shot = ''
  151. if shotData == True:
  152. try:
  153. job = getJob(sceneFile)
  154. seq = getSeq(sceneFile)
  155. shot = getShot(sceneFile)
  156. except:
  157. print 'foo'
  158.  
  159. # call the exporting command from vrUtils.py
  160. infoPrint('Calling the VRay export command')
  161. vrScenePath = exportVRScene(renderCam, renderRange, renderLayer, job, seq, shot)
  162.  
  163. if os.path.exists (vrScenePath) :
  164. infoPrint('VRay Scene Exists')
  165. else:
  166. errorPrint('VRay Scene (%s) does not exist. Exiting.' % (vrScenePath))
  167. #sys.exit(1)
  168. os._exit(1)
  169.  
  170. # let's setup the output information for stereo
  171. # this is going to require that you use 'left' and 'right' in the camera name.
  172. # is that so hard? jeez.
  173. if renderCam.lower().find('left') == 0:
  174. renderView = '_l'
  175. elif renderCam.lower().find('right') == 0:
  176. renderView = '_r'
  177. else :
  178. renderView = ''
  179. if renderLayer != '':
  180. renderLayer = '_%s' % (renderLayer)
  181.  
  182. # create a directory structure in the output area to help organize renders
  183. # it will output in the directory you specify and then add:
  184. # basepath/scene_file_name/render_layer_name/stereo_camera_view
  185. viewPath = '/%s' % (renderView)
  186. camName = ''
  187. camPath = ''
  188. if useCameraName == True:
  189. camName = renderCam.lower()
  190. if len(renderCam.lower().split(':')) > 1:
  191. camName = camName.split(':')[1]
  192. camPath = '/%s' % (camName.replace('shape',''))
  193. camName = '_%s' % (camName.replace('shape',''))
  194.  
  195. outputPath = '%s/%s%s/%s%s' % (outputPath, os.path.basename(sceneFile).split('.')[0].lower(),camPath, renderLayer.lower()[1:], viewPath)
  196. infoPrint ('Output Path : %s' % (outputPath))
  197.  
  198. # you can modify this to how you want the exr named.
  199. # currently it names it as the scene_renderlayer and then _l or _r for stereo work
  200. exrFile = '%s%s%s%s.#.exr' % (os.path.basename(sceneFile).split('.')[0].lower(), camName, renderLayer.lower(), renderView)
  201. infoPrint ('Rendering to file: %s' % (exrFile))
  202.  
  203.  
  204. # check on existing directory and create if it does not exist
  205. if os.path.exists(outputPath) == False:
  206. os.makedirs(outputPath)
  207. if os.path.exists(outputPath) == True:
  208. infoPrint('Created output directory : %s' % (outputPath))
  209. else:
  210. errorPrint('Could not create dir : %s' % (outputPath))
  211. #sys.exit(1)
  212. os._exit(1)
  213.  
  214.  
  215. # standard VRay exec arguments
  216. vrArgs = '-display=0 -autoClose=1 -sceneFile="%s" -imgFile="%s/%s" -frames="%s"' % (vrScenePath, outputPath, exrFile, renderRange)
  217.  
  218. # just for giggles let's make sure there are no extra issues on the command line here
  219. execCmd = vrayLocation.strip() + ' ' + vrArgs
  220. subprocess.call(execCmd, shell = True)
  221.  
  222. # check for existence of the rendered files
  223. infoPrint('Checking on existence of rendered frames now')
  224. renderSuccess = True
  225. for frame in range (int(renderRange.split('-')[0]),int(renderRange.split('-')[1])+1):
  226. fframe = '%04d' % (frame)
  227. fullPath = '%s/%s' % (outputPath, exrFile.replace('#',fframe))
  228. if os.path.exists (fullPath) :
  229. infoPrint('%s found.\n' % (fullPath))
  230. else :
  231. errorPrint('%s NOT found.\n' % (fullPath))
  232. renderSuccess = False
  233. #sys.exit(1)
  234. os._exit(1)
  235. if renderSuccess == False:
  236. errorPrint('You have errors in the render.\n')
  237. #sys.exit(1)
  238. os._exit(1)
  239. else :
  240. infoPrint('Render Successful.')
  241.  
  242. def usage (opts):
  243. for opt in opts:
  244. print 'Option missing: %s\n' % opt
  245. curScript = sys.argv[0]
  246. print '=' * 20, 'Usage','=' * 20, '\n'
  247. print '%s --scene file.mb --startframe 1 --endframe 100 --camera renderCam\n --layer renderLayer --output path/to/renderdir --usecameraname' % (curScript)
  248. print '\n'
  249. #sys.exit(1)
  250. os._exit(1)
  251.  
  252. if __name__ == '__main__':
  253. main()
  254. #sys.exit(0)
  255. os._exit(0)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.