#!/usr/bin/env node /** * Generates the desktop-Blender reference values for the Transform Orientation / * Transform Pivot Point functional tests. * * The reference comes from the real desktop Blender build in this repo, running * the genuine `bpy.ops.transform.*` operators against the same fixture the web * tests open. The web tests compare their results against this file instead of * against hand-written numbers. * * usage: node tools/web/make-transform-settings-reference.mjs [--out FILE] */ import { spawn } from 'node:child_process'; import { createHash } from 'node:crypto'; import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs'; import path from 'node:path'; const REPO = path.resolve(import.meta.dirname, '..', '..'); const SCRIPT = path.join(REPO, 'tools', 'web', 'transform-settings-reference.py'); const BLENDER = process.env.TX_BLENDER ?? path.join(REPO, 'build_blender_native', 'bin', 'blender'); const FIXTURE = process.env.TX_FIXTURE ?? path.join(REPO, 'tests', 'files', 'web', 'basic_scene.blend'); /* The monkey GLB the multi-object cases import; same asset the web tests use. */ const MONKEY = process.env.TX_MONKEY_GLB ?? path.join(REPO, 're', '猴头.glb'); const DEFAULT_OUT = path.join(REPO, 'tests', 'golden', 'TRANSFORM-SETTINGS-REFERENCE', 'reference.json'); function fail(message, code = 1) { process.stderr.write(`${message}\n`); process.exit(code); } const args = process.argv.slice(2); let out = DEFAULT_OUT; for (let index = 0; index < args.length; index += 1) { if (args[index] === '--out') out = path.resolve(args[++index] ?? DEFAULT_OUT); else if (args[index] === '--help' || args[index] === '-h') { process.stdout.write('usage: node tools/web/make-transform-settings-reference.mjs [--out FILE]\n'); process.exit(0); } else fail(`unknown argument: ${args[index]}`, 64); } for (const [label, file] of [['desktop Blender', BLENDER], ['reference script', SCRIPT], ['fixture', FIXTURE]]) { if (!existsSync(file)) fail(`missing ${label}: ${file}`, 2); } mkdirSync(path.dirname(out), { recursive: true }); /* A real window is required: `bpy.ops.transform.*` polls on a wmWindow + screen. */ const child = spawn(BLENDER, [ '--factory-startup', '--disable-autoexec', FIXTURE, '--python', SCRIPT, ], { env: { ...process.env, TX_REFERENCE_OUT: out, TX_MONKEY_GLB: MONKEY, ...(process.env.DISPLAY ? {} : { DISPLAY: ':1' }) }, stdio: ['ignore', 'pipe', 'pipe'], }); let stdout = '', stderr = ''; child.stdout.on('data', (chunk) => { stdout += chunk; }); child.stderr.on('data', (chunk) => { stderr += chunk; }); child.on('exit', (code) => { if (!existsSync(out)) { process.stderr.write(`${stdout}\n${stderr}\n`); fail(`desktop Blender did not write ${out} (exit ${code})`, 1); } const reference = JSON.parse(readFileSync(out, 'utf8')); reference.fixture = { path: FIXTURE, sha256: createHash('sha256').update(readFileSync(FIXTURE)).digest('hex'), bytes: statSync(FIXTURE).size, }; if (existsSync(MONKEY)) { reference.monkey = { path: MONKEY, sha256: createHash('sha256').update(readFileSync(MONKEY)).digest('hex'), bytes: statSync(MONKEY).size, }; } reference.blenderPath = BLENDER; reference.generatedAt = new Date().toISOString(); reference.acceptance = 'NOT_CLAIMED'; /* Bind the reference to the exact inputs it came from: the previous version * computed these in memory but never wrote them back, so the file on disk * carried no provenance at all. */ writeFileSync(out, `${JSON.stringify(reference, null, 2)}\n`); process.stdout.write(`${JSON.stringify({ status: 'PASS', out, blender: reference.blender, cases: Object.keys(reference.cases), }, null, 2)}\n`); process.exit(0); });