"""Only allow registered deterministic geometry and rendering executors.""" import json import os from pathlib import Path import subprocess import sys ROOT=Path(__file__).resolve().parents[2] def execute(script,args,out,logname,timeout=1800,blender=True): if script not in ('build_model.py','build_interior.py','render_interior.py','export_3dm.py','build_fitout.py','render_fitout.py','export_fitout.py'):raise ValueError('工具不在执行白名单') if blender: from paths import BLENDER cmd=[BLENDER,'--background','--factory-startup','--threads',str(max(1,min(4,int(os.getenv('ARCHROBOT_BLENDER_THREADS','4'))))),'--python',str(ROOT/'demo'/script),'--']+list(map(str,args)) else:cmd=[sys.executable,str(ROOT/'tools'/script)]+list(map(str,args)) with (out/logname).open('w',encoding='utf-8') as log: result=subprocess.run(cmd,stdout=log,stderr=log,timeout=timeout,creationflags=(0x00004000|0x08000000) if os.name=='nt' else 0) if result.returncode:raise RuntimeError(script+' 执行失败(退出码 '+str(result.returncode)+'),请查看 '+logname) def validate_outputs(out,interior,render): names=['model.glb','model.blend','model_info.json'] if interior:names+=['model.3dm','model-white.glb','rhino-report.json'] if render:names+=['render.png'] for name in names: f=out/name if not f.is_file() or not f.stat().st_size:raise ValueError('缺少输出文件:'+name) if (out/'model.glb').read_bytes()[:4]!=b'glTF':raise ValueError('无效 GLB 文件头') if interior: report=json.loads((out/'rhino-report.json').read_text(encoding='utf-8')) if report['invalidObjects'] or not report['newSolids']:raise ValueError('3DM 实体校验未通过') info=json.loads((out/'model_info.json').read_text(encoding='utf-8')) if info.get('variants'): for variant in ('structural','finished'): for ext in ('glb','3dm'): if not (out/f'model-{variant}.{ext}').is_file():raise ValueError('缺少双模型输出') if render and not (out/f'render-{variant}.png').is_file():raise ValueError('缺少双效果图') return dict(required=names,valid=True)