From 5b54a1178820cd99adb33a2bd734302c812302f2 Mon Sep 17 00:00:00 2001 From: Brett Williams Date: Mon, 5 Jun 2023 14:46:51 -0500 Subject: [PATCH] Packing Blender file now creates a zip --- lib/render_engines/blender_engine.py | 8 +++- .../scripts/blender/pack_project.py | 47 +++++++++++-------- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/lib/render_engines/blender_engine.py b/lib/render_engines/blender_engine.py index adbf9dd..ea4b08f 100644 --- a/lib/render_engines/blender_engine.py +++ b/lib/render_engines/blender_engine.py @@ -4,6 +4,9 @@ except ImportError: from base_engine import * import json import re +import logging + +logger = logging.getLogger() class Blender(BaseRenderEngine): @@ -76,6 +79,7 @@ class Blender(BaseRenderEngine): def pack_project_file(cls, project_path, timeout=30): # Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935 try: + logger.info(f"Starting to pack Blender file: {project_path}") results = cls.run_python_script(project_path, os.path.join(os.path.dirname(os.path.realpath(__file__)), 'scripts', 'blender', 'pack_project.py'), timeout=timeout) @@ -87,10 +91,10 @@ class Blender(BaseRenderEngine): for err in not_found: logger.error(err) - p = re.compile('Info: Saved "(.*)"') + p = re.compile('Saved to: (.*)\n') match = p.search(result_text) if match: - new_path = os.path.join(dir_name, match.group(1)) + new_path = os.path.join(dir_name, match.group(1).strip()) logger.info(f'Blender file packed successfully to {new_path}') return new_path except Exception as e: diff --git a/lib/render_engines/scripts/blender/pack_project.py b/lib/render_engines/scripts/blender/pack_project.py index d4e155f..3849dbe 100644 --- a/lib/render_engines/scripts/blender/pack_project.py +++ b/lib/render_engines/scripts/blender/pack_project.py @@ -4,12 +4,18 @@ import shutil import zipfile -def zip_files(file_paths, output_zip_path): - # Create a new Zip file - with zipfile.ZipFile(output_zip_path, 'w') as myzip: - for file_path in file_paths: - # Add each file to the Zip file - myzip.write(file_path) +def zip_files(paths, output_zip_path): + with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf: + for path in paths: + if os.path.isfile(path): + # If the path is a file, add it to the zip + zipf.write(path, arcname=os.path.basename(path)) + elif os.path.isdir(path): + # If the path is a directory, add all its files and subdirectories + for root, dirs, files in os.walk(path): + for file in files: + full_path = os.path.join(root, file) + zipf.write(full_path, arcname=os.path.join(os.path.basename(path), os.path.relpath(full_path, path))) # Get File path @@ -24,18 +30,21 @@ tmp_dir = os.path.join(os.path.dirname(project_path), 'tmp') asset_dir = os.path.join(tmp_dir, 'assets') os.makedirs(tmp_dir, exist_ok=True) -try: - # Find images we could not pack - usually videos - for img in bpy.data.images: - if not img.packed_file and img.filepath and img.users: - os.makedirs(asset_dir, exist_ok=True) - shutil.copy2(img.filepath, os.path.join(asset_dir, os.path.basename(img.filepath))) - img.filepath = '//' + os.path.join('assets', os.path.basename(img.filepath)) +# Find images we could not pack - usually videos +for img in bpy.data.images: + if not img.packed_file and img.filepath and img.users: + os.makedirs(asset_dir, exist_ok=True) + shutil.copy2(img.filepath, os.path.join(asset_dir, os.path.basename(img.filepath))) + print(f"Copied {os.path.basename(img.filepath)} to tmp directory") + img.filepath = '//' + os.path.join('assets', os.path.basename(img.filepath)) - # Save Output - bpy.ops.wm.save_as_mainfile(filepath=os.path.join(tmp_dir, os.path.basename(project_path)), compress=True) +# Save Output +bpy.ops.wm.save_as_mainfile(filepath=os.path.join(tmp_dir, os.path.basename(project_path)), compress=True, relative_remap=False) - zip_files([os.path.join(tmp_dir, os.path.basename(project_path)), asset_dir], - os.path.join(os.path.dirname(project_path), 'output.zip')) -finally: - os.remove(tmp_dir) +# Save Zip +zip_path = os.path.join(os.path.dirname(project_path), f"{os.path.basename(project_path).split('.')[0]}.zip") +zip_files([os.path.join(tmp_dir, os.path.basename(project_path)), asset_dir], zip_path) +print(f'Saved to: {zip_path}') + +# Cleanup +shutil.rmtree(tmp_dir, ignore_errors=True)