104 lines
5.1 KiB
JavaScript
104 lines
5.1 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Focused case: edit mode -> select all -> UV -> 展开(角度) on a mesh.
|
|
*
|
|
* node tools/web/complete/probe-physics.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: "ProbePhysics.py", console: false } } });
|
|
}), { requestId, source }, { timeout: 300_000 });
|
|
};
|
|
const physicsState = async () => {
|
|
const result = await python(`
|
|
import bpy, json
|
|
obj = bpy.context.view_layer.objects.active
|
|
data = {'object': obj.name, 'modifiers': [{'name': m.name, 'type': m.type} for m in obj.modifiers]}
|
|
cloth = next((m for m in obj.modifiers if m.type == 'CLOTH'), None)
|
|
data['cloth_mass'] = cloth.settings.mass if cloth else None
|
|
data['particles'] = [system.name for system in obj.particle_systems]
|
|
data['rigid_body'] = obj.rigid_body.type if obj.rigid_body else None
|
|
data['rigid_world'] = bool(bpy.context.scene.rigidbody_world)
|
|
data['rigid_mass'] = obj.rigid_body.mass if obj.rigid_body else None
|
|
print('PHYS ' + json.dumps(data))
|
|
`);
|
|
const out = result?.result?.json;
|
|
const status = await page.evaluate(() => document.querySelector('[data-testid="engine-status"]')?.textContent ?? "");
|
|
return { state: out?.state, output: out?.output, error: out?.error, status };
|
|
};
|
|
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 });
|
|
const workbench = page.locator('[data-testid="physics-workbench"]').first();
|
|
await workbench.waitFor({ timeout: 30_000 });
|
|
log("before", JSON.stringify(await physicsState()));
|
|
await workbench.getByRole("button", { name: "Add", exact: true }).first().click();
|
|
await page.waitForTimeout(2000);
|
|
log("after", JSON.stringify(await physicsState()));
|
|
const mass = workbench.locator('[aria-label="RIGID_BODY Mass"]');
|
|
if (await mass.count()) {
|
|
await mass.first().fill("5");
|
|
await mass.first().press("Enter");
|
|
await page.waitForTimeout(1500);
|
|
log("mass", JSON.stringify(await physicsState()));
|
|
}
|
|
const manifest = page.locator('[data-testid="physics-simulation-manifest"]').first();
|
|
await manifest.waitFor({ timeout: 30_000 });
|
|
const manifestInfo = async () => page.evaluate(() => {
|
|
const fieldset = document.querySelector('[data-testid="physics-simulation-manifest"]');
|
|
return {
|
|
output: fieldset?.querySelector('output')?.textContent ?? null,
|
|
buttons: Array.from(fieldset?.querySelectorAll('button') ?? []).map((button) => ({ text: button.textContent, disabled: button.hasAttribute('disabled') })),
|
|
frame: document.querySelector('input[aria-label="当前帧"]')?.value ?? null,
|
|
status: document.querySelector('[data-testid="engine-status"]')?.textContent ?? "",
|
|
};
|
|
});
|
|
log("manifest", JSON.stringify(await manifestInfo()));
|
|
await manifest.getByRole("button", { name: "播放", exact: true }).evaluate((element) => element.click());
|
|
await page.waitForTimeout(2500);
|
|
log("playing", JSON.stringify(await manifestInfo()));
|
|
await manifest.getByRole("button", { name: "暂停", exact: true }).evaluate((element) => element.click());
|
|
await page.waitForTimeout(1000);
|
|
log("paused", JSON.stringify(await manifestInfo()));
|
|
} catch (error) {
|
|
log("probe error", error instanceof Error ? error.message : String(error));
|
|
log("state", JSON.stringify(await physicsState().catch(() => null)));
|
|
} finally {
|
|
log("consoleErrors", JSON.stringify(consoleErrors.slice(-10)));
|
|
await context.close();
|
|
await browser.close();
|
|
}
|