docs: kitbash.md, the kit format by example, the four actions and a recipe

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-12-01 14:48:03 +01:00
commit e7f8519d42

View file

@ -0,0 +1,167 @@
# Kitbash parts: machinery from kits
`surfaces/blender/brokkr_kitbash` is a Blender add-on that places parametric machine parts from a **kit** and
reports where their **sockets** are. It is the machinery vocabulary of a ship's fixed chassis: the frame, tanks,
actuators, housings and plumbing an agent assembles offline under a hand-fit armor cage, baked once. It is a host
add-on in Brokkr's sense (`install.md`, "Host add-on actions"), a sibling of `brokkr_mesh_polish` and `brokkr_rig`:
it imports the polish package's operator base, `Refused` and `mesh_object`, and nothing from `brokkr_bridge`.
Enable `brokkr_mesh_polish` first; `brokkr_kitbash` refuses to register without it. `brokkr_actions.json` beside
its `__init__.py` lists the four operators agents may run. Every action writes a JSON report to
`brokkr_mesh_polish_result`, returned in the receipt's `hostResult`.
Who owns what:
- `brokkr_kitbash/core.py` (numpy and the standard library, no Blender) owns the kit format: what a kit document
may say (`check_kit`), what a part's params may be (`resolve_params`), and the geometry and socket frames a part
and its params produce (`build`). There is no other validator of the format.
- `brokkr_kitbash/kits/<name>.v0.json` own kit content. A kit is chosen by name (`a-z`, `0-9`, `_`, at most 32);
the name is the only thing an intent contributes to the path.
- In the scene, an object's `kit_kit`, `kit_part` and `kit_params` ID properties own what that part is. Its mesh is
derived from them: `set_params` rebuilds it, and an edit made to the mesh by hand is lost at the next
`set_params`. Socket frames are derived at every report and stored nowhere.
This add-on places and re-parameterises parts. Mating sockets, booleans and baking are not here.
## A part, end to end
A kit document is `{schema: "brokkr.kit.v0", kit, doc, socketTypes, parts}`. One part of a kit, whole:
```json
{
"name": "standoff",
"doc": "A post on a square foot.",
"params": [
{"name": "height", "type": "number", "min": 0.05, "max": 0.5, "default": 0.2, "doc": "foot face to top face"},
{"name": "radius", "type": "number", "min": 0.01, "max": 0.05, "default": 0.02, "doc": "post radius"},
{"name": "bolts", "type": "integer", "min": 1, "max": 4, "default": 2, "doc": "bolts along x"}
],
"bevel": "0.1*radius",
"shapes": [
{"shape": "box", "size": ["6*radius", "3*radius", "radius"], "at": [0, 0, "0.5*radius"]},
{"shape": "tube", "r0": "radius", "r1": "0.8*radius", "length": "height-0.5*radius", "segments": 24, "smooth": true,
"at": [0, 0, "0.5*radius"]},
{"shape": "repeat", "count": "bolts", "step": ["4*radius/max(bolts-1,1)", 0, 0], "of":
{"shape": "tube", "r0": "0.3*radius", "r1": "0.3*radius", "length": "0.4*radius", "segments": 6,
"at": ["-2*radius", 0, "0.9*radius"]}}
],
"sockets": [
{"name": "foot", "type": "mount", "size": "3*radius", "at": [0, 0, 0], "z": "-z", "y": "+y"},
{"name": "top", "type": "mount", "size": "1.6*radius", "at": [0, 0, "height"], "z": "+z", "y": "+y"}
]
}
```
Lengths are metres in the part's own frame. Unknown keys are refused at every level, and so is a repeated part,
param or socket name, a default outside `[min, max]`, and an integer param with a fractional bound or default.
**Params** are `number` or `integer` with inclusive bounds. A param name is lower case (`a-z`, then `a-z`, `0-9`,
`_`) and is not `min` or `max`.
**Shapes.** Each is one or more closed shells wound outward. `at` (metres) and `rot` (XYZ Euler degrees: about x,
then y, then z, about the shape's own origin) place it, rotation first; both default to zero.
| shape | fields | what it is |
| --- | --- | --- |
| `tube` | `r0`, `r1`, `length`, `segments`, `inner0`?, `inner1`?, `smooth`? | A frustum on +z from radius `r0` at z=0 to `r1` at z=`length`, with n-gon caps. Inner radii above 0 make it hollow (an inner wall and two flat end rings); they are both above 0 or both 0, and below the outer radius. `segments` is 3 to 128. `smooth` shades the walls smooth; caps stay flat. |
| `box` | `size` | A box centred on its origin; every size above 0. |
| `repeat` | `count`, `step`, `of` | `count` copies (1 to 256) of one tube or box at `k * step`, k from 0. A repeat cannot hold a repeat. |
Shells may overlap (a pad that runs into a tank), and a part is then not one solid: reports give the shell count.
**`bevel`** is the width of a Bevel modifier `place_part` adds (2 segments, edges sharper than 30 degrees). Keep it
well under the part's thinnest feature at its smallest params.
**Sockets** are frames. `at` is the origin; `z` is the outward normal (a mating socket faces it), `y` is up (the
roll reference), and x is y cross z; each is one of `+x -x +y -y +z -z` in the part's frame, on different axes.
`size` is a diameter in metres. `type` names one of the kit's `socketTypes`, whose `match` says how sizes are to
be compared when sockets are mated: `equal` (within 1 percent) or `fits` (the child at most the parent). A socket
sits on a flat face of the part whose normal is its `z`; `test_kitbash` checks this for every part of a shipped
kit at its default, smallest and largest params and at seeded draws between.
## Expressions
Every numeric field of a shape or socket, and `bevel`, is a number or a string over the part's params:
```text
+ - * / unary minus ( ) min(a, b) max(a, b) numbers param names
```
An expression is parsed once and walked against that list when the kit is checked; an attribute, subscript,
power, comparison, any other call, a keyword argument, a lambda or a string is refused there, naming the part and
field. Nothing in a kit is run as Python. Division by zero and a `count` or `segments` that is not whole are
refused when the part is built, naming the expression.
## The actions
| action | fields | what it does |
| --- | --- | --- |
| `brokkr_kitbash.describe` | `kit` (default `mechanical`), `part` (default: all) | Parts with their params (type, min, max, default, doc) and sockets in the part frame at default params. Changes nothing. |
| `brokkr_kitbash.place_part` | `kit`, `part`, `object_name`, `params` | A new mesh object at the origin: the part's mesh, a `Kit Bevel` modifier when the part has a bevel, and the three ID properties. Refuses a name an object already has. |
| `brokkr_kitbash.set_params` | `object_name`, `params` | Overlays `params` on the stored ones and rebuilds the mesh in the same datablock: transform, parent, modifiers and material slots stay, and the Bevel width follows. |
| `brokkr_kitbash.report` | `object_name` (default: every kit part) | Each part as the scene has it: kit, part, params, faces, shells, and each socket's world frame. Changes nothing. |
`params` is `name=value; name=value`; a param left out takes the part's default (`place_part`) or keeps its stored
value (`set_params`). A value outside its bounds is refused with the bounds in the message.
A socket in a report is `{name, type, size, matrix}`: `matrix` is the 4x4 as four rows of 6 decimals, in world
space for `place_part`, `set_params` and `report`. Column 2 is the outward normal, column 1 up, column 3 the
origin. A report over 16000 characters (what a receipt carries) is refused with advice to name one part or
object.
Placement, look and export are Brokkr's own rows: `setObjectTransform`, `assignMaterial`, `exportGltf`.
`place_part` has no location or colour fields.
## A recipe
`tools/run_intents.py` runs this in a background Blender (`install.md`, private user directories required):
```json
{"requestedBy": "nibu/hands", "outputDir": "/work/out", "hostAddons": ["brokkr_mesh_polish", "brokkr_kitbash"],
"saveBlend": "/work/out/engine.blend",
"intents": [
{"action": "brokkr_kitbash.describe", "part": "tank"},
{"action": "brokkr_kitbash.place_part", "part": "tank", "object_name": "Tank", "params": "radius=0.3; length=1.4"},
{"action": "setObjectTransform", "targetObjectName": "Tank", "location": [0, 0.6, 0], "rotationEuler": [1.5708, 0, 0]},
{"action": "assignMaterial", "targetObjectName": "Tank", "materialName": "Machinery", "color": [0.35, 0.35, 0.35]},
{"action": "brokkr_kitbash.place_part", "part": "nozzle", "object_name": "Nozzle"},
{"action": "brokkr_kitbash.set_params", "object_name": "Nozzle", "params": "exit_r=0.32"},
{"action": "brokkr_kitbash.report"},
{"action": "exportGltf", "filepath": "engine.glb", "selection": ["Tank", "Nozzle"]}
]}
```
The receipts file holds each report in `hostResult`. `tools/turntable_recipe.py` renders the saved `.blend`.
## The mechanical kit
`kits/mechanical.v0.json`: nine parts, each along +z, with socket types `flange` (equal), `mount` (fits) and
`pivot` (equal).
| part | params | sockets |
| --- | --- | --- |
| `nozzle` | `throat_r`, `exit_r`, `length`, `wall`, `segments` | `mount` (flange) |
| `tank` | `radius`, `length`, `segments` | `port_a`, `port_b` (flange), `saddle` (mount) |
| `ribbed_cylinder` | `radius`, `length`, `ribs`, `rib_depth`, `rib_width`, `segments` | `end_a`, `end_b` (flange) |
| `strut` | `length`, `width`, `thickness`, `segments` | `end_a`, `end_b` (pivot) |
| `actuator` | `body_r`, `body_len`, `rod_r`, `extension`, `segments` | `base`, `rod_end` (pivot); `rod_end` moves with `extension` |
| `pipe_fitting` | `pipe_r`, `run`, `branch`, `flange_r`, `flange_t`, `segments` | `a`, `b`, `c` (flange) |
| `radiator_stack` | `fins`, `fin_w`, `fin_h`, `fin_t`, `pitch`, `segments` | `base` (mount) |
| `vent` | `width`, `height`, `depth`, `slats`, `slat_t`, `tilt` | `back` (mount) |
| `plate` | `width`, `height`, `thickness` | `back`, `front` (mount) |
`describe` gives the bounds and docs.
## Adding a part or a kit
Edit the kit's JSON (or add `kits/<name>.v0.json`, whose `kit` field is `<name>`), then run the tests:
```text
python -m unittest discover -s surfaces/blender/tests -p "test_kitbash.py" -v
```
`MechanicalKitTests` builds every part of `mechanical` at its default, all-minimum and all-maximum params and at
16 seeded draws, and checks that every shell is closed and wound outward, every socket frame is right-handed and
seated on a face that faces its `z`, and no build passes 20000 faces. Choose param ranges so every combination
builds: a `min` or `max` in an expression keeps a derived length positive where two params could cross. A new
part of `mechanical` also joins the part and socket list in `test_the_kit_is_the_nine_parts_and_three_socket_types`.
The operators are exercised in a real Blender by `tests/live/kitbash.py`.