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,29 @@
import json
import bpy
# Get all cameras
cameras = []
for cam_obj in bpy.data.cameras:
user_map = bpy.data.user_map(subset={cam_obj}, value_types={'OBJECT'})
for data_obj in user_map[cam_obj]:
cam = {'name': data_obj.name,
'cam_name': cam_obj.name,
'cam_name_full': cam_obj.name_full,
'lens': cam_obj.lens,
'lens_unit': cam_obj.lens_unit,
'sensor_height': cam_obj.sensor_height,
'sensor_width': cam_obj.sensor_width}
cameras.append(cam)
scene = bpy.data.scenes[0]
data = {'cameras': cameras,
'engine': scene.render.engine,
'frame_start': scene.frame_start,
'frame_end': scene.frame_end,
'resolution_x': scene.render.resolution_x,
'resolution_y': scene.render.resolution_y,
'resolution_percentage': scene.render.resolution_percentage,
'fps': scene.render.fps}
data_string = json.dumps(data)
print("SCENE_DATA:" + data_string)

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)