Update pack_project script for Blender

This commit is contained in:
Brett Williams
2023-06-05 13:11:18 -05:00
parent 21557cf4ed
commit fab9661948
3 changed files with 44 additions and 9 deletions

View File

@@ -0,0 +1,41 @@
import bpy
import os
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)
# Get File path
project_path = str(bpy.data.filepath)
# Pack Files
bpy.ops.file.pack_all()
bpy.ops.file.make_paths_absolute()
# Temp dir
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))
# Save Output
bpy.ops.wm.save_as_mainfile(filepath=os.path.join(tmp_dir, os.path.basename(project_path)), compress=True)
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)