"""
@package CurveReader.hrender.py
Renders the scene setup by the other objects
@author Coen Klosters
@version 1.0
"""

from subprocess import Popen
import subprocess
import os
import hou

class ArtRenderer(object):
    """
    Object that can be used to render the scene
    """
    
    def __init__(self, inRenderNode, inOutputPath, inUserName):
        """
        @param inRenderNode: The node used for rendering the scene
        """
        
        # the render node to generate the ifd from and start the render
        self.RenderNode = inRenderNode
        # the comp network that is created
        self.CompNetwork = None
        # the output path
        self.OutputPath = inOutputPath
        # the user name
        self.UserName = inUserName
        # the formatted user name
        self.FUserName = inUserName.replace(" ", "_").replace(".","").lower()
        # the error log
        self.Log = ""
        # the file name to which the comp image is saved
        self.CompImage = None
        
        
        
    def Render(self):
        """
        Renders out the ifd, starts mantra, waits for it to finish
        @return: If it succeeded or not
        """
        
        # render the ifd
        self.RenderNode.render()
        
        # find the ifd and see if it exists
        file = self.RenderNode.evalParm("soho_diskfile")
        if not os.path.exists(file):
            self.Log += "Unable to locate IFD file: %s" % file
            return False
        
        # now launch mantra and wait for it to finish rendering
        # this could be more verbose but fuck it
        proc = Popen(["mantra", "-f%s" % file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        proc.wait()
        
        if proc.returncode != 0:
            self.Log += "Mantra Render Failed..."
            return False
            
        # make sure we have the image on disk
        img = self.RenderNode.evalParm("vm_picture")
        if not os.path.exists(img):
            self.Log += "Unable to find output picture after render: %s" % img
            return False
            
        # set up a comp scene and spit it out
        self.CompNetwork = hou.node("/img").createNode('img', "comp_%s" % self.FUserName)
        file_reader = self.CompNetwork.createNode("file")
        file_reader.parm("filename1").set(img)
        
        # add the liminal comp node and set it up
        invert = self.CompNetwork.createNode("liminal_comp")
        invert.parm("user_name").set(self.UserName.upper())
        invert.setInput(0, file_reader)
        
        # create the rop
        self.CompImage = "%s/%s_art_comped.png" % (self.OutputPath, self.FUserName)
        rop = self.CompNetwork.createNode("rop_comp")
        rop.parm("copoutput").set(self.CompImage)
        rop.parm("scopeplanes").set("C")
        rop.parm("alpha").set("C")
        rop.parm("trange").set(0)
        
        # set the input
        rop.setInput(0, invert)
        
        self.CompNetwork.layoutChildren()
        self.CompNetwork.moveToGoodPosition()
        
        # render the final comped image
        rop.render()
        
        return True
        