lmdraw script authoring skill

Use lmdraw to create editable 2D scenes and render still images or animation frames through typed lmscript calls. Facts on this page were checked against lmdraw 0.2.7 with lmscript 0.2.482.

Purpose

Write lmscript programs whose _lmcode_ block creates retained vector nodes, labeled diagram shapes and connectors, plot axes, or imported SVG artwork, updates node attributes, and renders PNG images or numbered frame sequences. The outer script computes data and runs the block with run_lmcode.

When to use it

Choose lmdraw for diagrams with explicit geometry (flowcharts, sequence diagrams, state machines, org charts), computed figures, title cards, vector overlays and scripted 2D animation where one scene produces many frames by changing a few attributes. Layout is never automatic: the script positions everything. Use a 3D host such as lmcad when the work depends on solid geometry or perspective, and a plotting host such as lmplot for data-heavy statistical charts. lmdraw writes PNG only; there is no SVG or PDF export.

Install

Install lmctl from npm; it provides lmscript. The install declaration on the block downloads the lmdraw release binary into the lmscript tool cache on first use, or fetch it ahead of time with lmscript install. Release binaries are built for Linux x86-64 and arm64 and need glibc 2.35 or newer. ffmpeg is optional and only needed to encode frames.

npm install -g @lmctl-ai/lmctl
lmscript --version
lmscript install github:lmctl-ai/lmdraw@latest

Commands

lmscript --check scene.lms
lmscript scene.lms
lmctl autorun https://raw.githubusercontent.com/lmctl-ai/lmdraw/main/examples/title-card.lms

lmscript --check compiles every _lmcode_ block against the schema the host reports and exits 2 on a type error without running anything. lmscript scene.lms compiles and runs the script. The host itself answers these protocol flags; lmscript and run_lmcode use them, and you can run them to inspect the installed binary:

lmdraw --version
lmdraw --lmcode-schema
lmdraw --lmcode-schema --json
lmdraw --lmcode-signatures
lmdraw --lmcode FILE.dslb [FILE.dslb ...]

The JSON schema is the compiler input; the form without --json is a compact summary for people. The earlier spellings --schema and --schema --json print the same bytes. run_lmcode writes the compiled program file and invokes --lmcode itself, so scripts do not handle program files.

Inputs and outputs

Scene.canvas(int, int, string) -> Scene
Scene.rect(float, float, float, float, string) -> Node
Scene.circle(float, float, float, string) -> Node
Scene.line(float, float, float, float, string, float) -> Node
Scene.arrow(float, float, float, float, string, float) -> Node
Scene.polyline(string, string, float) -> Node
Scene.path(string, string, string, float) -> Node
Scene.text(float, float, float, string, string) -> Node
Scene.text(float, float, float, string, string, float) -> Node
Scene.box(float, float, float, float, string, string, string, float, float) -> Shape
Scene.diamond(float, float, float, float, string, string, string, float) -> Shape
Scene.connector(Shape, string, Shape, string, string, string, string, string, string, float, string, float, float, float) -> Group
Scene.measure_text(string, float, bool) -> float
Scene.load_svg(string, string) -> Node
Scene.render_png(string, int, int, int) -> void
Node.set_attr(string, string) -> Node

This is a selection of the lines printed by lmdraw --lmcode-schema. The reference documents all 46 host methods with parameter names.

Safety rules

Worked examples

A flowchart from shapes, a grid and connectors

This script was compiled and run with lmdraw 0.2.7; it writes a 600 by 360 PNG.

mkdir("images");
p = _lmcode_ cli="lmdraw" install="github:lmctl-ai/lmdraw@latest";
scene.canvas(600, 360, "#101820");
let slots = scene.grid(60.0, 60.0, 480.0, 240.0, 2, 3, 20.0, 20.0);
let order = scene.box(slots.cell(0, 0), "c", 120.0, 44.0, "order", "#1e3247", "#4a6a88", 8.0, 14.0);
let gate = scene.diamond(300.0, 120.0, 130.0, 70.0, "in stock?", "#1e3247", "#f5c16c", 13.0);
let ship = scene.box(slots.cell(2, 1), "c", 120.0, 44.0, "ship", "#1e3247", "#6fbdb4", 22.0, 14.0);
scene.connector(order, "e", gate, "w", "straight", "none", "arrow", "none", "#8fb4d9", 2.0, "", 0.5, 0.0, 0.0);
scene.connector(gate, "s", ship, "n", "elbow", "none", "arrow", "6 5", "#8fb4d9", 2.0, "yes", 0.4, 8.0, 0.0);
scene.render_png("images/flow.png", 600, 360, 1);
end _lmcode_
r = run_lmcode(p);
require(r.ok, json_stringify(r));
println("wrote images/flow.png");
lmscript flow.lms
wrote images/flow.png

