#!/usr/bin/env node /** * Focused case: edit mode -> select all -> UV -> 展开(角度) on a mesh. * * node tools/web/complete/probe-compositor-graph.mjs --base-url http://127.0.0.1:5176 */ import { ASSETS, parseArgs, launchBrowser, newPage, openApp, openFile, waitScene } from "../course/lib/lesson-recorder.mjs"; const args = parseArgs(); const BASE = args.baseUrl || "http://127.0.0.1:5176"; const browser = await launchBrowser(args.gpu); const { context, page, consoleErrors } = await newPage(browser, {}); page.setDefaultTimeout(90_000); await page.addInitScript(() => { const NativeWorker = window.Worker; window.__probeWorkers = []; window.__probeMessages = []; window.Worker = class extends NativeWorker { constructor(...params) { super(...params); window.__probeWorkers.push(this); this.addEventListener("message", (event) => window.__probeMessages.push(event.data)); } }; }); const log = (...parts) => console.log(new Date().toISOString().slice(11, 19), ...parts); const python = async (source) => { const requestId = `probe-${Date.now()}-${Math.random().toString(36).slice(2)}`; return await page.evaluate(({ requestId, source }) => new Promise((resolve) => { const started = Date.now(); const read = () => { const hit = window.__probeMessages.find((message) => message && message.requestId === requestId && message.kind === "result"); if (hit) { clearInterval(timer); resolve(hit); return; } if (Date.now() - started > 240000) { clearInterval(timer); resolve({ timeout: true }); } }; const timer = setInterval(read, 250); for (const worker of window.__probeWorkers) worker.postMessage({ requestId, command: { type: "jsonCommand", cmd: "pythonExec", params: { source, filename: "ProbeCompGraph.py", console: false } } }); }), { requestId, source }, { timeout: 300_000 }); }; const compState = async () => { const result = await python(` import bpy, json scene = bpy.context.scene tree = getattr(scene, 'compositing_node_group', None) or getattr(scene, 'node_tree', None) nodes = [{'name': node.name, 'type': node.bl_idname} for node in tree.nodes] if tree else [] print('COMP ' + json.dumps({'nodes': nodes, 'use_compositing': scene.render.use_compositing})) `); const out = result?.result?.json; const ui = await page.evaluate(() => ({ canvas: document.querySelector('[data-testid="compositor-canvas"]')?.getAttribute('data-node-count') ?? null, status: document.querySelector('[data-testid="engine-status"]')?.textContent ?? "", })); return { state: out?.state, output: out?.output, error: out?.error, ...ui }; }; const menu = async (label, item) => { await page.getByRole("button", { name: label, exact: true }).click(); await page.getByRole("menuitem", { name: item, exact: true }).click(); }; try { await openApp(page, BASE); await openFile(page, ASSETS.studio); await waitScene(page, { objects: 6, timeout: 240_000 }); await page.locator('select[aria-label^="切换 "]').first().selectOption("Compositor"); const canvas = page.locator('[data-testid="compositor-canvas"]').first(); await canvas.waitFor({ timeout: 30_000 }); log("before", JSON.stringify(await compState())); const types = await canvas.locator('select[aria-label="添加节点类型"] option').evaluateAll((nodes) => nodes.map((node) => node.value)); log("types", JSON.stringify(types.slice(0, 12))); const addType = types.includes("CONSTANT_COLOR") ? "CONSTANT_COLOR" : types[0]; log("addType", addType); await canvas.locator('select[aria-label="添加节点类型"]').selectOption(addType); await canvas.getByRole("button", { name: "添加节点", exact: true }).evaluate((element) => element.click()); await page.waitForTimeout(800); log("added", JSON.stringify(await compState())); const fit = page.locator('.react-flow__controls-fitview').first(); if (await fit.count()) { await fit.click({ force: true }); await page.waitForTimeout(600); } const sourceHandle = canvas.locator('.react-flow__node', { hasText: "CONSTANT_COLOR" }).locator('[data-handleid="OUT:Image"]').first(); const targetHandle = canvas.locator('.react-flow__node', { hasText: "COMPOSITE" }).locator('[data-handleid="IN:Image"]').first(); const sourceBox = await sourceHandle.boundingBox(); const targetBox = await targetHandle.boundingBox(); log("boxes", JSON.stringify({ sourceBox, targetBox })); if (sourceBox && targetBox) { await page.mouse.move(sourceBox.x + sourceBox.width / 2, sourceBox.y + sourceBox.height / 2); await page.mouse.down(); await page.mouse.move(targetBox.x + targetBox.width / 2, targetBox.y + targetBox.height / 2, { steps: 12 }); await page.mouse.up(); await page.waitForTimeout(600); } log("linked", JSON.stringify(await compState())); await canvas.getByRole("button", { name: "提交图", exact: true }).evaluate((element) => element.click()); await page.waitForTimeout(2500); log("committed", JSON.stringify(await compState())); const panel = page.locator('[data-testid="scene-pipeline-settings-workbench"]').first(); await panel.waitFor({ timeout: 30_000 }); await panel.getByRole("button", { name: "WASM 合成求值", exact: true }).evaluate((element) => element.click()); for (let index = 0; index < 120; index++) { await page.waitForTimeout(500); const current = await compState(); const evalStatus = await page.evaluate(() => document.querySelector('[data-testid="scene-pipeline-settings-workbench"] output[role="status"]')?.textContent ?? ""); const hasImage = await page.evaluate(() => Boolean(document.querySelector('[data-testid="scene-pipeline-settings-workbench"] img'))); if (hasImage || (evalStatus && !/求值中/.test(evalStatus))) { log("evaluated", JSON.stringify({ ...current, evalStatus, hasImage })); break; } if (index === 119) log("eval timeout", JSON.stringify({ ...current, evalStatus, hasImage })); } } catch (error) { log("probe error", error instanceof Error ? error.message : String(error)); log("state", JSON.stringify(await compState().catch(() => null))); } finally { log("consoleErrors", JSON.stringify(consoleErrors.slice(-10))); await context.close(); await browser.close(); }