"""토치 노즐끝 클로즈업 — tgt를 노즐끝 월드로 정확히 설정, 마커(주황) 크게."""
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 shot(pg,j,name,r=0.42):
    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':1.2,'theta':0.6})
    await pg.wait_for_timeout(900); await pg.screenshot(path=f'shots/{name}.png')
    return tgt
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("""()=>{const a=window.__dzw; if(a._tcpMarker){a._tcpMarker.material.color.setHex(0xff8800);a._tcpMarker.material.emissive.setHex(0xff6600);}}""")
        t=await shot(pg,{'S':0,'L':30,'U':-30,'R':0,'B':-50,'T':0},'CLOSE_T0',1.15)
        print("노즐끝 world:",{k:round(v,3) for k,v in t.items()})
        await shot(pg,{'S':0,'L':30,'U':-30,'R':0,'B':-50,'T':90},'CLOSE_T90',1.15)
        await shot(pg,{'S':0,'L':30,'U':-30,'R':0,'B':-50,'T':-90},'CLOSE_Tneg90',1.15)
        await br.close()
asyncio.run(main())
