Skip to main content
Glama
README.md
# lightburn-mcp

Read, render, edit and generate **LightBurn** project files (`.lbrn` / `.lbrn2`) from Python, from the command line, or from any AI agent that speaks the [Model Context Protocol](https://modelcontextprotocol.io).

It grew out of galvo fiber-laser coin work, but the core works for any LightBurn project: CO₂, diode or galvo.

> **Not affiliated with LightBurn Software.** LightBurn is a trademark of LightBurn Software, LLC. This project reads and writes LightBurn's file format for interoperability. It does not drive lasers or talk to the LightBurn application.

![depth map to jig-ready coin project](docs/img/coin_pipeline.png)

## What it does

| | |
|---|---|
| **Inspect** | Device and version info, every layer's cut settings (including sub-layers like 3D-slice cleanup passes), embedded images (pixels, physical mm, rotation, mask), text objects, bounding box |
| **Render** | mm-accurate PNG previews: `fill` approximates the burn (per-layer even-odd fills, images, image masks), `wire` matches LightBurn's thumbnail |
| **Diff** | Compare two versions of a project: changed layer settings, shape counts, swapped images, edited text |
| **Edit** | Change layer settings, swap an embedded image while keeping its placement and mask, add text as outlines |
| **Generate** | Material-test grids (one layer per cell, labelled), and **depth map → coin project** in one step |
| **Round-trip safe** | Edits mutate the XML in place, so anything not understood is preserved byte for byte. Writes never overwrite silently |

### Depth map → coin project

The usual manual flow for a 3D-slice coin is: import the depth map, use LightBurn's image mask to trim it to a circle, apply the mask, scale it to the coin and move it onto the jig. `lbrn coin` / `lbrn_coin_from_depthmap` does all of that:

```mermaid
flowchart LR
    A[depth map PNG] --> B[detect coin disc<br/>least-squares circle fit]
    B --> C{crop}
    C -- rim --> D[trim bright outer rim<br/>at its inner edge]
    C -- edge --> E[keep full disc]
    D --> F[bake circle at source resolution<br/>RGBA, anti-aliased, white under alpha]
    E --> F
    F --> G[scale to jig diameter<br/>centre on jig]
    T[template .lbrn2<br/>layers + jig circle] --> G
    G --> H[jig-ready .lbrn2]
```

The template supplies the 3D-slice layer settings and the jig circle (the locked ellipse). A generic 35 mm template is bundled. Point `--template` at one of your own projects to use your jig and settings.

### Material test grids

![material test grid](docs/img/material_test.png)

LightBurn settings are per layer, so each cell gets its own layer (29 cells max), with row and column labels engraved as outlines on layer 29. Copy device and UI preferences from one of your own projects with `--template`.

## Install

Requires Python ≥ 3.11 and the Cairo library (for previews).

```bash
# system cairo: Arch: pacman -S cairo  ·  Debian/Ubuntu: apt install libcairo2-dev pkg-config  ·  macOS: brew install cairo
pip install git+https://github.com/rdominelli/lightburn-mcp
# or, from a clone:
uv venv && uv pip install -e '.[dev]'
```

This installs two commands: `lightburn-mcp` (the MCP server, stdio) and `lbrn` (the CLI).

Text-to-outlines needs a TTF. It uses `$LBRN_FONT` if set, otherwise the first common bold sans it finds (DejaVu, Liberation, Arial), otherwise `fc-match`.

## Use with an AI agent (MCP)

**Claude Code**
```bash
claude mcp add lightburn -- lightburn-mcp
```

**Claude Desktop / generic MCP client config**
```json
{
  "mcpServers": {
    "lightburn": { "command": "lightburn-mcp" }
  }
}
```

**Hermes Agent**
```bash
hermes mcp add lightburn --command lightburn-mcp
```

There's also an agent skill in [`skills/lightburn/SKILL.md`](skills/lightburn/SKILL.md) that tells the agent how to use the tools well: verify previews, never overwrite, galvo parameter names, file-format gotchas. Install it the way your agent loads skills, e.g. copy the folder to `~/.claude/skills/` (Claude Code) or `~/.hermes/skills/` (Hermes).

### Tools

| Tool | Purpose |
|---|---|
| `lbrn_list` | Find projects in a folder (device, version, size, layer/image counts); skips Syncthing conflict copies |
| `lbrn_inspect` | Full summary of one project |
| `lbrn_render` | PNG preview (returned as an image the model can see) |
| `lbrn_diff` | Compare two projects |
| `lbrn_set_layer` | Change cut settings, e.g. `{"maxPower": 40, "frequency": 45000, "QPulseWidth": 13}`; can create a layer |
| `lbrn_replace_image` | Swap an embedded bitmap, keeping position, rotation and mask |
| `lbrn_add_text` | Add text converted to curves |
| `lbrn_material_test` | Generate a labelled test grid |
| `lbrn_coin_from_depthmap` | Depth map → masked, jig-centred 3D-slice project |
| `lbrn_format_notes` | The file-format notes below, for the agent to read before hand-editing |

Every tool that writes needs either an `output_path` or `overwrite=true`. Overwriting always leaves a timestamped `.bak` next to the original.

## Command line

```bash
lbrn inspect project.lbrn2
lbrn render project.lbrn2 --mode fill -o preview.png
lbrn diff Side1.lbrn2 Side1_v2.lbrn2

# depth map -> coin project on your own jig, with a preview PNG
lbrn coin depthmap.png -o coin.lbrn2 --template my_jig.lbrn2 --preview
lbrn coin depthmap.png --crop edge          # keep the outer rim
lbrn coin depthmap.png --inset-px 6         # trim a little deeper

# 4 x 5 frequency x power grid around a known recipe
lbrn test-grid grid.lbrn2 --rows frequency=30000,40000,50000,60000 --cols maxPower=30,40,50,60,70 \
    --base '{"speed":1500,"QPulseWidth":13,"interval":0.01}' --template my_project.lbrn2
```

### Python

```python
from lbrn.core import Project
from lbrn.render import render

p = Project("Side1.lbrn2")
print(p.summary()["layers"])
p.set_layer_param(0, "numPasses", 120)
p.save("Side1_120passes.lbrn2")
render(p, "preview.png", mode="fill")
```

## Making your own jig template

Any `.lbrn2` works as a `--template`. Open a project that already burns correctly on your jig, delete the artwork, and keep:

- a **locked ellipse** for the jig circle (its centre and diameter set the coin's placement and size)
- an **Image layer** with your 3D-slice settings (and cleanup sub-layer if you use one)

Turn output off on the jig-circle layer unless you actually want it burned.

## File format

LightBurn's format is plain XML but undocumented. [`docs/FORMAT.md`](docs/FORMAT.md) records what this project relies on. The main gotchas:

- coordinates are mm and **Y-up**; `XForm` is a 6-value affine matrix
- `.lbrn2` paths use a compact `VertList`/`PrimList` encoding, and **most paths share topology with an earlier path by `PrimID`**
- embedded bitmaps are stored **bottom-up** (vertically flipped) at a nominal 96 dpi; a bitmap with no data reuses an earlier one with the same `File`
- image masks link by `ShapeID` / `MaskID` / `UsedByID`
- text carries LightBurn's own outlines in `BackupPath`, so previews need no fonts

## Status and limitations

**Alpha.** The reader and renderer were checked against ~100 real galvo projects from LightBurn 1.7 and 2.1. Every file parses, previews match LightBurn's embedded thumbnails, a load-and-save gives identical XML, and every path re-encodes losslessly.

Generated files (depth-map coins, material-test grids) open cleanly in LightBurn 1.7 (tested on Linux). Coin images land at exactly the jig diameter and centre: the crop snaps to whole pixels (≤ 0.5 px shift in the source), and LightBurn's own float32 noise in templates (e.g. `17.499998`) is cleaned on write. Still, frame and test on scrap before a real burn.

Known gaps:
- Editing *existing* text objects isn't supported (their saved outlines would go stale). Add new text as outlines instead.
- Rounded-rectangle corners and per-shape setting overrides aren't rendered or modelled yet.
- Only stored layer settings are handled. Device profiles, `.clb` material libraries and `.lbart` are out of scope for now.
- Previews are approximations. Always frame and test on scrap.

Issues and sample files that break it are very welcome. Please strip anything you don't want public.

## Development

```bash
uv venv && uv pip install -e '.[dev]'
pytest                                            # self-contained tests, synthetic fixtures
LBRN_CORPUS=~/laser/projects pytest tests/test_corpus.py   # optional: regression over your own files
```

`examples/` holds a synthetic depth map and the projects generated from it.

## Prior art

Thanks to [tscircuit/lbrnts](https://github.com/tscircuit/lbrnts) and [OpenBurn](https://github.com/SJLS27/openburn), whose format notes were a useful starting point. This implementation was then checked independently against real files, which corrected several details (prim indices, shared `PrimID` topology, bitmap row order, data-less bitmap reuse).

## License

MIT, see [LICENSE](LICENSE).