# -*- coding: utf-8 -*-
# ***************************************
#
# Author: Xinghai Zhao
# Created on: 2016.2.4 16:55:58
#
# ***************************************
'''
render: use foregroundRender after open maya app
'''
__version__ = '1.0.0'
import os,re,socket,logging
import sys
#add PYTHONPATH
if os.name == 'nt' and not 'Y:/PLE/studio/lib' in sys.path:
sys.path.append('Y:/PLE/studio/lib')
elif '/ibrix3/PLE/studio/lib' in sys.path:
sys.path.append('/ibrix3/PLE/studio/lib')
import cPickle as pickle
import bglobal,bjobsys
def render(scene_path,maya_render_tool_version = 2015,**kwargs):
# set PLATFORM path
scene_path = bglobal.toCurrent(scene_path)
_APPLAUNCHER = 'applauncher'
if bglobal.PLATFORM == 'win':
_APPLAUNCHER = 'S:/ple/studio/tool/gene/applauncher/bin/applauncher.bat'
# set mel path
_scriptpath = '/'.join([os.path.dirname(scene_path),'%s_%s.mel' % (socket.gethostname(),scene_path.replace('/','_').replace(':','_'))])
# set render frame tmp path
tmp_frame_path = '/'.join([os.path.dirname(scene_path),'%s_%s_%s' % (socket.gethostname(),scene_path.replace('/','_').replace(':','_'),'frame_tmp')])
# set open maya cmd
cmd = '%s maya --v %s' %(_APPLAUNCHER,str(maya_render_tool_version))
code = bjobsys.pathToJobCode(scene_path)
if code.get('show'):
cmd += ' --show=%s' % code.get('show')
if code.get('shot'):
cmd += ' --shot=%s' % code.get('shot')
if code.get('sequence'):
cmd += ' --sequence=%s' % code.get('sequence')
if code.get('ptype'):
cmd += ' --ptype %s' % code.get('ptype')
cmd += ' --appargs -file "%s" -script "%s"' %(scene_path,_scriptpath)
# set mel info
_cmd = ''
for _key in kwargs:
if kwargs.has_key(_key):
if _key in ['height','width','startFrame','endFrame','frameStep','pixelAspectRatio']:
_cmd += '%s=%s,' %(_key,str(kwargs.get(_key)))
else:
_cmd += '%s=\'%s\',' %(_key,kwargs.get(_key))
logging.info('run cmd: import foregroundRender.render;reload(foregroundRender.render);\
foregroundRender.render.tryRender(%s)' % _cmd)
_cmd = 'python "import foregroundRender.render;reload(foregroundRender.render);\
foregroundRender.render.tryRender(%s)";' % _cmd
# write mel
try:
_script = open(_scriptpath,'w')
_script.write(_cmd)
_script.close()
except:
raise Exception('can not write start mel in path: %s' % os.path.dirname(_scriptpath))
# write frame tmp
if not os.path.isfile(tmp_frame_path):
data_file = open(tmp_frame_path,'wb')
pickle.dump({},data_file)
data_file.close()
# run cmd
out = os.system(cmd)
print cmd
#if out != 0:
# raise Exception('run fail')
# get frame render tmp
if os.path.isfile(tmp_frame_path):
print tmp_frame_path
_info_file = open(tmp_frame_path,'rb')
_info = pickle.load(_info_file)
_info_file.close()
if isinstance(_info.get('frame'),(int,float)):
if _info.get('completed'):
_info['frame'] = _info.get('frame') + _info.get('frameStep')
if _info.get('frame') < _info.get('endFrame'):
if _info.get('try') > 2 and not _info.get('completed'):
raise Exception('frame: % render fail,' % _info.get('frame'))
else:
kwargs['startFrame'] = _info.get('frame')
kwargs['endFrame'] = _info.get('endFrame')
kwargs['frameStep'] = _info.get('frameStep')
render(scene_path = scene_path,
maya_render_tool_version = maya_render_tool_version,
**kwargs)
else:
raise Exception('open maya fail with scene :%s' % scene_path)
else:
logging.warning('can not find frame tmp file,please chick render output')
# remove mel,tmp
try:
if os.path.isfile(_scriptpath):
os.remove(_scriptpath)
except:
pass
try:
if os.path.isfile(tmp_frame_path):
os.remove(tmp_frame_path)
except:
pass
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option('-o', '--scene_path')
parser.add_option('-v', '--maya_render_tool_version',type='int',default=2015)
parser.add_option('-s', '--startFrame',type='int')
parser.add_option('-e', '--endFrame',type='int')
parser.add_option('-m', '--frameStep',type='float')
parser.add_option('-d', '--render_dir')
parser.add_option('-n', '--filename')
parser.add_option('-l', '--renderLayer')
parser.add_option('-c', '--camera')
parser.add_option('-t', '--height', type='int')
parser.add_option('-w', '--width', type='int')
parser.add_option('-p', '--pixelAspectRatio', type='float')
parser.add_option('-r', '--renderer')
option,args = parser.parse_args()
#option.__dict__
if option.__dict__.get('scene_path'):
render(**option.__dict__)
else:
for i in ['scene_path']:
if not option.__dict__.get(i):
raise Exception('can not find %s' % i)
08-16
08-16