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,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)