Skip to content
Snippets Groups Projects
render.py 208 KiB
Newer Older
  • Learn to ignore specific revisions
  •             except RuntimeError:
                    print("***POV ERROR WHILE READING OUTPUT FILE***")
    
                # Not needed right now, might only be useful if we find a way to use temp raw output of
                # pov 3.7 (in which case it might go under _test_wait()).
    
                '''
                def update_image():
                    # possible the image wont load early on.
                    try:
                        lay.load_from_file(self._temp_file_out)
                        # XXX, tests for border render.
                        #lay.load_from_file(self._temp_file_out, xmin, ymin)
                        #lay.load_from_file(self._temp_file_out, xmin, ymin)
                    except RuntimeError:
                        pass
    
                # Update while POV-Ray renders
                while True:
                    # print("***POV RENDER LOOP***")
    
                    # test if POV-Ray exists
                    if self._process.poll() is not None:
                        print("***POV PROCESS FINISHED***")
                        update_image()
                        break
    
                    # user exit
                    if self.test_break():
                        try:
                            self._process.terminate()
                            print("***POV PROCESS INTERRUPTED***")
                        except OSError:
                            pass
    
                        break
    
                    # Would be nice to redirect the output
                    # stdout_value, stderr_value = self._process.communicate() # locks
    
                    # check if the file updated
                    new_size = os.path.getsize(self._temp_file_out)
    
                    if new_size != prev_size:
                        update_image()
                        prev_size = new_size
    
                    time.sleep(self.DELAY)
                '''
    
    Luca Bonavita's avatar
    Luca Bonavita committed
    
    
            print("***POV FILE FINISHED***")
    
            #print(filename_log) #bring the pov log to blender console with proper path?
            with open(self._temp_file_log) as f: # The with keyword automatically closes the file when you are done
    
                print(f.read())
    
    
            if scene.pov.tempfiles_enable or scene.pov.deletefiles_enable:
    
                self._cleanup()
    
    ##################################################################################
    #################################Operators########################################
    ##################################################################################
    
    class RenderPovTexturePreview(Operator):
        bl_idname = "tex.preview_update"
        bl_label = "Update preview"
        def execute(self, context):
            tex=bpy.context.object.active_material.active_texture #context.texture
            texPrevName=string_strip_hyphen(bpy.path.clean_name(tex.name))+"_prev"
    
            if not os.path.isdir(preview_dir):
                os.mkdir(preview_dir)
    
            iniPrevFile=os.path.join(preview_dir, "Preview.ini")
            inputPrevFile=os.path.join(preview_dir, "Preview.pov")
            outputPrevFile=os.path.join(preview_dir, texPrevName)
    
            ##################### ini ##########################################
            fileIni=open("%s"%iniPrevFile,"w")
            fileIni.write('Version=3.7\n')
            fileIni.write('Input_File_Name="%s"\n'%inputPrevFile)
            fileIni.write('Output_File_Name="%s.png"\n'%outputPrevFile)
    
            fileIni.write('Library_Path="%s"\n' % preview_dir)
    
            fileIni.write('Width=256\n')
            fileIni.write('Height=256\n')
            fileIni.write('Pause_When_Done=0\n')
            fileIni.write('Output_File_Type=N\n')
            fileIni.write('Output_Alpha=1\n')
            fileIni.write('Antialias=on\n')
            fileIni.write('Sampling_Method=2\n')
            fileIni.write('Antialias_Depth=3\n')
            fileIni.write('-d\n')
            fileIni.close()
            ##################### pov ##########################################
            filePov=open("%s"%inputPrevFile,"w")
            PATname = "PAT_"+string_strip_hyphen(bpy.path.clean_name(tex.name))
            filePov.write("#declare %s = \n"%PATname)
    
    Maurice Raybaud's avatar
    Maurice Raybaud committed
            filePov.write(shading.exportPattern(tex, string_strip_hyphen))
    
    
            filePov.write("#declare Plane =\n")
            filePov.write("mesh {\n")
            filePov.write("    triangle {<-2.021,-1.744,2.021>,<-2.021,-1.744,-2.021>,<2.021,-1.744,2.021>}\n")
            filePov.write("    triangle {<-2.021,-1.744,-2.021>,<2.021,-1.744,-2.021>,<2.021,-1.744,2.021>}\n")
            filePov.write("    texture{%s}\n"%PATname)
            filePov.write("}\n")
            filePov.write("object {Plane}\n")
            filePov.write("light_source {\n")
            filePov.write("    <0,4.38,-1.92e-07>\n")
            filePov.write("    color rgb<4, 4, 4>\n")
            filePov.write("    parallel\n")
            filePov.write("    point_at  <0, 0, -1>\n")
            filePov.write("}\n")
            filePov.write("camera {\n")
            filePov.write("    location  <0, 0, 0>\n")
            filePov.write("    look_at  <0, 0, -1>\n")
            filePov.write("    right <-1.0, 0, 0>\n")
            filePov.write("    up <0, 1, 0>\n")
            filePov.write("    angle  96.805211\n")
            filePov.write("    rotate  <-90.000003, -0.000000, 0.000000>\n")
            filePov.write("    translate <0.000000, 0.000000, 0.000000>\n")
            filePov.write("}\n")
            filePov.close()
            ##################### end write ##########################################
    
            pov_binary = PovrayRender._locate_binary()
    
            if sys.platform[:3] == "win":
    
                p1=subprocess.Popen(["%s"%pov_binary,"/EXIT","%s"%iniPrevFile],
                    stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    
                p1=subprocess.Popen(["%s"%pov_binary,"-d","%s"%iniPrevFile],
                    stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
    
            p1.wait()
    
            tex.use_nodes = True
            tree = tex.node_tree
            links = tree.links
            for n in tree.nodes:
                tree.nodes.remove(n)
    
            im = tree.nodes.new("TextureNodeImage")
    
            pathPrev="%s.png"%outputPrevFile
            im.image = bpy.data.images.load(pathPrev)
            name=pathPrev
            name=name.split("/")
            name=name[len(name)-1]
    
            im.name = name
    
            previewer = tree.nodes.new('TextureNodeOutput')
    
            previewer.label = "Preview"
            previewer.location = 400,400
            links.new(im.outputs[0],previewer.inputs[0])
            #tex.type="IMAGE" # makes clip extend possible
            #tex.extension="CLIP"