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

@@ -61,7 +61,7 @@ class Blender(BaseRenderEngine):
scene_info = None
try:
results = cls.run_python_script(project_path, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'scripts', 'get_blender_info.py'), timeout=timeout)
'scripts', 'blender', 'get_file_info.py'), timeout=timeout)
result_text = results.stdout.decode()
for line in result_text.splitlines():
if line.startswith('SCENE_DATA:'):
@@ -75,15 +75,9 @@ class Blender(BaseRenderEngine):
@classmethod
def pack_project_file(cls, project_path, timeout=30):
# Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935
pack_expression = "import bpy\n" \
"bpy.ops.file.pack_all()\n" \
"bpy.ops.file.make_paths_absolute()\n" \
"myPath = bpy.data.filepath\n" \
"myPath = str(myPath)\n" \
"bpy.ops.wm.save_as_mainfile(filepath=myPath[:-6]+'_packed'+myPath[-6:])"
try:
results = Blender.run_python_expression(project_path, pack_expression, timeout=timeout)
results = cls.run_python_script(project_path, os.path.join(os.path.dirname(os.path.realpath(__file__)),
'scripts', 'blender', 'pack_project.py'), timeout=timeout)
result_text = results.stdout.decode()
dir_name = os.path.dirname(project_path)

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)