"""씬 구조 조사: '플랜지'가 어떤 객체인지 — 토치 노드/진실툴/로봇 손목의 자식 메시 나열."""
import asyncio
from playwright.async_api import async_playwright
URL='http://localhost:8090/DOZIKWORKS_OS_v5_locked.dc.html'

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_function("window.__dzw && window.__dzw._torchPivot", timeout=15000)
        res=await pg.evaluate("""()=>{
          const d=window.__dzw, T=d._T, out={};
          function desc(o){ if(!o) return null;
            const box=new T.Box3().setFromObject(o); const c=new T.Vector3(); box.getCenter(c); const s=new T.Vector3(); box.getSize(s);
            return {name:o.name||'(no name)', type:o.type, vis:o.visible,
              raycastOff:(o.raycast&&o.raycast.toString().length<40 && o.raycast.toString().includes('=>')) ,
              center:[+c.x.toFixed(0),+c.y.toFixed(0),+c.z.toFixed(0)], size:[+s.x.toFixed(0),+s.y.toFixed(0),+s.z.toFixed(0)],
              children:o.children.length}; }
          // 토치 노드 트리
          out.torchNode = desc(d._torchNode);
          out.torchNode_children = (d._torchNode?d._torchNode.children:[]).map(desc);
          out.torchPivot_children = (d._torchPivot?d._torchPivot.children:[]).map(desc);
          out.torchMesh = desc(d._torchMesh);
          // 진실툴(truthTool) 자식 = 마커/축/용접점 등
          out.truthTool = d._truthTool?desc(d._truthTool):null;
          out.truthTool_children = (d._truthTool?d._truthTool.children:[]).map(desc);
          // 로봇 손목/플랜지 후보: scene 전체에서 이름에 flange/plate/wrist/플랜지 포함
          const hits=[]; d.three.scene.traverse(o=>{ const n=(o.name||'').toLowerCase();
            if(n.includes('flange')||n.includes('plate')||n.includes('wrist')||n.includes('tool')||n.includes('tcp')||n.includes('weld')||n.includes('marker')) hits.push(desc(o)); });
          out.named_hits=hits;
          // 마커류 참조
          out.refs={hasWeldPoint:!!d._weldPoint, hasTcpMarker:!!d._tcpMarker, weldVis:d._weldPoint?d._weldPoint.visible:null, tcpVis:d._tcpMarker?d._tcpMarker.visible:null};
          return out;
        }""")
        import json
        print(json.dumps(res,ensure_ascii=False,indent=1))
        await br.close()

asyncio.run(main())
