mirror of
https://github.com/blw1138/Zordon.git
synced 2025-12-17 08:48:13 +00:00
Improve Blender file parsing.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
from utilities.render_worker import *
|
from utilities.render_worker import *
|
||||||
|
|
||||||
@@ -126,6 +127,18 @@ def run_python_expression_in_blend(path, python_expression):
|
|||||||
raise FileNotFoundError
|
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):
|
def pack_blender_files(path):
|
||||||
# Credit to L0Lock for pack script - https://blender.stackexchange.com/a/243935
|
# 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" \
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
def get_cameras_in_scene(path):
|
def get_scene_info(path):
|
||||||
cam_script = "import bpy;print('CAM:' + str([x.name + '@' + str(x.lens) for x in bpy.data.cameras]))"
|
|
||||||
|
|
||||||
|
scene_info = None
|
||||||
try:
|
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()
|
result_text = results.stdout.decode()
|
||||||
p = re.compile('CAM:\[(.*)\]')
|
for line in result_text.splitlines():
|
||||||
match = p.search(result_text)
|
if line.startswith('SCENE_DATA:'):
|
||||||
if match:
|
raw_data = line.split('SCENE_DATA:')[-1]
|
||||||
cameras = match.group(1).replace("'", "").split(",")
|
scene_info = json.loads(raw_data)
|
||||||
cameras = [{'name': x.split('@')[0].strip(), 'lens': x.split('@')[-1].strip()} for x in cameras]
|
break
|
||||||
return cameras
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f'Error getting file details for .blend file: {e}')
|
logger.error(f'Error getting file details for .blend file: {e}')
|
||||||
return None
|
return scene_info
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
19
utilities/get_blender_info.py
Normal file
19
utilities/get_blender_info.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
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, 'lens': cam_obj.lens}
|
||||||
|
cameras.append(cam)
|
||||||
|
|
||||||
|
data = {'cameras': cameras,
|
||||||
|
'frame_start': bpy.data.scenes[0].frame_start,
|
||||||
|
'frame_end': bpy.data.scenes[0].frame_end,
|
||||||
|
'resolution_x': bpy.data.scenes[0].render.resolution_x,
|
||||||
|
'resolution_y': bpy.data.scenes[0].render.resolution_y}
|
||||||
|
|
||||||
|
data_string = json.dumps(data)
|
||||||
|
print("SCENE_DATA:" + data_string)
|
||||||
Reference in New Issue
Block a user