#!/usr/bin/env node /** * Focused case: commit a connected compositor graph (RGB -> Composite) and run * the WASM CPU compositor evaluation through the product worker. * * node tools/web/complete/probe-compositor-eval.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 request = async (command) => { const requestId = `probe-${Date.now()}-${Math.random().toString(36).slice(2)}`; return await page.evaluate(({ requestId, command }) => 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 }); }), { requestId, command }, { timeout: 300_000 }); }; const python = (source) => request({ type: "jsonCommand", cmd: "pythonExec", params: { source, filename: "ProbeCompEval.py", console: false } }); try { await openApp(page, BASE); await openFile(page, ASSETS.studio); await waitScene(page, { objects: 6, timeout: 240_000 }); const sceneResult = await python("import bpy, json\nprint('SCENE ' + json.dumps({'name': bpy.context.scene.name}))"); const sceneId = `scene:${JSON.parse(sceneResult.result.json.output.split('SCENE ')[1].trim()).name}`; log("scene", sceneId); const graph = { schemaVersion: 1, id: "probe-graph", name: "Probe Compositor", outputNodeId: "out", nodes: [ { id: "rgb", type: "CONSTANT_COLOR", name: "RGB", properties: { color: [1, 0, 0, 1] }, location: { x: 0, y: 0 } }, { id: "out", type: "COMPOSITE", name: "Composite", properties: {}, location: { x: 300, y: 0 } }, ], links: [{ fromNodeId: "rgb", fromSocket: "Image", toNodeId: "out", toSocket: "Image" }], resources: [], }; const committed = await request({ type: "applyCommand", payload: { type: "setCompositorNodeGraph", sceneId, graph } }); log("commit", JSON.stringify({ ok: committed.ok, error: committed.error, revision: committed.result?.snapshot?.revision })); const treeResult = await python(`import bpy, json scene = bpy.context.scene tree = getattr(scene, 'compositing_node_group', None) nodes = [{'name': node.name, 'type': node.bl_idname} for node in tree.nodes] if tree else [] links = [{'from': link.from_node.name, 'fromSocket': link.from_socket.name, 'to': link.to_node.name, 'toSocket': link.to_socket.name} for link in tree.links] if tree else [] print('TREE ' + json.dumps({'nodes': nodes, 'links': links}))`); log("tree", treeResult.result?.json?.output ?? null); const evaluated = await request({ type: "jsonCommand", cmd: "compositorEvaluate", params: { sceneId } }); const payload = evaluated.result?.json; log("evaluate", JSON.stringify({ ok: evaluated.ok, error: evaluated.error, width: payload?.width, height: payload?.height, pixels: typeof payload?.pixelsBase64 === "string" ? payload.pixelsBase64.length : null })); 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 ui = await page.evaluate(() => { const workbench = document.querySelector('[data-testid="scene-pipeline-settings-workbench"]'); return { status: workbench?.querySelector('output[role="status"]')?.textContent ?? "", hasImage: Boolean(workbench?.querySelector('img')) }; }); if (ui.hasImage || (ui.status && !/求值中/.test(ui.status))) { log("ui", JSON.stringify(ui)); break; } if (index === 119) log("ui timeout", JSON.stringify(ui)); } } catch (error) { log("probe error", error instanceof Error ? error.message : String(error)); } finally { log("consoleErrors", JSON.stringify(consoleErrors.slice(-8))); await context.close(); await browser.close(); }