<commit_before>__author__ = '<NAME>'

from renderchan.module import RenderChanModule
import subprocess
import os, sys
import re
import random


class RenderChanBlenderModule(RenderChanModule):
    def __init__(self):
        RenderChanModule.__init__(self)
        self.conf['binary']="blender"
        self.conf["packetSize"]=40
        # Extra params
        self.extraParams["blender_cycles_samples"]=0
        self.extraParams["blender_prerender_count"]=0
        self.extraParams["single"]=None

    def getInputFormats(self):
        return ["blend"]

    def getOutputFormats(self):
        return ["png","exr","avi"]

    def analyze(self, filename):
        info={"dependencies":[]}

        script=os.path.join(os.path.dirname(__file__),"blender","analyze.py")
        dependencyPattern = re.compile("RenderChan dependency: (.*)$")
        startFramePattern = re.compile("RenderChan start: (.*)$")
        endFramePattern = re.compile("RenderChan end: (.*)$")

        env=os.environ.copy()
        env["PYTHONPATH"]=""
        commandline=[self.conf['binary'], "-b",filename, "-S","Scene", "-P",script]
        out = subprocess.Popen(commandline, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
        rc = None
        while rc is None:
            line = out.stdout.readline()
            if not line:
                break
            #print line,
            sys.stdout.flush()

            dep = dependencyPattern.search(line)
            if dep:
                info["dependencies"].append(dep.group(1).strip())

            start=startFramePattern.search(line)
            if start:
                info["startFrame"]=start.group(1).strip()

            end=endFramePattern.search(line)
            if end:
                info["endFrame"]=end.group(1).strip()


            rc = out.poll()

        out.communicate()
        rc = out.poll()

        if rc != 0:
            print '  Blender command failed...'

        return info

    def render(self, filename, outputPath, startFrame, endFrame, width, height, format, fps, audioRate, updateCompletion, extraParams={}):

        comp = 0.0
        updateCompletion(comp)

        totalFrames = endFrame - startFrame + 1
        frameCompletionPattern = re.compile("Saved:(\d+) Time: .* \(Saving: .*\)")
        frameCompletionPattern2 = re.compile("Append frame (\d+) Time: .* \(Saving: .*\)")
        frameNumberPattern = re.compile("Fra:(\d+) Mem:.*")

        random_string = "%08d" % (random.randint(0,99999999))
        renderscript="/tmp/renderchan"+os.path.basename(filename)+"-"+random_string+".py"
        script=open(os.path.join(os.path.dirname(__file__),"blender","render.py")).read()
        script=script.replace("params[UPDATE]","False")\
           .replace("params[WIDTH]", str(width))\
           .replace("params[HEIGHT]", str(height))\
           .replace("params[CAMERA]", '""')\
           .replace("params[AUDIOFILE]", '"'+os.path.splitext(outputPath)[0]+'.wav"')\
           .replace("params[FORMAT]", '"'+format+'"')\
           .replace("params[CYCLES_SAMPLES]",str(extraParams["blender_cycles_samples"]))\
           .replace("params[PRERENDER_COUNT]",str(extraParams["blender_prerender_count"]))
        f = open(renderscript,'w')
        f.write(script)
        f.close()

        if format in RenderChanModule.imageExtensions:
            if extraParams["projectVersion"]<1:
                outputPath=os.path.join(outputPath, "file")+".####"
            else:
                outputPath=os.path.join(outputPath, "file")+".#####"

        print '===================================================='
        print '  Output Path: %s' % outputPath
        print '===================================================='

        env=os.environ.copy()
        env["PYTHONPATH"]=""

        commandline=[self.conf['binary'], "-b",filename, "-S","Scene", "-P",renderscript, "-o",outputPath]
        if extraParams["single"] is None:
            commandline.append("-s")
            commandline.append(str(startFrame))<commit_msg>Fix Blender module for the case when SINGLE param is provided<commit_after>__author__ = '<NAME>'

from renderchan.module import RenderChanModule
import subprocess
import os, sys
import re
import random


class RenderChanBlenderModule(RenderChanModule):
    def __init__(self):
        RenderChanModule.__init__(self)
        self.conf['binary']="blender"
        self.conf["packetSize"]=40
        # Extra params
        self.extraParams["blender_cycles_samples"]=0
        self.extraParams["blender_prerender_count"]=0
        self.extraParams["single"]=None

    def getInputFormats(self):
        return ["blend"]

    def getOutputFormats(self):
        return ["png","exr","avi"]

    def analyze(self, filename):
        info={"dependencies":[]}

        script=os.path.join(os.path.dirname(__file__),"blender","analyze.py")
        dependencyPattern = re.compile("RenderChan dependency: (.*)$")
        startFramePattern = re.compile("RenderChan start: (.*)$")
        endFramePattern = re.compile("RenderChan end: (.*)$")

        env=os.environ.copy()
        env["PYTHONPATH"]=""
        commandline=[self.conf['binary'], "-b",filename, "-S","Scene", "-P",script]
        out = subprocess.Popen(commandline, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env)
        rc = None
        while rc is None:
            line = out.stdout.readline()
            if not line:
                break
            #print line,
            sys.stdout.flush()

            dep = dependencyPattern.search(line)
            if dep:
                info["dependencies"].append(dep.group(1).strip())

            start=startFramePattern.search(line)
            if start:
                info["startFrame"]=start.group(1).strip()

            end=endFramePattern.search(line)
            if end:
                info["endFrame"]=end.group(1).strip()


            rc = out.poll()

        out.communicate()
        rc = out.poll()

        if rc != 0:
            print '  Blender command failed...'

        return info

    def render(self, filename, outputPath, startFrame, endFrame, width, height, format, fps, audioRate, updateCompletion, extraParams={}):

        comp = 0.0
        updateCompletion(comp)

        totalFrames = endFrame - startFrame + 1
        frameCompletionPattern = re.compile("Saved:(\d+) Time: .* \(Saving: .*\)")
        frameCompletionPattern2 = re.compile("Append frame (\d+) Time: .* \(Saving: .*\)")
        frameNumberPattern = re.compile("Fra:(\d+) Mem:.*")

        random_string = "%08d" % (random.randint(0,99999999))
        renderscript="/tmp/renderchan"+os.path.basename(filename)+"-"+random_string+".py"
        script=open(os.path.join(os.path.dirname(__file__),"blender","render.py")).read()
        script=script.replace("params[UPDATE]","False")\
           .replace("params[WIDTH]", str(width))\
           .replace("params[HEIGHT]", str(height))\
           .replace("params[CAMERA]", '""')\
           .replace("params[AUDIOFILE]", '"'+os.path.splitext(outputPath)[0]+'.wav"')\
           .replace("params[FORMAT]", '"'+format+'"')\
           .replace("params[CYCLES_SAMPLES]",str(extraParams["blender_cycles_samples"]))\
           .replace("params[PRERENDER_COUNT]",str(extraParams["blender_prerender_count"]))
        f = open(renderscript,'w')
        f.write(script)
        f.close()

        if format in RenderChanModule.imageExtensions and extraParams["single"] is None:
            if extraParams["projectVersion"]<1:
                outputPath=os.path.join(outputPath, "file")+".####"
            else:
                outputPath=os.path.join(outputPath, "file")+".#####"

        print '===================================================='
        print '  Output Path: %s' % outputPath
        print '===================================================='

        env=os.environ.copy()
        env["PYTHONPATH"]=""

        commandline=[self.conf['binary'], "-b",filename, "-S","Scene", "-P",renderscript, "-o",outputPath]
        if extraParams["single"] is None:
            commandline.append("-s")
            commandline.append(str(startFrame))
{'fraction_non_alphanumeric': 0.18208588957055213, 'fraction_numerical': 0.006134969325153374, 'mean_word_length': 2.660080826223619, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5499483', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2319, 'n_words': 387}