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.
191 lines
8.5 KiB
TypeScript
191 lines
8.5 KiB
TypeScript
// Reaching a Blender: a headless one started for the script (launch), a running one with raea's add-on (live), one at
|
|
// a URL, or the one `raea blender run` started (its native call). Node's modules are taken from
|
|
// process.getBuiltinModule, never imported, so raea's own engine (JavaScriptCore or QuickJS, which have none) still bundles this file.
|
|
import type { Transport } from "./session";
|
|
|
|
type Proc = {
|
|
env: Record<string, string | undefined>;
|
|
pid: number;
|
|
platform: string;
|
|
getBuiltinModule?(name: string): any;
|
|
on?(ev: string, f: (...a: unknown[]) => void): void;
|
|
};
|
|
const proc = (): Proc | undefined => (globalThis as { process?: Proc }).process;
|
|
const node = (name: string): any => proc()?.getBuiltinModule?.(name);
|
|
|
|
export class HttpTransport implements Transport {
|
|
constructor(readonly url: string, readonly token: string) {}
|
|
async send(body: string): Promise<string> {
|
|
const r = await fetch(`${this.url}/rpc`, { method: "POST", headers: { "content-type": "application/json", "x-raea-token": this.token }, body });
|
|
const text = await r.text();
|
|
if (!r.ok) throw new Error(`the bridge answered ${r.status}: ${text}`);
|
|
return text;
|
|
}
|
|
}
|
|
|
|
/** Inside raea's engine (`raea blender run`): the engine posts each batch to the Blender it started. */
|
|
export class NativeTransport implements Transport {
|
|
async send(body: string): Promise<string> {
|
|
const call = (globalThis as { __raea_call?: (n: string, j: string) => string }).__raea_call!;
|
|
return call("blender.post", body);
|
|
}
|
|
}
|
|
export const hasNative = (): boolean => typeof (globalThis as { __raea_call?: unknown }).__raea_call === "function";
|
|
|
|
export interface LaunchOptions {
|
|
/** The Blender to run; else RAEA_BLENDER, `git config --global raea.blender`, raea's download, the usual places, PATH. */
|
|
blender?: string;
|
|
/** A .blend file to open. */
|
|
file?: string;
|
|
/** Blender's factory settings, no add-ons or preferences of the person's (default true). */
|
|
factoryStartup?: boolean;
|
|
/** More arguments for Blender, before --python. */
|
|
args?: string[];
|
|
/** Blender's own output (its prints, warnings) to this process's stderr (default true). */
|
|
echo?: boolean;
|
|
}
|
|
|
|
export interface Launched {
|
|
transport: HttpTransport;
|
|
pid: number;
|
|
/** asks Blender to quit (after the session closed) and waits for it to exit */
|
|
stop(): Promise<void>;
|
|
}
|
|
|
|
function git(args: string[]): string | null {
|
|
try {
|
|
return String(node("node:child_process").execFileSync("git", args, { stdio: ["ignore", "pipe", "ignore"] })).trim() || null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** The Blender raea would run, and where it was found. */
|
|
export function findBlender(): { path: string; from: string } | null {
|
|
const fs = node("node:fs"), path = node("node:path"), os = node("node:os");
|
|
const p = proc();
|
|
if (!fs || !p) return null;
|
|
const is = (f: string | null | undefined): f is string => !!f && fs.existsSync(f) && fs.statSync(f).isFile();
|
|
const win = p.platform === "win32", mac = p.platform === "darwin";
|
|
if (is(p.env.RAEA_BLENDER)) return { path: p.env.RAEA_BLENDER!, from: "RAEA_BLENDER" };
|
|
const configured = git(["config", "--global", "raea.blender"]);
|
|
if (is(configured)) return { path: configured, from: "raea.blender in git config" };
|
|
const own = path.join(os.homedir(), ".raea/tools/blender");
|
|
const ownBin = mac ? path.join(own, "Blender.app/Contents/MacOS/Blender") : path.join(own, win ? "blender.exe" : "blender");
|
|
if (is(ownBin)) return { path: ownBin, from: "raea's download" };
|
|
const places: string[] = [];
|
|
if (mac) places.push("/Applications/Blender.app/Contents/MacOS/Blender", path.join(os.homedir(), "Applications/Blender.app/Contents/MacOS/Blender"));
|
|
if (win) {
|
|
for (const root of [p.env.ProgramFiles, p.env["ProgramFiles(x86)"]].filter(Boolean) as string[]) {
|
|
const base = path.join(root, "Blender Foundation");
|
|
if (!fs.existsSync(base)) continue;
|
|
const dirs = fs.readdirSync(base).filter((d: string) => d.startsWith("Blender")).sort((a: string, b: string) => b.localeCompare(a, undefined, { numeric: true }));
|
|
for (const d of dirs) places.push(path.join(base, d, "blender.exe"));
|
|
}
|
|
}
|
|
if (!mac && !win) places.push("/usr/bin/blender", "/usr/local/bin/blender", "/snap/bin/blender", "/var/lib/flatpak/exports/bin/org.blender.Blender");
|
|
const found = places.find(is);
|
|
if (found) return { path: found, from: "the usual install folder" };
|
|
for (const dir of (p.env.PATH ?? "").split(win ? ";" : ":")) {
|
|
const f = path.join(dir, win ? "blender.exe" : "blender");
|
|
if (is(f)) return { path: f, from: "PATH" };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
const token = (): string => {
|
|
const b = new Uint8Array(16);
|
|
crypto.getRandomValues(b);
|
|
return [...b].map((x) => x.toString(16).padStart(2, "0")).join("");
|
|
};
|
|
|
|
/** The bridge script that ships beside this package's source. */
|
|
export function bridgeScript(): string {
|
|
const url = node("node:url");
|
|
return url.fileURLToPath(new URL("../python/headless.py", import.meta.url));
|
|
}
|
|
|
|
/** Starts a headless Blender serving this process; resolves once its bridge is listening. */
|
|
export async function launch(o: LaunchOptions = {}): Promise<Launched> {
|
|
const cp = node("node:child_process");
|
|
const p = proc();
|
|
if (!cp || !p) throw new Error("raea/blender: starting Blender needs Node, Bun or Deno; inside raea, run the script with `raea blender run SCRIPT.ts`");
|
|
const bin = o.blender ?? findBlender()?.path;
|
|
if (!bin) throw new Error("raea/blender: no Blender found: `raea setup blender` finds or downloads one (or set RAEA_BLENDER)");
|
|
const tok = token();
|
|
const args = ["--background", ...(o.factoryStartup === false ? [] : ["--factory-startup"]), ...(o.file ? [o.file] : []), ...(o.args ?? []), "--python-exit-code", "1", "--python", bridgeScript()];
|
|
const child = cp.spawn(bin, args, {
|
|
env: { ...p.env, RAEA_BLENDER_TOKEN: tok, RAEA_BLENDER_PORT: "0", RAEA_BLENDER_PARENT: String(p.pid) },
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
const echo = o.echo !== false;
|
|
const early: string[] = [];
|
|
let exited: Promise<number | null>;
|
|
const port = await new Promise<number>((resolve, reject) => {
|
|
let ready = false, buf = "";
|
|
exited = new Promise((done) => child.on("exit", (code: number | null) => done(code)));
|
|
child.on("error", (e: Error) => reject(new Error(`raea/blender: couldn't start ${bin}: ${e.message}`)));
|
|
child.stdout.on("data", (d: Uint8Array) => {
|
|
buf += String(d);
|
|
let nl;
|
|
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
const line = buf.slice(0, nl);
|
|
buf = buf.slice(nl + 1);
|
|
const m = /^RAEA_BLENDER_READY (.*)$/.exec(line.trim());
|
|
if (m && !ready) {
|
|
ready = true;
|
|
resolve(JSON.parse(m[1]).port);
|
|
} else if (!ready) early.push(line);
|
|
else if (echo && !/^Blender (quit|\d+\.\d+)/.test(line.trim())) node("node:process").stderr.write(line + "\n");
|
|
}
|
|
});
|
|
child.stderr.on("data", (d: Uint8Array) => {
|
|
if (!ready) early.push(String(d));
|
|
else if (echo) node("node:process").stderr.write(d);
|
|
});
|
|
void exited.then((code) => {
|
|
if (!ready) reject(new Error(`raea/blender: Blender exited (${code}) before its bridge started:\n${early.join("\n").slice(-2000)}`));
|
|
});
|
|
});
|
|
const transport = new HttpTransport(`http://127.0.0.1:${port}`, tok);
|
|
// a script that ends without closing still leaves no Blender behind
|
|
p.on?.("exit", () => {
|
|
try { child.kill(); } catch { /* already gone */ }
|
|
});
|
|
child.unref?.();
|
|
child.stdout.unref?.();
|
|
child.stderr.unref?.();
|
|
return {
|
|
transport,
|
|
pid: child.pid,
|
|
async stop() {
|
|
try {
|
|
await transport.send(JSON.stringify({ sid: "launcher", ops: [{ o: "quit" }] }));
|
|
} catch {
|
|
child.kill();
|
|
}
|
|
const t = setTimeout(() => child.kill(), 10_000);
|
|
await exited;
|
|
clearTimeout(t);
|
|
},
|
|
};
|
|
}
|
|
|
|
/** The Blender running with raea's add-on on this machine (~/.raea/blender/live.json), if one is. */
|
|
export function liveBridge(): HttpTransport | null {
|
|
const fs = node("node:fs"), path = node("node:path"), os = node("node:os");
|
|
const p = proc();
|
|
if (!fs || !p) return null;
|
|
const file = path.join(p.env.RAEA_HOME ?? path.join(os.homedir(), ".raea"), "blender", "live.json");
|
|
if (!fs.existsSync(file)) return null;
|
|
const live = JSON.parse(fs.readFileSync(file, "utf8")) as { port: number; token: string; pid: number };
|
|
try {
|
|
node("node:process").kill(live.pid, 0);
|
|
} catch {
|
|
return null;
|
|
}
|
|
return new HttpTransport(`http://127.0.0.1:${live.port}`, live.token);
|
|
}
|
|
|
|
export const envOf = (name: string): string | undefined => proc()?.env?.[name];
|