Skip to content

armgit / forma

Public Imported from GitHub
0

A 1.2 MB pure-Rust rewrite of headless Blender built for AI agents — 3D B-rep modeling engine, CLI, and MCP server

8.0 KB
Raw Blame

Using Forma with AI Agents

Forma is a 1.7 MB native (0.8 MB WASM) pure-Rust headless 3D polyhedral modeling engine built so any LLM or coding agent can create, inspect, and export watertight 3D models in milliseconds with minimal token overhead.


1. Connect via MCP (forma-mcp) — Recommended

forma-mcp is a stateful stdio JSON-RPC 2.0 server with 4 tools totaling ~1,000 o200k tokens (including the compact Forma-Pipe syntax spec and advertised topic ops), with full Native ↔ WASM (forma-3d-mcp) parity, automatic zero-token-when-clean lint on eval, and optional --log <path> / FORMA_MCP_LOG JSONL session journaling.

Build the binary once:

cargo build --release

Add to your MCP client config (Claude Code, Cursor, Codex, Antigravity, etc.) — either zero-install via npm (forma-3d) or pointing directly at the compiled native binary:

{
  "mcpServers": {
    "forma": {
      "command": "npx",
      "args": ["-y", "-p", "forma-3d", "forma-3d-mcp"]
    }
  }
}

Also see docs/agent-setup.md for an agent-driven interactive setup runbook and SKILL.md for a compact, drop-in Agent Skill file ready for LLM skill loaders.

Exposed Tools

Tool Purpose
eval Execute transactional Forma-Pipe code (strict: true auto-rolls back any non-manifold result); scalar variables persist across eval calls; delta-first observe() collapses unchanged healthy parts on >2-part incremental edits and emits sync: only when prior renders/exports go stale. Pass out: ["preview.png", "part.glb", ...] (with optional scene, size, view) to build, render, and export in 1 turn.
inspect Inspect scene invariants without mutating state, drill into one part with compiled CAD facts (part: "name" — invariants, sync status, planes, cylindrical bores/bosses, symmetry, min/p01/med wall thickness), run deterministic Layer A craftsmanship lint + pairwise assembly contact graph (lint: true), or load a progressive spec (topic: deform, curves, uv, remesh, scene).
tx O(1) copy-on-write state control: checkpoint, rollback, undo, reset (rolls back both geometry and scalar variables).
export Export to .glb (glTF 2.0 Y-up, PBR + UVs), faceted .step (planar faces), .3mf, .stl, .obj, .svg blueprint, .blend (via Blender bridge), or render a zero-GPU studio .png preview (~20–75 KB in ~2.8 ms). Supports paths: [...] batching, scene: true multi-part export/render, and view: "quad|front|back|right|left|top|iso".

2. Use via Shell / Unix CLI (forma)

If your agent already has a shell tool, you don't even need MCP—pipe scripts directly into forma:

# 1. Read the compact zero-shot language spec (~485 o200k tokens) and optional topics
forma spec
forma spec --topic deform   # also: curves, uv, remesh, scene

# 2. Build, verify watertightness + zero-overhead critique, export CAD/mesh, and render a 4-view preview in ~15 ms
#    (Add --scene for multi-part scripts so ALL named parts export/render together!)
forma eval --strict --critique --scene --out part.glb --render preview.png --draw blueprint.svg <<'EOF'
flange = box(4, 2, 0.4)
  | mat("steel", color=(0.55, 0.68, 0.85), metallic=0.9, roughness=0.25)
  | cut(cyl(0.5, 1))
  | cut(cyl(0.25, 1), at=[(-1.4,-0.5,0), (-1.4,0.5,0), (1.4,-0.5,0), (1.4,0.5,0)])
section(flange, 0.0)
EOF

# 3. Standalone deterministic geometric lint + pairwise assembly contact graph
forma lint --json script.forma

Example compact diagnostic returned to the agent (~85 tokens):

OK [6.92ms] parts=1
- flange: V=265 F=546 WT=ok NM=0 B=0 g=5 vol=2.576 bbox=[-2.00,-1.00,-0.20]..[2.00,1.00,0.20] size=[4.00,2.00,0.40]
  tags: back(2) bottom(196) front(2) left(2) right(2) side(146) top(196)
diff: +flange (vol=2.576)
query: section(part, z=0.000): loops=6 area=6.4393 perim=21.4096 xy=[-2.000,-1.000]..[2.000,1.000] size=[4.000,2.000]

