diff --git a/surfaces/blender/docs/kitbash.md b/surfaces/blender/docs/kitbash.md index 8ce9c2c..d5c5d00 100644 --- a/surfaces/blender/docs/kitbash.md +++ b/surfaces/blender/docs/kitbash.md @@ -1,12 +1,12 @@ # 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, +`surfaces/blender/brokkr_kitbash` is a Blender add-on that places parametric machine parts from a **kit**, mates +them **socket** to socket, cuts them with booleans and bakes them. 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 +its `__init__.py` lists the eight operators agents may run. Every action writes a JSON report to `brokkr_mesh_polish_result`, returned in the receipt's `hostResult`. Who owns what: @@ -24,8 +24,11 @@ Who owns what: `set_params` and `report` refuse a part whose world transform scales, mirrors or shears, its parents' included: a part's size is its params, and a socket's frame is a rotation and an origin. Set scale to 1 and resize with `set_params`. - -This add-on places and re-parameterises parts. Mating sockets, booleans and baking are not here. +- A connection between two parts belongs to the child: its parent pointer and its `kit_socket`, + `kit_parent_socket` and `kit_roll` properties. Where the child stands is derived from them + ("Assembling parts"). +- A boolean belongs to its Boolean modifier on the part that is cut. `kit_baked` says `apply` replaced the part's + mesh with the evaluated one. ## A part, end to end @@ -83,6 +86,73 @@ be compared when sockets are mated: `equal` (within 1 percent) or `fits` (the ch 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. +## Assembling parts + +**A connection** seats one socket of a child part on one socket of a parent part: the two origins together, the +two normals opposed, and the child turned `roll` degrees about the parent socket's normal (right-handed; at 0 the +two sockets' up axes line up). The child owns it. It is the child's parent pointer, under an identity parent +inverse, and three ID properties on the child: `kit_socket` (its own socket), `kit_parent_socket` and `kit_roll`. +Nothing else stores it: no parent name, no world matrix, no empties. The child's `matrix_basis` is derived, + +```text +matrix_basis = S_parent @ Rz(roll) @ FLIP @ inverse(S_child) FLIP = half a turn about y +``` + +with `S` each socket's frame in its own part at that part's stored params (`core.mate`). `attach` and `set_params` +write it; nothing else should. + +**What may mate** (`core.mate_refusal`): two sockets of one kit and one type whose sizes agree as the type's +`match` says: `equal` within 1 percent of the larger, `fits` with the child at most the parent. `attach` also +refuses, changing nothing: + +- a child that has a parent (`detach` it; `setObjectParent` with an empty parent clears a parent it was not + attached to); +- a parent that is the child, or is below it (a loop); +- an occupied socket: one a child is already attached to, or the one the part itself is attached by; +- a part that is not a kit part, or is scaled in the world. + +A part may carry a child on every socket but the one it hangs by, so assemblies are trees: tank, then a tee on +its port, then a nozzle on each free flange of the tee. + +**`set_params` keeps an assembly together.** Sockets move with params (an actuator's `rod_end` with `extension`, a +tank's `port_b` with `length`). After the rebuild `set_params` writes the derived `matrix_basis` of the part +itself, if it is attached, and of each part attached to it; what hangs further down follows through parenting. +Before it writes anything it checks every one of those connections against the part's new sockets, and a change +that breaks one (a `pipe_r` that no longer matches the flange it is bolted to) is refused, naming the +connection. + +**Drift.** `setObjectTransform` and `setObjectParent` are Brokkr's own rows and can move or re-parent an attached +child. Reports show it: a part's `connection` is `null`, or + +```json +{"parent": "Tee", "socket": "mount", "parentSocket": "c", "roll": 37.0, "residual": 2.1e-07, "holds": true} +``` + +`residual` is the largest entry of the difference between the socket's world frame and the frame its mate asks +for; `holds` is true when the parent is a kit part of the same kit, both sockets exist, the parent inverse is the +identity and the residual is at most 1e-5. The next `set_params` on the child or on its parent (with any param, +even at its current value) seats it again. A child whose parent was cleared or replaced by something that is not +a kit part reads `holds` false with a `null` residual, and `set_params` refuses it: `detach` clears what is left. +`detach` keeps the part where it stands in the world and leaves the parts attached to it attached. + +**`boolean`** adds a Boolean modifier named `Kit Boolean ` to a kit part, above its `Kit Bevel`, so the +cut's edges are bevelled with the rest. The modifier's object is the cutter; the name is a label. The solver is +always Exact with self-intersection on: a kit part is many overlapping closed shells, and the faster solvers +give wrong solids for those (the 1.8 m tank minus a box keeps 0.254 m3 with self-intersection and 0.008 m3 +without). That costs about a second per part each time the part is evaluated (0.3 s for a pipe fitting, 1.5 s +for a tank, on Blender 5.3), so keep live booleans few and `apply` them when the shape is settled. `boolean` +evaluates the part once and reports `evaluated: {faces, volume, nonManifoldEdges}`; `report` never evaluates a +modifier. The cutter is any other mesh object and is not changed: hide it with `setObjectVisibility`. A cutter +with no material leaves the part's material slots as they are; a cutter that has materials brings them, after +the part's own, on the faces it makes. + +**`apply`** bakes a part: its mesh becomes the evaluated mesh (booleans and bevel applied), every modifier goes, +and `kit_baked` is set. The material slots are the part's own, followed by any a cutter brought. A baked part +keeps `kit_kit`, `kit_part`, `kit_params` and its connections: its sockets are still derived from the params, +so what is attached to it stays attached and new parts can attach to it, and it can be cut and baked again. +`set_params` refuses it, since the params no longer make the mesh. `exportGltf` with `applyModifiers` already +exports the evaluated mesh; `apply` is for a baked `.blend`. + ## Expressions Every numeric field of a shape or socket, and `bevel`, is a number or a string over the part's params: @@ -103,19 +173,24 @@ vertices overflow. | --- | --- | --- | | `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. Per-face materials are carried by shape: each shape of the part keeps the material most of its faces had, whatever the new face count. | -| `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. | +| `brokkr_kitbash.set_params` | `object_name`, `params` | Overlays `params` on the stored ones and rebuilds the mesh in the same datablock: modifiers and material slots stay, and the Bevel width follows. Per-face materials are carried by shape: each shape of the part keeps the material most of its faces had, whatever the new face count. Seats the part and the parts attached to it again; refuses a change that breaks a connection, and a baked part. | +| `brokkr_kitbash.report` | `object_name` (default: every kit part) | Each part as the scene has it: kit, part, params, faces, shells, each socket's world frame, `connection` and `baked`. Changes nothing and evaluates no modifier. | +| `brokkr_kitbash.attach` | `object_name` (the child), `socket`, `parent_name`, `parent_socket`, `roll` (degrees, -360 to 360, default 0) | Seats the child's socket on the parent's and parents it there. Reports the child. | +| `brokkr_kitbash.detach` | `object_name` | Clears the part's connection; it stays where it stands. Refuses a part that is not attached. | +| `brokkr_kitbash.boolean` | `object_name`, `cutter`, `operation` (`DIFFERENCE`, `UNION` or `INTERSECT`; default `DIFFERENCE`) | Adds a live Exact Boolean above the Bevel. Refuses a cutter that is the part, is not a mesh, or already cuts it. Reports the part, `booleans: [{cutter, operation}]` and `evaluated`. | +| `brokkr_kitbash.apply` | `object_name` | Bakes the evaluated mesh into the part and clears its modifiers. Refuses a part with no modifiers and a mesh another object shares. Reports the part, `faces`, `volume`, `nonManifoldEdges` and the `cutters` it no longer refers to. | `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 +space for every action that reports a placed part. 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. +Placing a loose part, look, hiding a cutter and export are Brokkr's own rows: `setObjectTransform`, +`assignMaterial`, `setObjectVisibility`, `exportGltf`. `place_part` has no location or colour fields. Move an +assembly by its root: an attached part is placed by its connection. ## A recipe @@ -129,14 +204,23 @@ Placement, look and export are Brokkr's own rows: `setObjectTransform`, `assignM {"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"]} + {"action": "brokkr_kitbash.place_part", "part": "pipe_fitting", "object_name": "Tee", "params": "pipe_r=0.06"}, + {"action": "brokkr_kitbash.attach", "object_name": "Tee", "socket": "a", "parent_name": "Tank", "parent_socket": "port_b"}, + {"action": "brokkr_kitbash.place_part", "part": "nozzle", "object_name": "Nozzle", "params": "throat_r=0.06"}, + {"action": "brokkr_kitbash.attach", "object_name": "Nozzle", "socket": "mount", "parent_name": "Tee", "parent_socket": "c", "roll": 90}, + {"action": "brokkr_kitbash.set_params", "object_name": "Tank", "params": "length=1.8"}, + {"action": "createObject", "objectType": "MESH", "name": "Cutter", "location": [0.3, 0.6, 0], "size": 0.2}, + {"action": "brokkr_kitbash.boolean", "object_name": "Tank", "cutter": "Cutter"}, + {"action": "setObjectVisibility", "targetObjectName": "Cutter", "visible": false}, + {"action": "brokkr_kitbash.apply", "object_name": "Tank"}, + {"action": "brokkr_kitbash.report", "object_name": "Nozzle"}, + {"action": "exportGltf", "filepath": "engine.glb", "selection": ["Tank", "Tee", "Nozzle"]} ]} ``` -The receipts file holds each report in `hostResult`. `tools/turntable_recipe.py` renders the saved `.blend`. +The tank's port is 0.4 of its radius across (0.12 m here), so the tee and the nozzle are given the matching +`pipe_r` and `throat_r`. `set_params` on the tank carries the tee and the nozzle out with its port. The receipts +file holds each report in `hostResult`. `tools/turntable_recipe.py` renders the saved `.blend`. ## The mechanical kit @@ -157,6 +241,13 @@ The receipts file holds each report in `hostResult`. `tools/turntable_recipe.py` `describe` gives the bounds and docs. +Which flanges can meet follows from the sizes: a tank port is `0.4*radius` (0.048 to 0.14), a pipe fitting's +three faces `2*pipe_r` (0.06 to 0.14), a nozzle's mount `2*throat_r` (0.08 to 0.24), and a ribbed cylinder's ends +`2*radius` (0.16 to 0.6). So tanks, fittings and nozzles plumb together, and a ribbed cylinder takes only a +nozzle (throat 0.08 and up) or another ribbed cylinder: its smallest flange is larger than the largest pipe. +Struts and actuators pin together when `0.6*width` equals `0.9*body_r`. Mounts (`fits`) take any part whose +seat is no larger than theirs. + ## Adding a part or a kit Edit the kit's JSON (or add `kits/.v0.json`, whose `kit` field is ``), then run the tests: @@ -170,4 +261,6 @@ python -m unittest discover -s surfaces/blender/tests -p "test_kitbash.py" -v 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`. +`MateTests` covers the mate arithmetic and what may mate. The operators are exercised in a real Blender by +`tests/live/kitbash.py` (placing and re-parameterising) and `tests/live/kitbash_assembly.py` (attach, detach, +`set_params` on assemblies, boolean and apply).