From 69e557dfdb003c22586fb3733eb61b2b7f6ba537 Mon Sep 17 00:00:00 2001 From: Brett Williams Date: Mon, 22 May 2023 19:19:04 -0500 Subject: [PATCH] Consolidate Blender methods --- lib/render_workers/blender_worker.py | 51 ++++++++++++++-------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/render_workers/blender_worker.py b/lib/render_workers/blender_worker.py index bb5ff44..2cf8ade 100644 --- a/lib/render_workers/blender_worker.py +++ b/lib/render_workers/blender_worker.py @@ -33,6 +33,30 @@ class Blender(BaseRenderEngine): 'help_text': cls.get_help(), 'formats': cls.get_formats()} + @classmethod + def run_python_expression(cls, path, python_expression): + if os.path.exists(path): + try: + return subprocess.run([cls.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}") + pass + else: + raise FileNotFoundError + + @classmethod + def run_python_script(cls, path, python_path): + if os.path.exists(path) and os.path.exists(python_path): + try: + return subprocess.run([cls.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 + else: + raise FileNotFoundError + class BlenderRenderWorker(BaseRenderWorker): @@ -141,29 +165,6 @@ class BlenderRenderWorker(BaseRenderWorker): return max(total_percent, 0) -def run_python_expression_in_blend(path, python_expression): - if os.path.exists(path): - try: - 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}") - pass - else: - raise FileNotFoundError - - -def run_python_script_in_blend(path, python_path): - if os.path.exists(path) and os.path.exists(python_path): - try: - 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 - else: - raise FileNotFoundError - - def pack_blender_files(path): # Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935 pack_script = "import bpy\n" \ @@ -173,7 +174,7 @@ def pack_blender_files(path): "bpy.ops.wm.save_as_mainfile(filepath=myPath[:-6]+'_packed'+myPath[-6:])" try: - results = run_python_expression_in_blend(path, pack_script) + results = Blender.run_python_script(path, pack_script) result_text = results.stdout.decode() dir_name = os.path.dirname(path) @@ -198,7 +199,7 @@ def get_scene_info(path): scene_info = None try: - results = run_python_script_in_blend(path, os.path.join(os.path.dirname(os.path.realpath(__file__)), + results = Blender.run_python_script(path, os.path.join(os.path.dirname(os.path.realpath(__file__)), 'scripts', 'get_blender_info.py')) result_text = results.stdout.decode() for line in result_text.splitlines():