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
- A block opens with p = _lmcode_ cli="lmdraw" install="github:lmctl-ai/lmdraw@latest"; and closes with end _lmcode_. r = run_lmcode(p) returns a map with ok and result, or ok false with phase, code, message, line and column.
- The single context object is scene, of type Scene. scene.canvas(width, height, background) or scene.load_svg(path, id) must come before any drawing call.
- Coordinates and sizes are float CSS pixels, x right and y down, origin at the upper left. Text y is the baseline.
- Constructors return Node, Shape, Group, Grid or Gradient handles. set_attr(name, value) takes SVG-style attribute names and string values.
- Outer values enter the block as literals with ${expr}. Integral numbers arrive as int, so pass computed coordinates as strings, for example set_attr("x", ${str(x)}), or as path and points data, or compute them inside the block. Lists cannot cross; index them outside, as in ${names[2]}.
- scene.render_png(path, width, height, scale) writes a PNG of width × scale by height × scale pixels. The path must be a .png file directly under images/ or build/rendered/.
- scene.load_svg reads only a direct child of assets/; scene.load_font reads only a TrueType file directly under assets/fonts/; scene.image takes an embedded PNG data URI.
- A block can return data to the outer script with lmcode_return_set(value) or lmcode_return_put(key, value); it appears as r.result.
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
- Run only scripts you trust. lmscript runs with your full local authority, and the host imposes no instruction budget on a block; only input and image bounds apply.
- Write float arguments with a decimal point (36.0, not 36). Int and float do not mix implicitly; use float(i) for a loop index in float arithmetic and str(value) when an attribute needs a number.
- Keep the canvas within 4096 pixels per side and 8,000,000 pixels, and the render scale between 1 and 4 with scaled output at or below 8,000,000 pixels.
- Create images/ (or build/rendered/) with mkdir before the block when you want to be explicit; never point render_png at a subdirectory, an absolute path or another folder. Symbolic links and path traversal are rejected.
- SVG input must stay in the static subset: no CSS classes or stylesheets, scripts, event handlers, DTDs or entities, SMIL animation, foreignObject, filters, or external URLs and files.
- A new scene.canvas or a load_svg of a different file starts a new scene generation; handles from the earlier scene fail if reused.
- The embedded Noto Sans and Noto Sans Math fonts cover Latin, Greek, Cyrillic, sub and superscripts, arrows and common math symbols. There is no complex-script shaping or right-to-left layout.
- The standard file functions (file_read, file_write and the rest) act on the process working directory without a sandbox. The host performs no network access.
- Test for optional tools before calling them, for example which("ffmpeg") != null, because the outer system call throws when the program is missing.
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
- Measure before placing: scene.measure_text(text, size, bold) returns the pixel width; the six-argument scene.text wraps at a pixel width.
- Arrowheads and dashes: set marker-end to arrow, dot or diamond and stroke-dasharray to a list such as 6 5 on constructed line, path and polyline nodes.
- Several blocks in one host process sharing one scene: b = lmcode_build(["lmdraw", "--lmcode"]); b.add(p, ["$DSLFILE"]); for each block; r = run_lmcode(b). The builder resolves the command name lmdraw on PATH, so the lmdraw executable must be on PATH for this form; otherwise it fails with cannot resolve lmcode command lmdraw. The result then holds one entry per block and a parts count. The repository example truss-bridge.lms uses this.
- The example book and the repository examples folder hold 38 runnable programs.
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 code | Meaning |
|---|---|
| 0 | Success. |
| 2 | Invalid command-line arguments (code CLI_USAGE). |
| 3 | Program file load or verification failure, or a failed host call (for example codes LOAD_BAD_BYTECODE, LOAD_UNBOUND, RUN_HOST). |
| 4 | Another application failure. |
| Message | Fix |
|---|---|
| call scene.canvas or scene.load_svg before drawing | Start the scene first. |
| canvas must fit 1..4096 pixels per side and 8000000 pixels total | Lower the canvas dimensions. |
| render scale must be 1..4 and keep total output at or below 8000000 pixels | Lower 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 path | Place the SVG directly in assets/ and inline its presentation attributes. |
| unsupported node attribute | Use 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
- Run lmscript --check on the script; it must exit 0.
- Run the script and require r.ok; print json_stringify(r) on failure.
- 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.
- When determinism matters, run twice and compare sha256sum output; the same binary writes the same bytes.
- For animation, count the numbered frames, inspect the first, middle and last, and check the encoded video with ffprobe.
- To confirm what the installed host accepts, compare lmdraw --lmcode-signatures with the calls in the block.