SHARE
TWEET

Untitled

a guest Jul 7th, 2014 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import datetime
  2. import time
  3. import sys
  4. xsi = Application
  5.  
  6. def logMessage(lvl, msg):
  7.     if (len(lvl)==0):
  8.         xsi.LogMessage(datetime.datetime.now().strftime("' %H:%M.%S") + " rrSI      : " + str(msg))
  9.     else:
  10.         xsi.LogMessage(datetime.datetime.now().strftime("' %H:%M.%S") + " rrSI - " + str(lvl) + ": " + str(msg))
  11.    
  12. def logMessageDebug(lvl, msg):
  13.         if (False):
  14.                 logMessage(lvl, msg)
  15.  
  16.  
  17. def flushLog():
  18.     sys.stdout.flush()        
  19.     sys.stderr.flush()    
  20.  
  21. def applyRendererOptions_Arnold(inPass,inVerbose,inArnoldAc,inRenderThreads, inRenderDemo,inRenderDisplace):
  22.     if ((inRenderDemo!= None) and (len(str(inRenderDemo))>0)):
  23.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.abort_on_license_fail",False)
  24.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.skip_license_check",True)
  25.     else:
  26.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.abort_on_license_fail",True)
  27.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.skip_license_check",False)
  28.     if ((inArnoldAc!= None) and (len(str(inArnoldAc))>0)):
  29.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.AA_samples",inArnoldAc)
  30.     if ((inVerbose!= None) and (len(str(inVerbose))>0)):
  31.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.log_level",inVerbose)
  32.     if ((inRenderThreads!= None) and (len(str(inRenderThreads))>0)):
  33.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.autodetect_threads",False)
  34.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.threads",inRenderThreads)
  35.     if ((inRenderDisplace!= None) and (len(str(inRenderDisplace))>0)):
  36.         xsi.SetValue(str(inPass)+".Arnold_Render_Options.ignore_displacement",inRenderDisplace)
  37.  
  38.  
  39. def applyRendererOptions_Vray(inPass,inFName,inVerbose):
  40.     if ((inVerbose!= None) and (len(str(inVerbose))>0)):
  41.         xsi.SetValue(str(inPass)+".VRay_Options.sys_level",inVerbose)
  42.     filName=inFName
  43.     dirName=""
  44.     PD="/"
  45.     if (inFName.find("\\")>=0):
  46.         PD="\\"
  47.     pos= inFName.rfind(PD);
  48.     if (pos>0):
  49.         filName= inFName[pos+1:]
  50.         dirName= inFName[0:pos]
  51.     pos= filName.rfind(".");
  52.     if (pos>0):
  53.         filName= filName[0:pos]
  54.     xsi.SetValue(str(inPass)+".VRay_Options.out_save_in",dirName)
  55.     xsi.SetValue(str(inPass)+".VRay_Options.out_img_file_name",filName)
  56.     xsi.SetValue(str(inPass)+".VRay_Options.gsw_dont_rend_finimg",False)
  57.    
  58.    
  59.  
  60.  
  61. def applyRendererOptions_MRay(inPass,inVerbose,inMrayAAsmin,inMrayAAsmax, inRenderDisplace):
  62.     if ((inRenderDisplace!= None) and (len(str(inRenderDisplace))>0)):
  63.         xsi.SetValue(str(inPass)+".mentalray.EnableDisplacementShaders",inRenderDisplace)
  64.     if ((inVerbose!= None) and (len(str(inVerbose))>0)):
  65.         xsi.SetValue(str(inPass)+".mentalray.VerbosityLevel",inVerbose)
  66.     if ((inMrayAAsmin!= None) and (len(str(inMrayAAsmin))>0)):
  67.         xsi.SetValue(str(inPass)+".mentalray.SamplesMin",inMrayAAsmin)
  68.     if ((inMrayAAsmax!= None) and (len(str(inMrayAAsmax))>0)):
  69.         xsi.SetValue(str(inPass)+".mentalray.SamplesMax",inMrayAAsmax)
  70.  
  71.  
  72. kso_global_inPass = ""
  73.  
  74. def ksoRenderFrame(inFrStart,inFrEnd,inFrStep ):
  75.     logMessage("","Changing scene frame to frame #"+str(inFrStart)+" ...")
  76.     xsi.SetValue("PlayControl.Current", inFrStart, "")
  77.     xsi.SceneRefresh()
  78.     logMessage("","Starting to render frame #"+str(inFrStart)+" ...")
  79.     global kso_global_inPass
  80.     beforeFrame=datetime.datetime.now()
  81.     xsi.renderpass (kso_global_inPass ,"",  inFrStart,inFrEnd,inFrStep)
  82.     nrofFrames=((inFrEnd-inFrStart)/inFrStep+1)
  83.     afterFrame=datetime.datetime.now()
  84.     afterFrame=afterFrame-beforeFrame;
  85.     afterFrame=afterFrame/nrofFrames
  86.     logMessage("","Average time per frame: "+str(afterFrame)+"  h:m:s.ms")
  87.     logMessage("","rrKSO Frame(s) done #"+str(inFrEnd)+" ")
  88.     xsi.LogMessage("                                                            ")
  89.     xsi.LogMessage("                                                            ")
  90.     xsi.LogMessage("                                                            ")
  91.     flushLog()
  92.    
  93.  
  94.  
  95.  
  96. def rrKSOStartServer(inKSOPort):
  97.     import kso_tcp
  98.     kso_tcp.log_command="Application.LogMessage(' \\\'"
  99.     logMessage("","rrKSO startup...")
  100.     if ((inKSOPort== None) or (len(str(inKSOPort))<=0)):
  101.         inKSOPort=7774
  102.     HOST, PORT = "localhost", 7774
  103.     server = kso_tcp.rrKSOServer((HOST, PORT), kso_tcp.rrKSOTCPHandler)
  104.     logMessage("","rrKSO server started")
  105.     kso_tcp.rrKSONextCommand=""
  106.     while server.continueLoop:
  107.         try:
  108.             logMessageDebug("","rrKSO waiting for new command...")
  109.             server.handle_request()
  110.             time.sleep(1) # handle_request() seem to return before handle() completed execution
  111.         except Exception, e:
  112.             logMessage("ERR", e)
  113.             server.continueLoop= False;
  114.             import traceback
  115.             logMessage("ERR",traceback.format_exc())
  116.         xsi.LogMessage("                                                            ")
  117.         xsi.LogMessage("                                                            ")
  118.         xsi.LogMessage("                                                            ")
  119.         logMessage("","rrKSONextCommand '"+ kso_tcp.rrKSONextCommand+"'")  
  120.         flushLog()
  121.         if (len(kso_tcp.rrKSONextCommand)>0):
  122.             if ((kso_tcp.rrKSONextCommand=="ksoQuit()") or (kso_tcp.rrKSONextCommand=="ksoQuit()\n")):
  123.                 server.continueLoop=False
  124.                 kso_tcp.rrKSONextCommand=""
  125.             else:
  126.                 exec (kso_tcp.rrKSONextCommand)
  127.                 kso_tcp.rrKSONextCommand=""
  128.     logMessage("","rrKSO closed")
  129.  
  130. def render_KSO(inPass,inFrStart,inFrEnd,inFrStep,inKSOPort):
  131.     logMessage("","Importing kso...")
  132.     global kso_global_inPass
  133.     kso_global_inPass= inPass
  134.     rrKSOStartServer(inKSOPort)
  135.    
  136.    
  137. def render_default(inPass,inFrStart,inFrEnd,inFrStep):
  138.     xsi.renderpass (inPass ,"",  inFrStart,inFrEnd,inFrStep)
  139.  
  140.  
  141. def getParam(allArgList, argFindName):
  142.         argFindName=argFindName.lower()
  143.         for argComb in allArgList:
  144.                 arg= argComb.split(":")
  145.                 if (len(arg)<2):
  146.                     continue
  147.                 argName=arg[0].strip().lower()
  148.                 argValue=arg[1]
  149.                 if (len(arg)>2):  # e.g. C:\program files\...
  150.                         argValue+=":" + arg[2]
  151.                 if (len(arg)>3):
  152.                         argValue+=":" + arg[3]
  153.                 argValue=argValue.strip()
  154.                 if (argName==argFindName):
  155.                         logMessage("","Flag  "+argFindName.ljust(15)+": '"+argValue+"'");
  156.                         return argValue
  157.         return ""
  158.  
  159.  
  160.  
  161. def rrStart(argAll):
  162.     logMessage("","")
  163.     timeStart=datetime.datetime.now()
  164.     #argAll is almost a JSON string, but it is not to keep the commandline cleaner and less error prone
  165.     allArgList= argAll.split(",")  
  166.     logMessageDebug("DGB",allArgList)
  167.     inSName=getParam(allArgList,"SName")
  168.     inKSOMode=getParam(allArgList,"KSOMode")
  169.     inKSOPort=getParam(allArgList,"KSOPort")
  170.     inDb=getParam(allArgList,"Db")
  171.     inPass=getParam(allArgList,"Pass")
  172.     inSkipFrame=getParam(allArgList,"SkipFrame")
  173.     inFName=getParam(allArgList,"FName")
  174.     inFExt=getParam(allArgList,"FExt")
  175.     inFPadding=getParam(allArgList,"FPadding")
  176.     inFType=getParam(allArgList,"FType")
  177.     inFNameChannelAdd=getParam(allArgList,"FNameChannelAdd")
  178.     inFrStart=getParam(allArgList,"FrStart")
  179.     inFrEnd=getParam(allArgList,"FrEnd")
  180.     inFrStep=getParam(allArgList,"FrStep")
  181.     inFrOffset=getParam(allArgList,"FrOffset")
  182.     inCamera=getParam(allArgList,"Camera")
  183.     inVerbose=getParam(allArgList,"Verbose")
  184.     inMrayAAsmin=getParam(allArgList,"MrayAAsmin")
  185.     inMrayAAsmax=getParam(allArgList,"MrayAAsmax")
  186.     inArnoldAc=getParam(allArgList,"ArnoldAc")
  187.     inResX=getParam(allArgList,"ResX")
  188.     inResY=getParam(allArgList,"ResY")
  189.     inRegionX1=getParam(allArgList,"RegionX1")
  190.     inRegionX2=getParam(allArgList,"RegionX2")
  191.     inRegionY1=getParam(allArgList,"RegionY1")
  192.     inRegionY2=getParam(allArgList,"RegionY2")
  193.     inRenderThreads=getParam(allArgList,"RenderThreads")
  194.     inRenderDemo=getParam(allArgList,"RenderDemo")
  195.     inRenderDisplace=getParam(allArgList,"RenderDisplace")
  196.     inPyModPath=getParam(allArgList,"PyModPath")
  197.     #[RS] Get Redshift CUDA devices list from args
  198.     inRedshiftCudaDevices=getParam(allArgList,"RsCudaDevices")
  199.  
  200.     if ((inPyModPath!= None) and (len(inPyModPath)>0)):
  201.         import sys
  202.         sys.path.append(inPyModPath)
  203.        
  204.     if ((inDb!= None) and (len(inDb)>0)):
  205.         logMessage("","Set project to '" + inDb+"'...")
  206.         xsi.ActiveProject = inDb
  207.     logMessage("", "Active Project: " + str(xsi.ActiveProject2.Path))
  208.  
  209.     logMessage("", "Loading Scene '" + str(inSName)+"'...")
  210.     xsi.OpenScene(inSName, False, "")
  211.     if ((inDb!= None) and (len(inDb)>0)):
  212.         logMessage("","Set project to '" + inDb+"'...")
  213.         xsi.ActiveProject = inDb
  214.     logMessage("", "Active Project: " + str(xsi.ActiveProject2.Path))
  215.  
  216.  
  217.     if ((inPass!= None) and (len(inPass)>0)):
  218.         logMessage("","Set pass to '" + str(inPass) +"'")
  219.         inPass= "Passes." + str(inPass)
  220.         xsi.SetCurrentPass(str(inPass))
  221.     else:
  222.         inPass= xsi.GetCurrentPass()
  223.         logMessage("","Using current pass '" + inPass +"'")
  224.        
  225.     renderer=xsi.GetValue(str(inPass)+".renderer")
  226.     if (len(renderer)==0):
  227.         renderer = xsi.GetValue("Passes.RenderOptions.Renderer")    
  228.     logMessage("","renderer used: '" + renderer +"'")
  229.    
  230.     if ((inCamera!= None) and (len(inCamera)>0)):
  231.         logMessage("","Set renderer to " +inCamera)
  232.         xsi.SetValue(str(inPass)+".Camera",inCamera)
  233.        
  234.     if ((inSkipFrame!= None) and (len(str(inSkipFrame))>0)):
  235.         logMessage("","Set FrameSkipRendered to " +str(inSkipFrame))
  236.         xsi.SetValue(str(inPass)+".FrameSkipRendered",inSkipFrame)
  237.  
  238.     if ((inResX!= None) and (len(str(inResX))>0)):
  239.         logMessage("","Set width to " +str(inResX))
  240.         if (xsi.GetValue(str(inPass)+".ImageFormatOverride")):
  241.             xsi.SetValue(str(inPass)+".ImageWidth",inResX)
  242.         else:
  243.             xsi.SetValue("Passes.RenderOptions.ImageWidth",inResX)
  244.  
  245.     if ((inResY!= None) and (len(str(inResY))>0)):
  246.         logMessage("","Set height to " +str(inResY))
  247.         if (xsi.GetValue(str(inPass)+".ImageFormatOverride")):
  248.             xsi.SetValue(str(inPass)+".ImageLockAspectRatio",False)
  249.             xsi.SetValue(str(inPass)+".ImageHeight",inResY)
  250.         else:
  251.             xsi.SetValue("Passes.RenderOptions.ImageLockAspectRatio",False)
  252.             xsi.SetValue("Passes.RenderOptions.ImageHeight",inResY)
  253.  
  254.     if ((inRegionX1!= None) and (len(str(inRegionX1))>0)):
  255.         xsi.SetValue(str(inPass)+".CropWindowEnabled",True)
  256.         xsi.SetValue(str(inPass)+".SelectionTracking",False)
  257.         xsi.SetValue(str(inPass)+".CropWindowOffsetY",0)
  258.         xsi.SetValue(str(inPass)+".CropWindowHeight",9999)
  259.         xsi.SetValue(str(inPass)+".CropWindowOffsetX",inRegionX1)
  260.         xsi.SetValue(str(inPass)+".CropWindowWidth",(inRegionX2-inRegionX1+1))
  261.         if ((inRegionX1!= None) and (len(str(inRegionX1))>0)):
  262.             xsi.SetValue(str(inPass)+".CropWindowOffsetY",inRegionY1)
  263.             xsi.SetValue(str(inPass)+".CropWindowHeight",(inRegionY2-inRegionY1+1))
  264.             logMessage("","Set region to X:" +str(inRegionX1)+"-" +str(inRegionX2)+"  Y:" +str(inRegionX1)+"-" +str(inRegionX2))
  265.         else:
  266.             logMessage("","Set region to X:" +str(inRegionX1)+"-" +str(inRegionX2))
  267.        
  268.     if ((inFPadding!= None) and (len(str(inFPadding))>0)):
  269.         if (inFPadding<=4):
  270.             logMessage("","Set frame padding to " +str(inFPadding))
  271.             xsi.SetValue("Passes.RenderOptions.FramePadding",inFPadding)
  272.    
  273.     inFNameCopy=inFName
  274.  
  275.     if ((inFName!= None) and (len(str(inFName))>0)):
  276.         xsi.SetValue(str(inPass)+".Main.Enabled", True)
  277.         inFName=inFName.replace("<Layer>", "[Pass]")
  278.         inFName=inFName.replace("<Channel>", "[Framebuffer]")
  279.         inFName=inFName.replace("<Camera>", "[Camera]")
  280.         inFName=inFName.replace("<Camera_no.>", "[Camera]")
  281.         orgFileName=xsi.GetValue(str(inPass)+".Main.Filename")      
  282.         if ((inFPadding!= None) and (len(str(inFPadding))>0)):
  283.             if (inFPadding>4):
  284.                 for o in range(1, int(inFPadding)):
  285.                     inFName= inFName+"#"
  286.             else:
  287.                 inFName= inFName+"[Frame]"
  288.         else:
  289.             inFName= inFName + "[Frame "
  290.             if ((inFrOffset!= None) and (len(str(inFrOffset))>0) and (int(inFrOffset)!=0)):
  291.                 if (int(inFrOffset<0)):
  292.                     inFName= inFName + str(inFrOffset)
  293.                 else:
  294.                     inFName= inFName + "+" +str(inFrOffset)
  295.             inFName= inFName + "]"
  296.         if ((inFExt!= None) and (len(inFExt)>0)):
  297.             inFName= inFName + inFExt
  298.         logMessage("","Set Filename to " +inFName)
  299.         xsi.SetValue(str(inPass)+".Main.Filename",inFName)      
  300.         if (renderer=="Arnold Render" and (orgFileName.lower().find("framebuffer")<=0)):
  301.             oPass= xsi.GetValue(str(inPass))
  302.             for fbuffer in oPass.Framebuffers:
  303.                 if (orgFileName==xsi.GetValue(fbuffer.FileName)):
  304.                     logMessage("","Set Channel Filename to " +inFName)
  305.                     fbuffer.FileName=inFName
  306.  
  307.  
  308.     if ((inFNameChannelAdd!= None) and (len(str(inFNameChannelAdd))>0)):
  309.         oPass= xsi.GetValue(str(inPass))
  310.         for fbuffer in oPass.Framebuffers:
  311.             if (fbuffer.name!="Main"):
  312.                 chFname= xsi.GetValue(fbuffer.Filename)
  313.                 chDname=""
  314.                 spos=chFname.lower().find("[Frame")  
  315.                 if (spos>0):
  316.                     chDname =  chFname[spos:]
  317.                     chFname =  chFname[0:spos-1]
  318.                     chDname="."+chdname
  319.                 else:
  320.                     spos=chFname.lower().find("#")
  321.                     if (spos>0):
  322.                         chDname = chFname[spos:]
  323.                         chFname = chFname[0:spos-1]
  324.                         chDname="."+chDname
  325.                 if ((chFname[-1] != ".") and (chFname[-1] != "_")):
  326.                     chFname=chFname +"."
  327.                 chFname= chFname + inFNameChannelAdd + chDname
  328.                 xsi.SetValue(fbuffer.Filename, chFname)
  329.                 logMessage("","Set framebuffer output to " +inFName)
  330.  
  331.     #[RS] Select CUDA devices for Redshift
  332.     Application.Redshift_SelectCudaDevices(eval(inRedshiftCudaDevices))
  333.  
  334.     if (renderer=="Arnold Render"):
  335.         applyRendererOptions_Arnold(inPass,inVerbose,inArnoldAc,inRenderThreads, inRenderDemo,inRenderDisplace)
  336.     elif (renderer=="VRay"):
  337.         applyRendererOptions_Vray(inPass,inFName,inVerbose)
  338.     else:
  339.         applyRendererOptions_MRay(inPass,inVerbose,inMrayAAsmin,inMrayAAsmax, inRenderDisplace)
  340.  
  341.    
  342.     inFrStart=int(inFrStart)
  343.     inFrEnd=int(inFrEnd)
  344.     inFrStep=int(inFrStep)
  345.     logMessage("","Changing current frame to " +str(inFrStart))
  346.     xsi.SetValue("PlayControl.Current", inFrStart, "")
  347.     xsi.SceneRefresh()
  348.     logMessage("","Scene init done, starting to render... ")
  349.     timeEnd=datetime.datetime.now()
  350.     timeEnd=timeEnd - timeStart;
  351.     logMessage("","Scene load time: "+str(timeEnd)+"  h:m:s.ms")
  352.    
  353.  
  354.     if ((inKSOMode!= None) and (len(str(inKSOMode))>0)):
  355.         render_KSO(inPass,inFrStart,inFrEnd,inFrStep,inKSOPort)
  356.     else:
  357.         render_default(inPass,inFrStart,inFrEnd,inFrStep)
  358.        
  359.     logMessage("","Render done")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top