A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server
How to embed forma-3d in JavaScript, Edge, and Browsers
Use this guide to evaluate Forma-Pipe scripts, manage stateful modeling sessions, and export meshes or renders inside Node.js (>=18.0.0), Bun, Deno, Cloudflare Workers, Vercel/Netlify Edge, Fastly Compute, or browsers using the zero-dependency forma-3d npm package.
Install the package
npm install forma-3d
forma-3d has zero runtime dependencies and zero top-level node:* imports. Its package.json exports map automatically selects the right entry point per runtime:
| Runtime | Auto-selected entry | Notes |
|---|---|---|
Cloudflare Workers (workerd) |
lib/workerd.mjs |
Zero boilerplate; statically imports ../wasm/forma.wasm for Wrangler's CompiledWasm rule. |
| Vercel Edge / Netlify Edge / Fastly / Browsers | lib/edge.mjs |
Pure Web-standard loader with zero node:* references; accepts WebAssembly.Module, Response, Uint8Array, or URL. |
| Node.js / Bun / Deno | lib/index.mjs |
Lazy local-file loader for bundled wasm/forma.wasm; zero top-level node:* imports. |
Node.js / TypeScript one-shot usage
Use the top-level helpers when each request builds an independent part:
import { writeFile } from "node:fs/promises";
import { evalPipe, exportMesh, renderPng, drawSvg } from "forma-3d";
const code = `
units("mm")
flange = box(80, 40, 8)
| cut(cyl(10, 20))
| bevel(vertical, 2.5)
| mat("steel", color=(0.55, 0.68, 0.85), metallic=0.9, roughness=0.25)
`;
const obs = await evalPipe(code, { strict: true });
console.log(obs.text);
const glb = await exportMesh(code, "glb", { strict: true });
const threemf = await exportMesh(code, "3mf", { strict: true });
const png = await renderPng(code, 512, { strict: true });
const svg = await drawSvg(code, { strict: true });
await Promise.all([
writeFile("flange.glb", glb),
writeFile("flange.3mf", threemf),
writeFile("flange.png", png),
writeFile("flange.svg", svg),
]);
Supported exportMesh formats are "glb", "step", "stp", "3mf", "stl", "obj", "svg", and "png".
Cloudflare Workers (zero boilerplate)
No nodejs_compat flag or manual .wasm wiring is required. Wrangler resolves forma-3d through the workerd condition to lib/workerd.mjs:
import { exportMesh } from "forma-3d";
export default {
async fetch(req) {
const glb = await exportMesh(await req.text(), "glb", { strict: true });
return new Response(glb, { headers: { "content-type": "model/gltf-binary" } });
},
};
If an isolate ever encounters a WebAssembly.RuntimeError (such as out-of-memory), forma-3d automatically evicts the poisoned WASM instance from its cache so the next request recovers cleanly on a fresh WebAssembly.Instance.
Vercel Edge, Netlify Edge, Fastly, and Browsers
Import the .wasm asset using the .wasm-suffixed subpath (forma-3d/forma.wasm, matching edge bundler rules) or pass a fetch() Response:
// Edge bundler (Vercel Edge / Netlify Edge / Fastly)
import formaWasm from "forma-3d/forma.wasm";
import { exportMesh } from "forma-3d";
const glb = await exportMesh("a = box(1, 1, 1)", "glb", { wasm: formaWasm, strict: true });
// Browser / Web Worker (uses WebAssembly.instantiateStreaming when content-type is application/wasm)
import { loadForma } from "forma-3d/edge";
const engine = await loadForma(fetch("/forma.wasm"));
const session = engine.createSession();
try {
session.eval("part = box(2, 2, 1) | bevel(vertical, 0.2)", { strict: true });
const glb = session.exportBytes("glb");
} finally {
session.free();
}
Run multi-step edits with a stateful FormaSession
When your application applies iterative edits, maintains named checkpoints, or exports multi-part glTF scene graphs, load the WASM engine once with loadForma() and create a FormaSession:
import { writeFile } from "node:fs/promises";
import { loadForma } from "forma-3d";
const engine = await loadForma();
const session = engine.createSession();
try {
session.eval('base = box(4, 4, 0.4) | mat("base", color=(0.25, 0.28, 0.34))', { strict: true });
session.tx("checkpoint", "after_base");
session.eval('post = cyl(0.4, 2.0, z0=0.4) | mat("steel", color=(0.7, 0.72, 0.76), metallic=0.8)', {
strict: true,
});
// Inspect structured invariants for a specific part
const inv = session.invariants("post");
console.log({ watertight: inv.watertight, nonManifold: inv.non_manifold_edges, volume: inv.volume });
// Export all named parts as separate glTF 2.0 nodes
const sceneGlb = session.exportSceneBytes();
await writeFile("assembly.glb", sceneGlb);
// Roll back to the saved checkpoint if needed
session.tx("rollback", "after_base");
} finally {
// Always release the WASM-side session handle
session.free();
}
Always call session.free() inside a finally block to release memory allocated inside the WebAssembly instance. Pass { fresh: true } to loadForma(source, { fresh: true }) if you need an isolated WebAssembly.Instance rather than the cached instance.
Query the compiled spec and GBNF grammar at runtime
import { getSpec, getSpecTopic } from "forma-3d";
const pipeSpec = await getSpec({ dialect: "pipe" });
const gbnfGrammar = await getSpec({ gbnf: true });
const deformDocs = await getSpecTopic("deform");