"""배포용 최종 캡처 4장 — 홈/T0/T90/용접자세 클로즈업 (생산 색상, tgt.set 프레이밍)."""
import asyncio, json
from playwright.async_api import async_playwright
URL='http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html'
wp=json.loads(open(r'E:\도진팩토리\3D스캔및티칭시스템\weld_path.json',encoding='utf-8').read())
arc=[{k:p[k] for k in ['S','L','U','R','B','T']} for p in wp['poses'] if p.get('arc')==1]
async def tip_shot(pg,j,name,r,phi=1.2,theta=0.6):
    tgt=await pg.evaluate("""(j)=>{const a=window.__dzw,T=a._T; a.setJoints(j); a._grp.updateMatrixWorld(true);
      const w=new T.Vector3(); a._tcpMarker.getWorldPosition(w); return {x:w.x,y:w.y,z:w.z};}""",j)
    await pg.evaluate("""(o)=>{const a=window.__dzw; if(a.orbit){a.orbit.tgt.set(o.x,o.y,o.z);a.orbit.r=o.r;a.orbit.phi=o.phi;a.orbit.theta=o.theta;a.updateCam();}}""",{**tgt,'r':r,'phi':phi,'theta':theta})
    await pg.wait_for_timeout(900); await pg.screenshot(path=f'shots/{name}.png')
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':1536,'height':864})
        await pg.goto(URL); await pg.wait_for_timeout(33000)
        await pg.evaluate("()=>window.__dzw.loadWeldPath()"); await pg.wait_for_timeout(1500)
        # 1) 홈 — 전체 (기본 tgt 복원)
        await pg.evaluate("""()=>{const a=window.__dzw; a.setJoints({S:0,L:0,U:0,R:0,B:0,T:0}); a.orbit.tgt.set(0.1,0,0.4); a.orbit.r=3.2;a.orbit.phi=1.1;a.orbit.theta=0.6;a.updateCam();}""")
        await pg.wait_for_timeout(900); await pg.screenshot(path='shots/DELIV_home.png')
        # 2,3) T0 / T90 클로즈업
        base={'S':0,'L':30,'U':-30,'R':0,'B':-50}
        await tip_shot(pg,{**base,'T':0},'DELIV_T0',1.15)
        await tip_shot(pg,{**base,'T':90},'DELIV_T90',1.15)
        # 4) 용접자세 — 노즐끝이 용접선에 (SEAM-01 포즈)
        await tip_shot(pg,arc[0],'DELIV_weld',1.5,phi=1.15,theta=0.5)
        print("배포캡처: DELIV_home DELIV_T0 DELIV_T90 DELIV_weld")
        await br.close()
asyncio.run(main())
