lmfilament skill: build Google Filament scenes from LMScript

Use this page to write, run and verify an lmfilament program on the first try. Every command and output below was produced with lmscript 0.2.482 and the released lmfilament 0.3.21 on Linux.

Purpose

lmfilament is a native host for the lmscript _lmcode_ statement that embeds Google Filament 1.78.1: the material compiler, the engine and the geometry tools, in one static executable with no runtime dependency. Inside a block you compile materials, create procedural or OBJ meshes and textures, assemble a scene with lights, a camera and a node hierarchy, drive the Filament render loop and write a preview PNG.

When to use it

Install

npm install -g @lmctl-ai/lmctl
lmscript --version

Nothing else is installed by hand. A block that starts with cli="lmfilament" install="github:lmctl-ai/lmfilament@latest" makes lmscript download the release binary into its tool cache and read the embedded schema from it. The current release lane publishes Linux x64 and arm64 binaries (glibc 2.35 or newer); macOS and Windows are not produced by the current lane.

Commands

CommandUse
lmscript FILE.lmsCompile the block, start the host, return the result envelope to the script
lmscript --check FILE.lmsCompile and type-check the block against the lmfilament schema without running
lmctl autorun FILE.lms or URLRun a script or a raw GitHub example URL, as the repository README does
lmfilament --versionVersion line
lmfilament --lmcode-schema --jsonEmbedded schema JSON
lmfilament --lmcode-signaturesAll callable signatures as text, standard library included
lmfilament --lmcode FILE [FILE ...]The host protocol that run_lmcode drives; rarely typed by hand
lmfilament --version
lmfilament 0.3.21

Inputs and outputs

Safety rules

Worked examples

A vertex-colored triangle with returned facts

This is the repository hellotriangle example with result fields added. It compiles the unlit bakedColor material inline, renders two frames on the noop backend and writes a preview PNG.

mkdir("build");
prog = _lmcode_ cli="lmfilament" install="github:lmctl-ai/lmfilament@latest";
let mat = fil.material("bakedColor", "material {\n    name : BakedColor,\n    requires : [\n        color\n    ],\n    shadingModel : unlit,\n    culling : none,\n    featureLevel : 0\n}\n\nfragment {\n    void material(inout MaterialInputs material) {\n        prepareMaterial(material);\n        material.baseColor = getColor();\n    }\n}\n");
let s = fil.scene();
s.skybox(0.1, 0.125, 0.25);
s.camera(45.0, 0.0, 0.0, 3.0, 0.0, 0.0, 0.0);
let tri = fil.triangle();
s.add(tri, mat);
let frames = s.render_frames(2);
s.render_png("build/triangle.png", 800, 600);
lmcode_return_put("filament", fil.version());
lmcode_return_put("frames", frames);
lmcode_return_put("vertices", tri.vertex_count());
lmcode_return_put("renderables", s.renderable_count());
lmcode_return_put("package_bytes", mat.package_size());
end _lmcode_
r = run_lmcode(prog);
if (!r.ok) { println(r.stderr + r.stdout); }
assert(r.ok);
println(r.result);
lmscript triangle.lms
Using 'soft' CircularBuffer (12288 KiB)
FEngine (64 bits) created at 0x94eb860 (threading is enabled)
FEngine resolved backend: Noop
Backend feature level: 1
FEngine feature level: 1
{"filament":"1.78.1","frames":2,"vertices":3,"renderables":1,"package_bytes":152462}

Lit primitives with a shadow map

From the repository shadows example. The material mat is a lit material with baseColor, metallic and roughness parameters, compiled as above. Parameters are set before each add, add_part returns a Part that can be moved, and shadows(true) turns on the preview shadow map.

let s = fil.scene();
s.skybox(0.55, 0.7, 0.9);
mat.set_float3("baseColor", 0.75, 0.75, 0.72);
s.add_at(fil.plane(14.0, 14.0), mat, 0.0, -1.0, 0.0);
mat.set_float3("baseColor", 0.85, 0.3, 0.25);
s.add_at(fil.sphere(1.0, 48), mat, -2.8, 0.0, 0.0);
mat.set_float3("baseColor", 0.25, 0.6, 0.85);
s.add_part(fil.box(1.8, 1.8, 1.8), mat).translate(-0.4, -0.1, 0.8).rotate(0.0, 25.0, 0.0);
s.shadows(true);
s.directional_light(1.0, 1.0, 1.0, 110000.0, 0.6, -0.55, -0.45);
s.camera(38.0, 0.0, 4.5, 9.5, 0.0, -0.3, 0.0);
s.render_frames(1);
s.render_png("images/shadows.png", 900, 560);

Run a repository example

lmctl autorun https://raw.githubusercontent.com/lmctl-ai/lmfilament/main/examples/ball-stick.lms

Errors and exit codes

ExitPhase and codeMeaning
0noneSuccess, also for --version and the schema flags
2cli, CLI_USAGEUsage error: the host prints the usage line and a failure frame
3load, LOAD_BAD_BYTECODEA compiled program file could not be read
lmfilament --lmcode missing.dslb
LMCODE_RETURN_BEGIN
{"ok":false,"phase":"load","code":"LOAD_BAD_BYTECODE","message":"cannot read missing.dslb"}
LMCODE_RETURN_END

Compile errors come from lmscript before the host starts, with exit 2. Calling a name that is not in the schema looks like this:

lmscript --check t.lms
error:/tmp/lmfil-work/t.lms:11:1: _lmcode_: undefined name 'lmcode_return_put_string' (locals are declared with let; the schema provides: fil; built-ins: float, str, sin, cos, sqrt, abs, floor, pow, min, max, clamp, lerp, ease, ease_in, ease_out, int)

How to verify