Load an SVG and tween 24 frames

The outer script writes a small SVG under assets/; the block takes a handle to the element with id dot and moves it with lerp and ease, rendering one PNG per frame.

mkdir("images");
mkdir("assets");
svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"320\" height=\"120\" viewBox=\"0 0 320 120\">";
svg = svg + "<rect id=\"plate\" x=\"10\" y=\"10\" width=\"300\" height=\"100\" rx=\"12\" fill=\"#1f344b\"/>";
svg = svg + "<circle id=\"dot\" cx=\"60\" cy=\"60\" r=\"24\" fill=\"#f5c16c\"/></svg>";
write_file("assets/badge.svg", svg);
p = _lmcode_ cli="lmdraw" install="github:lmctl-ai/lmdraw@latest";
let dot = scene.load_svg("assets/badge.svg", "dot");
dot.set_attr("fill", "#6fbdb4");
for (i in range(0, 24)) {
  let t = float(i) / 23.0;
  dot.set_attr("cx", str(lerp(60.0, 260.0, ease(t))));
  scene.render_png("images/badge-" + str(i) + ".png", 320, 120, 1);
}
end _lmcode_
r = run_lmcode(p);
require(r.ok, json_stringify(r));
println("wrote 24 frames");
lmscript tween.lms
wrote 24 frames

Encode the frames with ffmpeg at a fixed frame rate and pixel format:

ffmpeg -framerate 24 -start_number 0 -i 'images/badge-%d.png' -frames:v 24 -c:v libx264 -pix_fmt yuv420p -movflags +faststart badge.mp4

More patterns

Errors and exit codes

A compile error is reported by lmscript with file, line and column, and lmscript --check exits 2 (the script directory is shortened to /work here):

lmscript --check c.lms
error:/work/c.lms:2:12: _lmcode_: method Scene.rect expects float for argument 1; found int

A host refusal does not throw by itself: run_lmcode returns ok false with the phase, code, message and block position, so check r.ok. This is the result for a render path outside the allowed folders:

lmscript bad.lms
dsl line 3:7: PNG output must be a .png file directly under build/rendered/ or images/
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 3:7: PNG output must be a .png file directly under build/rendered/ or images/","line":3,"column":7}
Host exit codeMeaning
0Success.
2Invalid command-line arguments (code CLI_USAGE).
3Program file load or verification failure, or a failed host call (for example codes LOAD_BAD_BYTECODE, LOAD_UNBOUND, RUN_HOST).
4Another application failure.
MessageFix
call scene.canvas or scene.load_svg before drawingStart the scene first.
canvas must fit 1..4096 pixels per side and 8000000 pixels totalLower the canvas dimensions.
render scale must be 1..4 and keep total output at or below 8000000 pixelsLower the scale or the render size.
PNG output must be a .png file directly under build/rendered/ or images/Write to images/name.png or build/rendered/name.png.
load_svg accepts only an assets/*.svg relative pathPlace the SVG directly in assets/ and inline its presentation attributes.
unsupported node attributeUse SVG geometry, fill, stroke, opacity, transform or text attributes; filters are not available.
import ... is not provided by the host (LOAD_UNBOUND)The block calls a standard function newer than the installed host, such as the rest functions on lmdraw 0.2.7. Remove the call or use a host release that includes it.

How to verify

  1. Run lmscript --check on the script; it must exit 0.
  2. Run the script and require r.ok; print json_stringify(r) on failure.
  3. Check each PNG with file or an image viewer: dimensions must equal width × scale by height × scale. Look at the render; text overflow and overlaps are not errors to the host.
  4. When determinism matters, run twice and compare sha256sum output; the same binary writes the same bytes.
  5. For animation, count the numbered frames, inspect the first, middle and last, and check the encoded video with ffprobe.
  6. To confirm what the installed host accepts, compare lmdraw --lmcode-signatures with the calls in the block.