From 074b802d456d4f6759aa2582891047c3c199753a Mon Sep 17 00:00:00 2001 From: Brett Williams Date: Mon, 22 May 2023 18:55:54 -0500 Subject: [PATCH] blender_worker refactoring --- lib/render_workers/blender_worker.py | 70 +++++++++++++++++----------- 1 file changed, 42 insertions(+), 28 deletions(-) diff --git a/lib/render_workers/blender_worker.py b/lib/render_workers/blender_worker.py index 73d62c8..bb5ff44 100644 --- a/lib/render_workers/blender_worker.py +++ b/lib/render_workers/blender_worker.py @@ -4,14 +4,39 @@ import re from .render_worker import * +class Blender(BaseRenderEngine): + + install_paths = ['/Applications/Blender.app/Contents/MacOS/Blender'] + supported_extensions = ['.blend'] + + @classmethod + def version(cls): + version = None + try: + render_path = cls.renderer_path() + if render_path: + ver_out = subprocess.check_output([render_path, '-v']) + version = ver_out.decode('utf-8').splitlines()[0].replace('Blender', '').strip() + except Exception as e: + logging.error(f'Failed to get Blender version: {e}') + return version + + @classmethod + def get_formats(cls): + format_string = cls.get_help().split('Format Options')[-1].split('Animation Playback Options')[0] + formats = re.findall(r"'([A-Z_0-9]+)'", format_string) + return formats + + @classmethod + def full_report(cls): + return {'version': cls.version(), + 'help_text': cls.get_help(), + 'formats': cls.get_formats()} + + class BlenderRenderWorker(BaseRenderWorker): - renderer = 'Blender' - render_engine = 'blender' - supported_extensions = ['.blend'] - install_paths = ['/Applications/Blender.app/Contents/MacOS/Blender'] - supported_export_formats = ['TGA', 'RAWTGA', 'JPEG', 'IRIS', 'IRIZ', 'AVIRAW', 'AVIJPEG', 'PNG', 'BMP', 'HDR', - 'TIFF', 'OPEN_EXR', 'OPEN_EXR_MULTILAYER', 'MPEG', 'CINEON', 'DPX', 'DDS', 'JP2'] + engine = Blender def __init__(self, input_path, output_path, args=None): super(BlenderRenderWorker, self).__init__(input_path=input_path, output_path=output_path, @@ -34,21 +59,9 @@ class BlenderRenderWorker(BaseRenderWorker): if self.render_all_frames else 1 self.current_frame = int(self.scene_info.get('frame_start', 0)) - @classmethod - def version(cls): - version = None - try: - render_path = cls.renderer_path() - if render_path: - ver_out = subprocess.check_output([render_path, '-v']) - version = ver_out.decode('utf-8').splitlines()[0].replace('Blender', '').strip() - except Exception as e: - logging.error(f'Failed to get {cls.renderer} version: {e}') - return version - def generate_worker_subprocess(self): - cmd = [self.renderer_path()] + cmd = [self.engine.renderer_path()] if self.args.get('background', True): # optionally run render not in background cmd.append('-b') cmd.append(self.input_path) @@ -131,7 +144,7 @@ class BlenderRenderWorker(BaseRenderWorker): def run_python_expression_in_blend(path, python_expression): if os.path.exists(path): try: - return subprocess.run([BlenderRenderWorker.renderer_path(), '-b', path, '--python-expr', python_expression], + return subprocess.run([Blender.renderer_path(), '-b', path, '--python-expr', python_expression], capture_output=True) except Exception as e: logger.warning(f"Error running python expression in blender: {e}") @@ -143,8 +156,7 @@ def run_python_expression_in_blend(path, python_expression): def run_python_script_in_blend(path, python_path): if os.path.exists(path) and os.path.exists(python_path): try: - return subprocess.run([BlenderRenderWorker.renderer_path(), '-b', path, '--python', python_path], - capture_output=True) + return subprocess.run([Blender.renderer_path(), '-b', path, '--python', python_path], capture_output=True) except Exception as e: logger.warning(f"Error running python expression in blender: {e}") pass @@ -201,12 +213,14 @@ def get_scene_info(path): if __name__ == '__main__': + print(Blender.full_report()) + # x = pack_blender_files('/Users/brett/Blender Files/temple_animatic.blend') # print(x) - logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG) - r = BlenderRenderWorker('/Users/brett/Blender Files/temple_animatic.blend', '/Users/brett/testing1234') - # r.engine = 'CYCLES' - r.start() - while r.is_running(): - time.sleep(1) + # logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logging.DEBUG) + # r = BlenderRenderWorker('/Users/brett/Blender Files/temple_animatic.blend', '/Users/brett/testing1234') + # # r.engine = 'CYCLES' + # r.start() + # while r.is_running(): + # time.sleep(1)