3. Token-Efficient Agent Loop

  1. Batch Into 1 Turn With Fused eval(out=[...]): Write the complete part or assembly parametrically and pass out: ["preview.png", "assembly.glb", "blueprint.svg"] (with scene: true, view: "iso"|"quad") directly to eval to build, render, and export in a single tool call (6.0 -> 1.0 median turns). Use persistent scalars, def name(p1, p2=default) = expr macros, [expr for i in range(n)] list comprehensions, gear(...), and tan/atan/atan2. Prefix temporary helpers with _ (_tmp = ...); use del("name") to remove stale parts.
  2. Verify Invariants, Contacts & Critique: Check WT=ok NM=0 B=0, size=[x,y,z], vol, and Euler genus g in the delta-first diff (on >2-part edits, unchanged healthy parts collapse to one line). Use inspect({ part: "name" }) to drill into one part (-82% tokens) or inspect({ lint: true }) / CLI --critique to detect floating shells, paper-thin walls, grazing gaps, and volumetric part interpenetration (0 extra tokens on clean builds).
  3. Skip Routine Checkpoints & Watch sync: Staleness: eval(strict=true) auto-rolls back on any error or non-manifold result. Only call tx(action="checkpoint", tag="v1") before genuinely exploratory edits. If sync: rev=rN render=rM(stale) export=rM(stale) appears after an edit, re-export before finishing.
  4. Preview Multi-Part Assemblies Cleanly: Render a quad (Front / Right / Top / Iso) or single (iso/front/top) studio preview via eval(out=["preview.png"], scene=true, view="iso") or export(paths=["preview.png", "scene.glb"], scene=true) in ~2.8 ms with zero GPU. scene: true renders all named scene parts together with their materials — do not boolean-merge a dummy assembly just to preview.

4. Zero-Syntax-Error Constrained Decoding (GBNF)

For local or self-hosted inference engines (llama.cpp, vLLM, XGrammar), export the formal GBNF grammar directly from the CLI or repo:

forma spec --gbnf > forma_pipe.gbnf
# or use grammar/forma_pipe.gbnf directly

5. Common Pitfalls & Best Practices

  • Cutting through-holes cleanly: Make cutting cylinders slightly taller than the target plate (e.g., cyl(0.5, 1.0) for a 0.4 thick plate) to avoid grazing coplanar caps.
  • Multi-hole patterns: Pass at=[(x1,y1,z1), ...] or a list comprehension at=[(r*cos(2*pi*i/n), r*sin(2*pi*i/n), 0) for i in range(n)] in a single | cut(...) or | add(...) call—Forma automatically batches pairwise-disjoint booleans in one pass.
  • Meshing spur gears: gear(z, module=m, h=h, bore=...) places tooth 0 on +X; position a mating gear at center distance m * (z1 + z2) / 2 and rotate it around Z by 180 / z2 degrees when z2 is even (0 degrees when z2 is odd).
  • Sharp revolved/mechanical subsurf: Use subsurf(1, crease=true) if you want smooth curvature without shrinking sharp rims.
  • Multi-part scenes & translucency: Always pass --scene (CLI) or scene: true (MCP) when exporting/rendering scripts with multiple named parts — never boolean-merge separate objects just to export them together (--scene supports .glb, .blend, .obj, .3mf, .stl, .step, .png, .svg). For steam, glass, water, or lamps, use mat(..., color=(r,g,b[,a]), alpha=, transmission=, ior=, emissive=(r,g,b), emissive_strength=, double_sided=) (colors are display sRGB and automatically converted to linear RGB in .glb).
  • Exporting for downstream tools: Use .glb for web/Blender/Three.js (glTF 2.0 Y-up, sRGB→linear PBR materials + alphaMode: "BLEND" + KHR_materials_transmission/emissive_strength + LSCM/triplanar UVs; pass --scene / scene: true for multi-part scenes), faceted .step (watertight planar-face ISO 10303-21 solid; supports multi-solid --scene), .3mf/.stl for 3D printing (model in mm? start the script with units("mm") so .3mf/.step declare millimetres; .3mf supports multi-object --scene; .stl/.obj have no unit field and keep model units; .obj supports multi-object o <name> --scene), .svg for dimensioned blueprints (supports --scene / scene: true), and .blend via Blender bridge.