"""완료 보고용 캡처 — tgt 미설정(검증된 프레이밍), r/phi/theta만 조정."""
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 cam(pg,phi,theta,r):
    await pg.evaluate("(o)=>{const a=window.__dzw;if(a.orbit){a.orbit.phi=o.phi;a.orbit.theta=o.theta;a.orbit.r=o.r;a.updateCam();}}",{'phi':phi,'theta':theta,'r':r})
    await pg.wait_for_timeout(900)

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(32000)
        # 오버레이 + 큰 초록 fk점 주입
        await pg.evaluate("""async ()=>{const a=window.__dzw,T=a._T,grp=a._grp;
          a.loadWeldPath(); await new Promise(r=>setTimeout(r,2200));
          const mat=new T.MeshBasicMaterial({color:0x22ff44});
          const wp=%s;
          wp.forEach(j=>{const t=a.fk(j);const s=new T.Mesh(new T.SphereGeometry(16,12,10),mat);s.position.set(t.x,t.y,t.z);grp.add(s);});
        }"""%json.dumps(arc))
        # 1) 홈자세 전체
        await pg.evaluate("(()=>window.__dzw.setJoints({S:0,L:0,U:0,R:0,B:0,T:0}))()")
        await cam(pg,1.1,0.6,3.3); await pg.screenshot(path='shots/FINAL_home.png')
        # 2) 경로 포개짐: 위에서 내려다보기 (phi 작게) — 오버레이(4색)+fk점(초록) 겹침
        await cam(pg,0.32,0.6,3.0); await pg.screenshot(path='shots/FINAL_path.png')
        await cam(pg,0.55,1.3,2.6); await pg.screenshot(path='shots/FINAL_path2.png')
        # 3) T회전: 팔 뻗은 자세에서 T=0 / +90 / -90 (검증된 근접 프레이밍, tgt 미설정)
        base={'S':0,'L':30,'U':-30,'R':0,'B':-50}
        await pg.evaluate("(j)=>window.__dzw.setJoints(j)",{**base,'T':0}); await cam(pg,1.25,0.35,1.5); await pg.screenshot(path='shots/FINAL_T0.png')
        await pg.evaluate("(j)=>window.__dzw.setJoints(j)",{**base,'T':90}); await pg.wait_for_timeout(600); await pg.screenshot(path='shots/FINAL_T90.png')
        await pg.evaluate("(j)=>window.__dzw.setJoints(j)",{**base,'T':-90}); await pg.wait_for_timeout(600); await pg.screenshot(path='shots/FINAL_Tneg90.png')
        print("완료: FINAL_home FINAL_path FINAL_path2 FINAL_T0 FINAL_T90 FINAL_Tneg90")
        await br.close()
asyncio.run(main())
