Packing Blender file now creates a zip

This commit is contained in:
Brett Williams
2023-06-05 14:46:51 -05:00
parent fab9661948
commit 5b54a11788
2 changed files with 34 additions and 21 deletions

View File

@@ -4,6 +4,9 @@ except ImportError:
from base_engine import * from base_engine import *
import json import json
import re import re
import logging
logger = logging.getLogger()
class Blender(BaseRenderEngine): class Blender(BaseRenderEngine):
@@ -76,6 +79,7 @@ class Blender(BaseRenderEngine):
def pack_project_file(cls, project_path, timeout=30): def pack_project_file(cls, project_path, timeout=30):
# Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935 # Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935
try: 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__)), results = cls.run_python_script(project_path, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'scripts', 'blender', 'pack_project.py'), timeout=timeout) 'scripts', 'blender', 'pack_project.py'), timeout=timeout)
@@ -87,10 +91,10 @@ class Blender(BaseRenderEngine):
for err in not_found: for err in not_found:
logger.error(err) logger.error(err)
p = re.compile('Info: Saved "(.*)"') p = re.compile('Saved to: (.*)\n')
match = p.search(result_text) match = p.search(result_text)
if match: 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}') logger.info(f'Blender file packed successfully to {new_path}')
return new_path return new_path
except Exception as e: except Exception as e:

View File

@@ -4,12 +4,18 @@ import shutil
import zipfile import zipfile
def zip_files(file_paths, output_zip_path): def zip_files(paths, output_zip_path):
# Create a new Zip file with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
with zipfile.ZipFile(output_zip_path, 'w') as myzip: for path in paths:
for file_path in file_paths: if os.path.isfile(path):
# Add each file to the Zip file # If the path is a file, add it to the zip
myzip.write(file_path) 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 # 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') asset_dir = os.path.join(tmp_dir, 'assets')
os.makedirs(tmp_dir, exist_ok=True) os.makedirs(tmp_dir, exist_ok=True)
try: # Find images we could not pack - usually videos
# Find images we could not pack - usually videos for img in bpy.data.images:
for img in bpy.data.images: if not img.packed_file and img.filepath and img.users:
if not img.packed_file and img.filepath and img.users: os.makedirs(asset_dir, exist_ok=True)
os.makedirs(asset_dir, exist_ok=True) shutil.copy2(img.filepath, os.path.join(asset_dir, os.path.basename(img.filepath)))
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)) img.filepath = '//' + os.path.join('assets', os.path.basename(img.filepath))
# Save Output # Save Output
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(tmp_dir, os.path.basename(project_path)), compress=True) 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], # Save Zip
os.path.join(os.path.dirname(project_path), 'output.zip')) zip_path = os.path.join(os.path.dirname(project_path), f"{os.path.basename(project_path).split('.')[0]}.zip")
finally: zip_files([os.path.join(tmp_dir, os.path.basename(project_path)), asset_dir], zip_path)
os.remove(tmp_dir) print(f'Saved to: {zip_path}')
# Cleanup
shutil.rmtree(tmp_dir, ignore_errors=True)