mirror of
https://github.com/shinyobjectz/raea.git
synced 2026-12-01 12:44:11 -10:00
Shane, 11-10: "include natively a blender api package in raea/blender using samchon/blendia as good starting place and get us to a solid scripting parity to the official blender python api. we will own this ourselves", and "make sure to add GodotJS which should add a more complete integration and bindings for godot integration". raea/blender (sdk/blender, started from samchon/blendia, MIT, and rewritten): bpy, bmesh, mathutils, bpy_extras and gpu typed from Blender 5.2.1's own RNA and docstrings (tools/gen.sh: 1809 structs, 2498 operators, 41 modules outside RNA; 102 of about 12 900 members any), run against a real Blender. Paths are lazy as Python's attribute access is (writes and calls sent in order, reads awaited, py.pin for a stable handle), errors are Python's (BlenderError), mathutils is computed in TypeScript with Blender's maths (1450 values within 1e-5 of Blender's), float arrays are the value Blender gives (Vector, Matrix, Euler, Quaternion, Color), a call that makes a thing by its type returns that class (tools/refine.py), node sockets are typed by name, and every argument can be given by keyword. `raea blender run SCRIPT.ts [FILE.blend] [ARGS...]` runs a script on raea's engine against a headless Blender (apps/tools blender_bridge.rs, runtime/calls blender.post); under Bun or Node it starts its own, or joins the person's Blender through raea's add-on (python/raea_blender). raea/godot (sdk/godot) on GodotJS (MIT): Godot 4.6.1's every class, singleton, enum and builtin typed from GodotJS's codegen, JavaScriptCore on the Mac and QuickJS-ng on Windows and Linux (no V8). `raea setup godot` downloads GodotJS's editor build pinned by size and sha256 (apps/tools godot.rs); `raea godot run SCRIPT.ts` bundles the script and runs its default export (a function given the SceneTree and ARGS, or a SceneTree subclass as the main loop) headless or with --window, --movie recording with Godot's Movie Maker (the window kept on top, the frame size from the project), a callback that throws stopping the run, RAEA_CWD with cwd() and resolve() for paths; `raea godot types` and `raea godot open` (godot_run.rs). The guides: raea guide bevy/blender gains "A step in TypeScript" and bevy/godot is new, each example type-checked with its SDK's tests (sdk/blender/test/guide.ts, sdk/godot/test/guide.ts) and run as shown; raea guide bevy routes to bevy/godot. Their raea-skills copies are 7497067 (branch blender-godot-ts, not pushed). sdk/tsconfig.base.json maps raea/blender and raea/godot; tools/release.sh ships @raea/blender's python/. Proven: sdk/blender's tsc and bun tests against Blender 5.2.1; sdk/godot's tsc and its 7 bun tests against the installed GodotJS; apps/tools godot_run's unit tests; post 07b's film (releases/0.1.1/src/engines), its left half built and keyed by raea/blender and its right half recorded by raea/godot.
159 lines
11 KiB
TypeScript
159 lines
11 KiB
TypeScript
// mathutils parity: each operation on seeded random inputs, computed here (src/mathutils) and in Blender's own
|
|
// mathutils, must agree. One exec evaluates every Python expression, so the run is one round trip.
|
|
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
import { Color, Euler, findBlender, Matrix, Quaternion, start, Vector, type Blender } from "../src";
|
|
|
|
const has = findBlender() !== null;
|
|
let b: Blender;
|
|
beforeAll(async () => {
|
|
if (has) b = await start();
|
|
});
|
|
afterAll(async () => {
|
|
await b?.close();
|
|
});
|
|
|
|
/** mulberry32: the same inputs every run */
|
|
function rng(seed: number): () => number {
|
|
return () => {
|
|
seed = (seed + 0x6d2b79f5) | 0;
|
|
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
|
|
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
};
|
|
}
|
|
|
|
const py = (v: unknown): string => (Array.isArray(v) ? `(${v.map(py).join(", ")}${v.length === 1 ? "," : ""})` : typeof v === "string" ? `'${v}'` : String(v));
|
|
const ORDERS = ["XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"] as const;
|
|
type Case = [name: string, python: string, local: () => unknown];
|
|
|
|
function cases(r: () => number): Case[] {
|
|
const n = () => Math.round((r() * 4 - 2) * 1e4) / 1e4;
|
|
const v3 = () => [n(), n(), n()];
|
|
const ang = () => Math.round((r() * 6 - 3) * 1e4) / 1e4;
|
|
const ord = ORDERS[Math.floor(r() * 6)];
|
|
const a = v3(), c = v3(), e = v3(), s = [r() + 0.5, r() + 0.5, r() + 0.5].map((x) => Math.round(x * 1e4) / 1e4);
|
|
const q = [n(), n(), n(), n()], t = Math.round(r() * 1e4) / 1e4, angle = ang(), sh = n();
|
|
const lrs = `Matrix.LocRotScale(Vector(${py(a)}), Euler(${py(e)}, '${ord}'), Vector(${py(s)}))`;
|
|
const LRS = () => Matrix.LocRotScale(new Vector(a), new Euler(e, ord), new Vector(s));
|
|
const m = (f: (x: Matrix) => unknown) => f(LRS());
|
|
const mut = <T>(x: T, f: (x: T) => void) => (f(x), x);
|
|
return [
|
|
["vector.normalized", `Vector(${py(a)}).normalized()`, () => new Vector(a).normalized()],
|
|
["vector.cross", `Vector(${py(a)}).cross(Vector(${py(c)}))`, () => new Vector(a).cross(new Vector(c))],
|
|
["vector.dot", `Vector(${py(a)}).dot(Vector(${py(c)}))`, () => new Vector(a).dot(new Vector(c))],
|
|
["vector.angle", `Vector(${py(a)}).angle(Vector(${py(c)}))`, () => new Vector(a).angle(new Vector(c))],
|
|
["vector.angle_signed", `Vector(${py(a.slice(0, 2))}).angle_signed(Vector(${py(c.slice(0, 2))}))`, () => new Vector(a.slice(0, 2)).angle_signed(new Vector(c.slice(0, 2)))],
|
|
["vector.project", `Vector(${py(a)}).project(Vector(${py(c)}))`, () => new Vector(a).project(new Vector(c))],
|
|
["vector.reflect", `Vector(${py(a)}).reflect(Vector(${py(c)}))`, () => new Vector(a).reflect(new Vector(c))],
|
|
["vector.lerp", `Vector(${py(a)}).lerp(Vector(${py(c)}), ${t})`, () => new Vector(a).lerp(new Vector(c), t)],
|
|
["vector.slerp", `Vector(${py(a)}).slerp(Vector(${py(c)}), ${t}, None)`, () => new Vector(a).slerp(new Vector(c), t, null)],
|
|
["vector.orthogonal", `Vector(${py(a)}).orthogonal()`, () => new Vector(a).orthogonal()],
|
|
["vector.rotation_difference", `Vector(${py(a)}).rotation_difference(Vector(${py(c)}))`, () => new Vector(a).rotation_difference(new Vector(c))],
|
|
["vector.to_track_quat", `Vector(${py(a)}).to_track_quat('-Z', 'Y')`, () => new Vector(a).to_track_quat("-Z", "Y")],
|
|
["vector.to_4d", `Vector(${py(a)}).to_4d()`, () => new Vector(a).to_4d()],
|
|
["vector.length", `Vector(${py(a)}).length`, () => new Vector(a).length],
|
|
["vector.zyx", `Vector(${py(a)}).zyx`, () => new Vector(a).zyx],
|
|
["vector.rotate", `(lambda v: (v.rotate(Euler(${py(e)}, '${ord}')), v)[1])(Vector(${py(a)}))`, () => mut(new Vector(a), (v) => v.rotate(new Euler(e, ord)))],
|
|
["vector @ matrix", `Vector(${py(a)}) @ ${lrs}.to_3x3()`, () => new Vector(a).matmul(LRS().to_3x3())],
|
|
["matrix @ vector", `${lrs} @ Vector(${py(a)})`, () => LRS().matmul(new Vector(a))],
|
|
["matrix.inverted", `${lrs}.inverted()`, () => m((x) => x.inverted())],
|
|
["matrix.transposed", `${lrs}.transposed()`, () => m((x) => x.transposed())],
|
|
["matrix.determinant", `${lrs}.determinant()`, () => m((x) => x.determinant())],
|
|
["matrix.adjugated", `${lrs}.adjugated()`, () => m((x) => x.adjugated())],
|
|
["matrix.normalized", `${lrs}.normalized()`, () => m((x) => x.normalized())],
|
|
["matrix.to_euler", `${lrs}.to_euler('${ord}')`, () => m((x) => x.to_euler(ord))],
|
|
["matrix.to_euler compat", `${lrs}.to_euler('XYZ', Euler((3.0, -3.0, 6.0)))`, () => m((x) => x.to_euler("XYZ", new Euler([3, -3, 6])))],
|
|
["matrix.to_quaternion", `${lrs}.to_quaternion()`, () => m((x) => x.to_quaternion())],
|
|
["matrix.to_scale", `${lrs}.to_scale()`, () => m((x) => x.to_scale())],
|
|
["matrix.decompose", `${lrs}.decompose()`, () => m((x) => x.decompose())],
|
|
["matrix.lerp", `${lrs}.lerp(Matrix.Rotation(${angle}, 4, 'Z'), ${t})`, () => m((x) => x.lerp(Matrix.Rotation(angle, 4, "Z"), t))],
|
|
["matrix @ matrix", `${lrs} @ Matrix.Rotation(${angle}, 4, Vector(${py(c)}))`, () => m((x) => x.matmul(Matrix.Rotation(angle, 4, new Vector(c))))],
|
|
["matrix.median_scale", `${lrs}.median_scale`, () => m((x) => x.median_scale)],
|
|
["matrix.Shear", `Matrix.Shear('XY', 3, (${t}, ${sh}))`, () => Matrix.Shear("XY", 3, [t, sh])],
|
|
["matrix.OrthoProjection", `Matrix.OrthoProjection(Vector(${py(a)}), 4)`, () => Matrix.OrthoProjection(new Vector(a), 4)],
|
|
["matrix.Scale axis", `Matrix.Scale(${t + 0.5}, 4, Vector(${py(c)}))`, () => Matrix.Scale(t + 0.5, 4, new Vector(c))],
|
|
["quaternion.to_euler", `Quaternion(${py(q)}).normalized().to_euler('${ord}')`, () => new Quaternion(q).normalized().to_euler(ord)],
|
|
["quaternion.to_matrix", `Quaternion(${py(q)}).to_matrix()`, () => new Quaternion(q).to_matrix()],
|
|
["quaternion.to_axis_angle", `Quaternion(${py(q)}).to_axis_angle()`, () => new Quaternion(q).to_axis_angle()],
|
|
["quaternion.to_swing_twist", `Quaternion(${py(q)}).normalized().to_swing_twist('Y')`, () => new Quaternion(q).normalized().to_swing_twist("Y")],
|
|
["quaternion.to_exponential_map", `Quaternion(${py(q)}).normalized().to_exponential_map()`, () => new Quaternion(q).normalized().to_exponential_map()],
|
|
["quaternion.slerp", `Quaternion(${py(q)}).normalized().slerp(Euler(${py(e)}).to_quaternion(), ${t})`, () => new Quaternion(q).normalized().slerp(new Euler(e).to_quaternion(), t)],
|
|
["quaternion.rotation_difference", `Quaternion(${py(q)}).normalized().rotation_difference(Euler(${py(e)}).to_quaternion())`, () => new Quaternion(q).normalized().rotation_difference(new Euler(e).to_quaternion())],
|
|
["quaternion @ vector", `Quaternion(${py(q)}) @ Vector(${py(a)})`, () => new Quaternion(q).matmul(new Vector(a))],
|
|
["quaternion @ quaternion", `Quaternion(${py(q)}) @ Euler(${py(e)}).to_quaternion()`, () => new Quaternion(q).matmul(new Euler(e).to_quaternion())],
|
|
["quaternion.inverted", `Quaternion(${py(q)}).inverted()`, () => new Quaternion(q).inverted()],
|
|
["quaternion.cross", `Quaternion(${py(q)}).cross(Quaternion(${py([...a, t])}))`, () => new Quaternion(q).cross(new Quaternion([...a, t]))],
|
|
["quaternion axis angle", `Quaternion(Vector(${py(a)}), ${angle})`, () => new Quaternion(new Vector(a), angle)],
|
|
["quaternion exp map", `Quaternion(${py(a)})`, () => new Quaternion(a)],
|
|
["quaternion.angle", `Quaternion(${py(q)}).normalized().angle`, () => new Quaternion(q).normalized().angle],
|
|
["euler.to_matrix", `Euler(${py(e)}, '${ord}').to_matrix()`, () => new Euler(e, ord).to_matrix()],
|
|
["euler.to_quaternion", `Euler(${py(e)}, '${ord}').to_quaternion()`, () => new Euler(e, ord).to_quaternion()],
|
|
["euler.rotate_axis", `(lambda x: (x.rotate_axis('Y', ${angle}), x)[1])(Euler(${py(e)}, '${ord}'))`, () => mut(new Euler(e, ord), (x) => x.rotate_axis("Y", angle))],
|
|
["euler.make_compatible", `(lambda x: (x.make_compatible(Euler((6.0, -6.0, 12.0))), x)[1])(Euler(${py(e)}))`, () => mut(new Euler(e), (x) => x.make_compatible(new Euler([6, -6, 12])))],
|
|
["euler.rotate", `(lambda x: (x.rotate(Quaternion(${py(q)}).normalized()), x)[1])(Euler(${py(e)}, '${ord}'))`, () => mut(new Euler(e, ord), (x) => x.rotate(new Quaternion(q).normalized()))],
|
|
["color.hsv", `Color(${py(s.map((x) => x / 2))}).hsv`, () => new Color(s.map((x) => x / 2)).hsv],
|
|
["color.to_srgb", `Color(${py(s.map((x) => x / 2))}).from_scene_linear_to_srgb()`, () => new Color(s.map((x) => x / 2)).from_scene_linear_to_srgb()],
|
|
["color.from_srgb", `Color(${py(s.map((x) => x / 2))}).from_srgb_to_scene_linear()`, () => new Color(s.map((x) => x / 2)).from_srgb_to_scene_linear()],
|
|
["color.to_xyz", `Color(${py(s.map((x) => x / 2))}).from_scene_linear_to_xyz_d65()`, () => new Color(s.map((x) => x / 2)).from_scene_linear_to_xyz_d65()],
|
|
];
|
|
}
|
|
|
|
/** A value as plain data to compare: a mathutils value is its class and components. */
|
|
function plain(x: unknown): unknown {
|
|
if (x instanceof Matrix) return { Matrix: x.toJSON() };
|
|
if (x instanceof Euler) return { Euler: [...x], order: x.order };
|
|
if (x instanceof Vector || x instanceof Quaternion || x instanceof Color) return { [x.constructor.name]: [...x] };
|
|
if (Array.isArray(x)) return x.map(plain);
|
|
return x;
|
|
}
|
|
/** Where two values differ beyond rounding (tol, relative for large values), or null. */
|
|
function differ(a: unknown, b: unknown, path = "", tol = 1e-5): string | null {
|
|
if (typeof a === "number" && typeof b === "number") return Math.abs(a - b) <= tol * Math.max(1, Math.abs(a)) ? null : `${path}: ${a} vs ${b}`;
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
if (a.length !== b.length) return `${path}: length ${a.length} vs ${b.length}`;
|
|
for (let i = 0; i < a.length; i++) {
|
|
const d = differ(a[i], b[i], `${path}[${i}]`, tol);
|
|
if (d) return d;
|
|
}
|
|
return null;
|
|
}
|
|
if (a && b && typeof a === "object" && typeof b === "object") {
|
|
const ka = Object.keys(a).sort().join(), kb = Object.keys(b).sort().join();
|
|
if (ka !== kb) return `${path}: ${ka} vs ${kb}`;
|
|
for (const k of Object.keys(a)) {
|
|
const d = differ((a as Record<string, unknown>)[k], (b as Record<string, unknown>)[k], `${path}.${k}`, tol);
|
|
if (d) return d;
|
|
}
|
|
return null;
|
|
}
|
|
return a === b ? null : `${path}: ${JSON.stringify(a)} vs ${JSON.stringify(b)}`;
|
|
}
|
|
|
|
describe.skipIf(!has)("mathutils parity with Blender", () => {
|
|
test("every operation, on 25 seeded random inputs each, agrees with Blender's", async () => {
|
|
const all: Case[] = [];
|
|
for (let seed = 1; seed <= 25; seed++) all.push(...cases(rng(seed)));
|
|
const code = "from mathutils import *\nresult = []\nfor e in exprs:\n try:\n result.append(['ok', eval(e)])\n except Exception as x:\n result.append(['err', type(x).__name__ + ': ' + str(x)])";
|
|
const theirs = await b.py.exec<[string, unknown][]>(code, { exprs: all.map(([, p]) => p) });
|
|
const fails: string[] = [];
|
|
all.forEach(([name, python, local], i) => {
|
|
const [status, value] = theirs[i];
|
|
let mine: unknown, err: string | null = null;
|
|
try {
|
|
mine = local();
|
|
} catch (e) {
|
|
err = String(e);
|
|
}
|
|
if (status === "err" || err) {
|
|
if (!(status === "err" && err)) fails.push(`${name}: Blender ${status === "err" ? value : "ok"}, here ${err ?? "ok"} (${python})`);
|
|
return;
|
|
}
|
|
// Blender converts colours through OpenColorIO, whose processor differs from the exact sRGB curve by ~1e-4
|
|
const d = differ(plain(mine), plain(value), "", name.startsWith("color.to_srgb") || name.startsWith("color.from_srgb") ? 2e-4 : 1e-5);
|
|
if (d) fails.push(`${name}${d} (${python})`);
|
|
});
|
|
expect(fails.slice(0, 20)).toEqual([]);
|
|
expect(all.length).toBeGreaterThan(1000);
|
|
});
|
|
});
|