"""원복 상태 확인: TCP540 토치 복원 + 토치끝 구 0개 + 손목-토치 캡처(직접 판독용)."""
import asyncio, json
from playwright.async_api import async_playwright
URL='http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html'
OUT=r'E:\도진팩토리\3D스캔및티칭시스템\shots\ceo_feedback'
CEO_HOME={"S":-0.7,"L":-59.4,"U":-44.4,"R":-1.0,"B":-92.9,"T":-1.1}
async def main():
    async with async_playwright() as p:
        br=await p.chromium.launch(channel='msedge',headless=True)
        pg=await br.new_page(viewport={'width':1400,'height':950})
        errs=[]; pg.on('pageerror',lambda e: errs.append(str(e)))
        await pg.goto(URL); await pg.wait_for_function("window.__dzw && window.__dzw._torchMesh",timeout=15000)
        res=await pg.evaluate("""(HOME)=>{ const d=window.__dzw,T=d._T; const out={};
          if(d.setJoints) d.setJoints(HOME);
          out.meshName=d._torchMesh?d._torchMesh.name:null;
          const tt=d._truthTool; tt.updateMatrixWorld(true);
          const tcp=new T.Vector3(d.tcpOffset[0],d.tcpOffset[1],d.tcpOffset[2]); let vis=[];
          tt.traverse(o=>{ if(o.isMesh&&o.geometry&&o.geometry.type==='SphereGeometry'&&o.visible){ if(o.position.distanceTo(tcp)<40) vis.push(o.name||'sphere'); }});
          out.visibleSpheresNearTip=vis;
          const me=d._torchMesh; me.updateMatrixWorld(true);
          const b=new T.Box3().setFromObject(me); const c=new T.Vector3(); b.getCenter(c);
          if(d.orbit){ d.orbit.tgt.copy(c); d.orbit.r=0.5; d.orbit.theta=-0.6; d.orbit.phi=1.45; d.updateCam&&d.updateCam(); }
          return out; }""", CEO_HOME)
        await pg.wait_for_timeout(500)
        await pg.screenshot(path=OUT+r'\revert_state_75e2318.png')
        print(json.dumps(res,ensure_ascii=False)); print("errs:",errs if errs else "없음")
        await br.close()
asyncio.run(main())
