mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 16:58:12 +00:00
Improve Blender file parsing.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import re
|
||||
from utilities.render_worker import *
|
||||
|
||||
@@ -126,6 +127,18 @@ def run_python_expression_in_blend(path, python_expression):
|
||||
raise FileNotFoundError
|
||||
|
||||
|
||||
def run_python_script_in_blend(path, python_path):
|
||||
if os.path.exists(path) and os.path.exists(python_path):
|
||||
try:
|
||||
return subprocess.run([BlenderRenderWorker.renderer_path(), '-b', path, '--python', python_path],
|
||||
capture_output=True)
|
||||
except Exception as e:
|
||||
logger.warning(f"Error running python expression in blender: {e}")
|
||||
pass
|
||||
else:
|
||||
raise FileNotFoundError
|
||||
|
||||
|
||||
def pack_blender_files(path):
|
||||
# Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935
|
||||
pack_script = "import bpy\nbpy.ops.file.pack_all()\nmyPath = bpy.data.filepath\nmyPath = str(myPath)\n" \
|
||||
@@ -153,22 +166,21 @@ def pack_blender_files(path):
|
||||
return None
|
||||
|
||||
|
||||
def get_cameras_in_scene(path):
|
||||
cam_script = "import bpy;print('CAM:' + str([x.name + '@' + str(x.lens) for x in bpy.data.cameras]))"
|
||||
def get_scene_info(path):
|
||||
|
||||
scene_info = None
|
||||
try:
|
||||
results = run_python_expression_in_blend(path, cam_script)
|
||||
|
||||
results = run_python_script_in_blend(path, os.path.join(os.path.dirname(os.path.realpath(__file__)),
|
||||
'get_blender_info.py'))
|
||||
result_text = results.stdout.decode()
|
||||
p = re.compile('CAM:\[(.*)\]')
|
||||
match = p.search(result_text)
|
||||
if match:
|
||||
cameras = match.group(1).replace("'", "").split(",")
|
||||
cameras = [{'name': x.split('@')[0].strip(), 'lens': x.split('@')[-1].strip()} for x in cameras]
|
||||
return cameras
|
||||
for line in result_text.splitlines():
|
||||
if line.startswith('SCENE_DATA:'):
|
||||
raw_data = line.split('SCENE_DATA:')[-1]
|
||||
scene_info = json.loads(raw_data)
|
||||
break
|
||||
except Exception as e:
|
||||
logger.error(f'Error getting file details for .blend file: {e}')
|
||||
return None
|
||||
return scene_info
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user