Files
AIC-Project/Tools/ModelTranslator/mt_run.py
T

37 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""find_blender / run_blender_scriptmodel_decimate 与 model_bake 共享的
headless Blender 运行器(与 model_translator.py 同模式,脚本名与参数泛化)。"""
import json
import os
import shutil
import subprocess
import sys
DEFAULT_BLENDER = r"D:\tools\blender-5.0.0-windows-x64\blender.exe"
HERE = os.path.dirname(os.path.abspath(__file__))
def find_blender(cli_arg):
for cand in (cli_arg, os.environ.get("BLENDER_EXE"), DEFAULT_BLENDER,
shutil.which("blender")):
if cand and os.path.isfile(cand):
return cand
sys.exit("找不到 blender.exe,请用 --blender 指定或设置环境变量 BLENDER_EXE")
def run_blender_script(blender, script, script_args):
"""跑 HERE 下的 bl_*.py,返回 MT_SUMMARY JSON;失败或 summary 带 error 则退出。"""
cmd = [blender, "-b", "--factory-startup",
"--python", os.path.join(HERE, script), "--"] + list(script_args)
proc = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8",
errors="replace")
summary = None
for line in (proc.stdout or "").splitlines():
if line.startswith("MT_SUMMARY "):
summary = json.loads(line[len("MT_SUMMARY "):])
if proc.returncode != 0 or summary is None or "error" in summary:
sys.stderr.write((proc.stdout or "")[-2000:] + "\n" +
(proc.stderr or "")[-2000:] + "\n")
err = summary["error"] if summary and "error" in summary else script
sys.exit("Blender 执行失败:%s" % err)
return summary