spine-parts 2.30.2 → 2.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli.ts CHANGED
@@ -877,9 +877,9 @@ usage:
877
877
  non-square inputs use the recorded square transform, square uploads stay
878
878
  unchanged. A selector alone is not installation: model files are checked.
879
879
 
880
- rig-parts comfy plate --mode background|foreground --painting <png>
881
- --mask-from seethrough:<dir>|figure-mask:<dir>|matte:<dir> --out <dir>
882
- [--graph lama|core] [--model <name>] [--checkpoint-sha256 <hex>]
880
+ rig-parts comfy plate --mode background|foreground|occluder --painting <png>
881
+ --mask-from seethrough:<dir>|figure-mask:<dir>|matte:<dir> | --occluder <rgba png> [--holes]
882
+ --out <dir> [--graph lama|core] [--model <name>] [--checkpoint-sha256 <hex>]
883
883
  [--seed 0] [--grow 8] [--steps 30] [--cfg 6] [--prompt <text>] [--negative <text>]
884
884
  [--host <url>] [--wait 1800] [--timeout 3600] [--poll 3]
885
885
  A step-4 plate with a receipt: <out>/plate.png, mask.png and receipt.json
@@ -895,7 +895,10 @@ usage:
895
895
  place), and pastes the result into the painting inside the mask only.
896
896
  --checkpoint-sha256 is required for background and is recorded as declared.
897
897
  foreground runs no box: the painting's pixels inside the mask, transparent
898
- outside.
898
+ outside. occluder runs the background route over the mask an RGBA image of
899
+ what stands in front of the character gives (alpha above 0, on the
900
+ painting's grid, enclosed holes filled with --holes, grown --grow px): the
901
+ painting with the occluder inpainted away, its receipt binding the occluder.
899
902
 
900
903
  rig-parts build --config <config.json> --source <painting.png> --full <dir|psd>
901
904
  [--head <dir|psd>] --out <dir> [--seam near-white|silhouette]
@@ -1181,7 +1184,7 @@ const TAKES = {
1181
1184
  'comfy seethrough': { valued: ['--image', '--out', '--resolution', '--steps', '--seed', '--prefix', '--host', '--wait', '--timeout', '--poll', '--config', '--depth-run'], switches: ['--offload', '--no-offload', '--lama', '--nf4'] },
1182
1185
  'comfy paint': { valued: ['--config', '--out', '--seeds', '--seed0', '--host', '--wait', '--timeout', '--poll'], switches: [] },
1183
1186
  'comfy matte': { valued: ['--painting', '--out', '--node', '--model', '--checkpoint-sha256', '--resolution', '--threshold', '--host', '--wait', '--timeout', '--poll'], switches: [] },
1184
- 'comfy plate': { valued: ['--mode', '--painting', '--mask-from', '--out', '--graph', '--model', '--checkpoint-sha256', '--seed', '--grow', '--steps', '--cfg', '--prompt', '--negative', '--host', '--wait', '--timeout', '--poll'], switches: [] },
1187
+ 'comfy plate': { valued: ['--mode', '--painting', '--mask-from', '--occluder', '--out', '--graph', '--model', '--checkpoint-sha256', '--seed', '--grow', '--steps', '--cfg', '--prompt', '--negative', '--host', '--wait', '--timeout', '--poll'], switches: ['--holes'] },
1185
1188
  inputs: { valued: ['--source', '--config', '--out'], switches: [] },
1186
1189
  build: { valued: ['--config', '--source', '--full', '--head', '--out', '--seam', '--project', '--page-edges', '--pack-shape', '--requirements', '--idle-keys', '--lead-in', '--alt', '--set', '--environment', '--wind-band'], switches: ['--loop', '--seam-pairs', '--keep-stills', '--all-sets', '--no-extremes'] },
1187
1190
  compose: { valued: ['--scene', '--out', '--requirements', '--accept'], switches: [] },
@@ -1989,9 +1992,13 @@ function secondsFlag(v: Map<string, string>, flag: string, fallback: number): nu
1989
1992
  /** Flags `comfy plate --mode foreground` does not take: it runs no box and no model. */
1990
1993
  const PLATE_BOX_FLAGS = ['--graph', '--model', '--checkpoint-sha256', '--seed', '--grow', '--steps', '--cfg', '--prompt', '--negative', '--host', '--wait', '--timeout', '--poll'];
1991
1994
 
1992
- async function cmdComfyPlate(v: Map<string, string>): Promise<number> {
1995
+ async function cmdComfyPlate(v: Map<string, string>, on: Set<string> = new Set()): Promise<number> {
1993
1996
  const mode = v.get('--mode');
1994
- if (mode !== 'background' && mode !== 'foreground') return usage(`comfy plate needs --mode background or --mode foreground${mode === undefined ? '' : `; got "${mode}"`}`);
1997
+ if (mode !== 'background' && mode !== 'foreground' && mode !== 'occluder') return usage(`comfy plate needs --mode background, foreground or occluder${mode === undefined ? '' : `; got "${mode}"`}`);
1998
+ // Issue #1230: the occluder mode reads its mask from --occluder and nothing else; the other modes never read one.
1999
+ if (mode === 'occluder' && v.has('--mask-from')) return usage('comfy plate --mode occluder reads its mask from --occluder; --mask-from is for --mode background or foreground');
2000
+ if (mode === 'occluder' && !v.has('--occluder')) return usage('comfy plate --mode occluder needs --occluder <rgba png>');
2001
+ if (mode !== 'occluder') for (const x of ['--occluder', '--holes']) if (v.has(x) || on.has(x)) return usage(`${x} is for --mode occluder`);
1995
2002
  const painting = v.get('--painting');
1996
2003
  if (painting === undefined) return usage('comfy plate needs --painting <png>');
1997
2004
  const out = v.get('--out');
@@ -2018,11 +2025,11 @@ async function cmdComfyPlate(v: Map<string, string>): Promise<number> {
2018
2025
  for (const x of [seed, grow, steps]) if (typeof x === 'string') return usage(x);
2019
2026
  if (graph === 'lama') for (const x of ['--steps', '--prompt', '--negative']) if (v.has(x)) return usage(`${x} is for --graph core; the lama graph takes no prompt and no steps`);
2020
2027
  const cfg = coreCfg(v.get('--cfg'), graph);
2021
- console.log(`rig-parts comfy plate: background (${graph}) ${painting} -> ${out}`);
2028
+ console.log(`rig-parts comfy plate: ${mode} (${graph}) ${painting} -> ${out}`);
2022
2029
  // Every local refusal (no source mask, the checksum, the grid) comes before the host is resolved.
2023
2030
  const r = await runBackground(
2024
- () => new ComfyClient(resolveHost(v.get('--host'), process.env.COMFY_HOST), { poll: poll as number, request: 30 }),
2025
- { painting, maskFrom: v.get('--mask-from'), out, graph, model: v.get('--model'), checkpointSha256: v.get('--checkpoint-sha256'), seed: seed as number, growPx: grow as number, steps: steps as number, ...(v.has('--cfg') ? { cfg } : {}), prompt: v.get('--prompt'), negative: v.get('--negative'), wait: wait as number, timeout: timeout as number },
2031
+ () => new ComfyClient(resolveHost(v.get('--host'), process.env.COMFY_HOST), { poll: poll as number, request: 30, wait: wait as number }),
2032
+ { mode, painting, maskFrom: v.get('--mask-from'), ...(mode === 'occluder' ? { occluder: v.get('--occluder'), holes: on.has('--holes') } : {}), out, graph, model: v.get('--model'), checkpointSha256: v.get('--checkpoint-sha256'), seed: seed as number, growPx: grow as number, steps: steps as number, ...(v.has('--cfg') ? { cfg } : {}), prompt: v.get('--prompt'), negative: v.get('--negative'), wait: wait as number, timeout: timeout as number },
2026
2033
  (l) => console.log(l),
2027
2034
  );
2028
2035
  console.log(` wrote ${join(out, r.output.path)} (sha256 ${r.output.sha256}), ${r.mask.path} and receipt.json; model ${r.model?.name} (checkpoint sha256 declared), GPU ${r.gpu}`);
@@ -2044,7 +2051,7 @@ async function cmdComfyMatte(v: Map<string, string>): Promise<number> {
2044
2051
  for (const n of [resolution, threshold, wait, timeout, poll]) if (typeof n === 'string') return usage(n);
2045
2052
  try {
2046
2053
  const receipt = await runMatte(
2047
- () => new ComfyClient(resolveHost(v.get('--host'), process.env.COMFY_HOST), { poll: poll as number, request: 30 }),
2054
+ () => new ComfyClient(resolveHost(v.get('--host'), process.env.COMFY_HOST), { poll: poll as number, request: 30, wait: wait as number }),
2048
2055
  { painting, out, node: v.get('--node'), model: v.get('--model'), checkpointSha256: v.get('--checkpoint-sha256'), resolution: resolution as number, threshold: threshold as number, wait: wait as number, timeout: timeout as number },
2049
2056
  line => console.log(line),
2050
2057
  );
@@ -2058,7 +2065,7 @@ async function cmdComfy(args: string[]): Promise<number> {
2058
2065
  if (sub !== 'seethrough' && sub !== 'paint' && sub !== 'plate' && sub !== 'matte') return usage(`comfy takes seethrough, paint, plate or matte; got ${sub === undefined ? 'nothing' : `"${sub}"`}`);
2059
2066
  const f = comfyFlags(rest, `comfy ${sub}`);
2060
2067
  if (typeof f === 'string') return usage(f);
2061
- if (sub === 'plate') return cmdComfyPlate(f.v);
2068
+ if (sub === 'plate') return cmdComfyPlate(f.v, f.on);
2062
2069
  if (sub === 'matte') return cmdComfyMatte(f.v);
2063
2070
  const { v, on } = f;
2064
2071
  if (on.has('--offload') && on.has('--no-offload')) return usage('--offload and --no-offload cannot be combined');
@@ -2081,7 +2088,7 @@ async function cmdComfy(args: string[]): Promise<number> {
2081
2088
  if (sub === 'seethrough' && depthConfig !== undefined && depthRun !== 'full' && depthRun !== 'head') return usage('active config.depth requires --depth-run <full|head>; the source run is not inferred');
2082
2089
  if (sub === 'seethrough' && depthConfig === undefined && depthRun !== undefined) return usage('--depth-run requires --config with an explicit depth block');
2083
2090
  const host = resolveHost(v.get('--host'), process.env.COMFY_HOST);
2084
- const client = new ComfyClient(host, { poll: poll as number, request: 30 });
2091
+ const client = new ComfyClient(host, { poll: poll as number, request: 30, wait: wait as number });
2085
2092
  if (sub === 'seethrough') {
2086
2093
  const image = v.get('--image');
2087
2094
  if (image === undefined) return usage('comfy seethrough needs --image <png>');
package/docs/AUTHORING.md CHANGED
@@ -161,6 +161,7 @@ read by no CPU stage).
161
161
  | `assemble.yield_refused` | assemble | **authored**, optional boolean, default `false` (issue #819; other types: `CONFIG_FIELD_TYPE`). With `true`, an original front projection-core pixel refused at max-channel drift > `DRIFT_LIMIT` (90) can yield when its original source pixel is still opaque. Non-core rim pixels (0 < alpha < `CORE_ALPHA`, 250) on the same 8-connected part body can then yield too (issue #832). Connectivity reads the part support before clearing: the opaque border outside the eroded projection core connects the body to its rim but is not itself newly cleared. A separate rim island without a yielded core seed is unchanged. At every cleared pixel, the actual top-most opaque holder below it (alpha >= 250), after earlier yields, must have colour within 90 of the painting. A deeper matching part cannot bypass an intervening opaque mismatch. This setting also applies to alternate sets. A yielded pixel's alpha becomes 0 and its RGB is not repainted. Accepted own-core pixels in the same connected support within the original yielded core's Gaussian footprint (radius 4 rig pixels), with a matching opaque behind holder, finish to painting RGB while retaining alpha and ownership (issue #841); original refusals, protected samples and replacements are excluded. Away from that footprint projection is unchanged. Merge/ring replacements, samples explicitly kept by `keep_under_occluders`, and pixels outside a derived piece cannot connect a rim or be newly yielded. Front is relative to the matching part behind, so original refusals in another run are eligible even when a different part is the global top holder. The reading proceeds back to front against the current behind pixels; alpha changes do not repaint or create a new matching colour. `yieldReadings` optionally records `finishedProjection` (accepted samples whose RGB changed), and records `yieldedRim` as the rim subset of total `yielded`, skipped core merge/ring/nonopaque/no-match counts and original/final global holders; a yielded pixel also taken by projection closing is omitted from the written `source_px_taken` count. Existing cuts/patches and thresholds are unchanged. The `drift:` line reports `yielded N px` per affected part, even below the drift-body size bar; with the setting off it names `assemble.yield_refused: true` as the conditional remedy. Rebuild and inspect the newly exposed layer in motion before considering any cut; a mismatching or absent behind holder cannot be repaired by this setting. |
162
162
  | `assemble.keep_under_occluders` | assemble | **authored**, optional array of unique source part names from `assemble.plan`, default `[]` (issue #825). For each named part, keep its own See-through RGBA where the front-most covering part in the final draw stack has alpha greater than 0 and below `CORE_ALPHA` (250), including a cover in the other run. Covers at raw alpha <= 8 are ghosts and are not kept under; a ghost-cleaned sample with final alpha 0 is absent cover and does not trigger restoration. Only original opaque own-layer samples are restored after source projection/closing and protected from later seam re-take; merged/rim, cap, under-fill and authored replacement pixels are excluded. This keeps their existing provenance and makes no holes; fully opaque cover and absent cover keep the old path. Cuts and limb pieces inherit the source part's protection. Unknown names: `CONFIG_NAME_RESOLVES`; duplicates: `CONFIG_PART_UNIQUE`; invalid shape: `CONFIG_FIELD_TYPE`. Inspect the part's own amodal layer first: this handle preserves its existing content and cannot complete a missing pixel — in particular, it does not close a motion opening inside the figure (`figure_openings` / [Openings inside the figure on undeclared layer pairs](#openings-inside-the-figure-on-undeclared-layer-pairs-issue-933)) where the lower layer has no opaque core under an opaque upper layer (measured with every `assemble.plan` part named in `keep_under_occluders`: `0` of `1` pair-animation warnings across `demo`'s eight animations closed, with `hair_back\|handwear_r` on `hop` frame `5` staying at `40` cells over `28` allowed in `38x70` world units at `2.517` world units/cell, while `scarf` reads `CLEAR` (`0` warnings) both with and without `keep_under_occluders`; use `assemble.extend_painting` when the lower part lacks opaque source material under an opaque upper cover). Example: `"keep_under_occluders": ["shin_r"]`. |
163
163
  | `assemble.require_hidden_extent` | assemble, build | **authored**, optional array of unique `{part, occluder}` source-name pairs from `assemble.plan` (issue #1047). The distinct occluder must draw after the part. Before cleanup, projection, underfill, cuts or limb splitting, the original selected source is normalized to the rig grid. Own support is alpha >8. Its enclosed holes are `fillHoles(support) \ support`, using four-connected exterior background. Any hole cell under the resolved front-most named occluder (alpha >8) refuses `ASSEMBLE_HIDDEN_EXTENT_ABSENT`. Zero holes with measurable filled-support overlap passes. No overlap or unavailable original ancestry refuses `ASSEMBLE_HIDDEN_EXTENT_UNMEASURED`. A region take's output (`assemble.take`) counts as the selected source. Cover resolves over `assemble.plan` source families only; cut slots are not resolved as cover, which is a stated limit. Omitted or `[]` adds no reading or output. This is a source-support bar reached by build at assemble. See [Original hidden extent required](#original-hidden-extent-required). |
164
+ | `assemble.occluders` | assemble, build | **authored**, optional (issue #1230): `[{image, sha256, under, rgb_within}]` — an RGBA PNG of what stands in front of the character on the painting's grid (`image`, relative to the config's directory; another size refuses `ASSEMBLE_OCCLUDER_GRID`), its bytes' sha256 (`CONFIG_OCCLUDER_SHA256`), the plan parts it stands over (`under`, `CONFIG_NAME_RESOLVES`) and an integer 0–255 (`rgb_within`). After any take and before projection, a pixel of an `under` part's selected layer at alpha above 128, where the occluder's alpha is above 0, within `rgb_within` (max channel) of the painting there, is the occluder's colour copied into the layer: `assemble` prints `WARN ASSEMBLE_OCCLUDER_COLOUR_COPIED` with the part, the rig px count and the first pixel. No output byte moves. |
164
165
  | `assemble.editor_receipts` | assemble, build | **authored**, optional (issue #946): `{receipts: [{file, sha256, from_sha256, editor}], required?, trace_parts?}`. Each receipt records an image edited in an image editor; `file` (relative to the config's directory) must exist and hash to `sha256`, verified at assemble; `from_sha256` and `editor` are the author's declaration, held only to form. With `required: true`, every author-made layer, hidden-art layer, cut `under` and limb `cap` the build reads, alternate sets' runs included, needs a receipt whose `sha256` is its file's. With `trace_parts: true` (issue #947), every part's pixels must trace to a layer of the split or to a receipted image: the receipts are demanded as under `required`, and every shape the config draws through a part — a cut's `polygon`, `seethrough.arm_split`, a limb's `split.line`, a patch's `box`, a painting extension's `band` — is refused (`ASSEMBLE_PART_SOURCE`). Absent, no file is read and no byte moves. See [Editor receipts](#editor-receipts-assembleeditor_receipts-issue-946) and [Part sources](#part-sources-assembleeditor_receiptstrace_parts-issue-947). |
165
166
  | `limbs` | assemble (the cut and the caps), rig (through `regions`), check (`LIMB_ONE_BONE`) | **proposed** by `propose --keypoints` (issue #349), then checked: `{<limb>: {segments: [{part, from, bone}, {part, from, bone, joint, range?, cap?, edge?, cap_shape?, cut_band?, hybrid?}, …], split?, layout?, front?}}`, the upper segment first. The rule is *Limbs: cut at their joints* below the table. `part` is a new part (`CONFIG_PART_NAME`, `CONFIG_PART_UNIQUE` against the plan, patches, cuts and every other limb part); `from` a plan part or a cut's piece (`CONFIG_NAME_RESOLVES`) — the layer the segment's pixels come from, which is cut into the limb parts and is not itself made: a `meshes` or `regions` entry for it is `CONFIG_NAME_RESOLVES`, and the segments of one limb that take one part are consecutive (`CONFIG_LIMB_FROM_RUN`); `bone` a declared bone, each segment's the child of the one before (`CONFIG_LIMB_BONES_CHAIN`); the upper segment names no `joint`, `range`, `cap`, `edge`, `cap_shape` or `cut_band` (`CONFIG_KEY_KNOWN`), every other names its `joint` (`CONFIG_FIELD_PRESENT`) — the joint is its bone's origin; `range` is `[min, max]` degrees from rest with `min <= 0 <= max` (`CONFIG_LIMB_RANGE`), which `assemble` writes into the lower part's `parts.json` record and `check` poses the joint at — its two ends and its middle — reading the seam there (`SEAM_OVER_RANGE`, issue #360; rig-c needs no rotation limit); `cap` a PNG path relative to the config's directory (`CONFIG_FIELD_TYPE`); `edge` `"feather"` (issue #360), the one value it takes, and `cap_shape` `"disc"` (issue #360) or `"ball"` (issue #579) (`CONFIG_FIELD_TYPE` on any other) — the cut's geometry at that joint, *Limbs: cut at their joints* below; absent, the cut is hard and the cap the layer's half-disc of half the limb's width; `cut_band` a whole number of px, 1 or more (`CONFIG_FIELD_TYPE`), on a segment that takes the segment above's part — between two layers nothing is cut, so a band there is `CONFIG_KEY_KNOWN` — places the cut within that many px of the joint line (issue #360, *Limbs: cut at their joints*); absent, the cut is the joint line. Each limb part is attached like any part — `regions.<part>`, which must be the segment's `bone` (`CONFIG_LIMB_BONE`), or a mesh, which `check` holds to that bone alone (`LIMB_ONE_BONE`). With `limbs`, `assemble` reads `bones` too (`CONFIG_FIELD_PRESENT` without it). Absent, nothing is cut at a joint and no byte of any output moves. Optional `front` (issue #664), `{parts: [<own segment part>, …], after: "<produced part>"}`, gives each named segment a second slot `<part>_front`, empty at setup, placed after `after`'s slot family, for a gesture set to draw on (`CONFIG_FRONT_SCOPE`, `CONFIG_FRONT_NAME`, `CONFIG_FRONT_LAYOUT`; *Front slots (issue #664)* below) |
166
167
  | `bones` | rig; `propose --from-config` | **proposed** by `propose`, then corrected. A single bone `{name, parent, at, tip?}` or a chain `{chain, parent, points, tip, link_inherit?}` whose links are named `<chain>0 … <chain>n`. Parents come before children. In `rig.json` each link is **turned along its chain** — `rotation` is the direction from its origin to the next link's (the last link's to `tip`) and `length` that distance (issue #73), so a physics constraint added downstream finds a lever; under `--idle-keys ctl` the link's `<link>_ctl` carries the turn and the length and the link sits at local rotation 0 beneath it. A single bone with a `tip` is turned the same way, towards its tip, with that distance as its length (issue #239); a tip on the bone's own origin is refused (`RIG_TIP_DISTINCT`). The five face bones `eye_r`, `eye_l`, `brow_r`, `brow_l` and `mouth` (by name, when one has no tip of its own) are turned to the eye line — the line through the origins of `eye_r` and `eye_l` as `bones` writes them, read image-left to image-right, drawn when parts.json holds both an `eyewhite-r` and an `eyewhite-l` part — so the blink's `scaley` squashes across the eye line on a tilted head; move an eye bone and the line moves with it. With one eyewhite or none, or an eye bone missing, the rig stage's `face axis:` line says so and they keep the page's axes. A bone under a face bone keeps the face's axis. With the line, `rig.json` also declares `invariants.axes` — one entry, `"face"`, through `eye_r` and `eye_l`, holding the turned face bones (and their controls) — so rig-c's gate holds the build to the line; the line is the one rigc draws through the same two bones, so the tolerance is only the float floor — the largest gap the emitted (float32) bones have to it, rounded up to 6 places plus one 6-place unit — and the `face axis:` line prints it. With the line the rig stage also adds `head_up` (issue #250): a slotless bone under `head`, at head's origin, turned to the eye line + 90 degrees and as long as the eye midpoint lies from head's origin along that axis — the head's frame, which a rule that needs it reads; `head` keeps its own frame, so no key on it or under it changes meaning, and a config bone of that name is refused (`RIG_HEAD_UP_NAME_FREE`). A face bone with its own `tip` is held too (issue #258), in a second entry, `"face_tips"`, when its tip lies along the line within whole-pixel rounding — a tip written in whole pixels sits at most sqrt(2)/2 px off the point of the line it stands for, so its direction may sit up to asin((sqrt(2)/2) / length) degrees off — with its tolerance measured the same way; a tip further off is the bone's own axis, and the `face axis:` line names it with its gap and that bound. A failure is rig-c's `A51_BONES_LIE_ALONG_THEIR_DECLARED_AXIS` line, which names the bone and the fix (rig-c's `docs/AUTHORING.md` §3.7 and §5.2); with no line no entry is written and A51 reports SKIP. Every region on a turned bone is turned back, so the setup pose does not move. Any other single bone keeps world rotation 0; one whose parent is a link is turned back upright. A single bone may declare `inherit` (issue #348): what it takes from its parent's transform, one of rig-c's own mode names exactly as rig-c's table spells them — `normal`, `onlyTranslation`, `noRotationOrReflection`, `noScale`, `noScaleOrReflection` (rig-c's `RigBone.inherit`) — written to that bone in `rig.json` as declared; any other spelling, `"NoScale"` included (the runtime would fold its first letter; nothing here folds or maps), is refused (`CONFIG_BONE_INHERIT`), and a chain takes no `inherit` (`CONFIG_KEY_KNOWN`). A chain may instead declare `link_inherit` (issue #369), an object mapping its own expanded link names to those same modes, for example `"link_inherit": {"leg_r1": "noScale"}`. Only the named link changes; omitted links inherit normally. A key outside this chain is `CONFIG_NAME_RESOLVES`, with this chain's valid link names in the detail; a non-object or non-string mode is `CONFIG_FIELD_TYPE`, and another mode spelling is `CONFIG_BONE_INHERIT`. The mode is forwarded to the expanded link, or to its control under `--idle-keys ctl`, by the same rule as a single bone. This is an authored opt-in: `propose` does not add it. Its setup rotation is the turn from where rig-c's own poser puts the bone with no rotation of its own under that mode, so it still stands upright (or along its tip) at the setup pose. Where the idle gives the bone a `<bone>_ctl`, the mode goes on the control: the control is the bone the parent's rotation reaches, and the bone under it inherits normally, so the idle's keys moved onto the control still turn it. A foot that does not take its shin's rotation (`noRotationOrReflection` or `onlyTranslation`) keeps its shoe level while the leg is posed (`FEET_STAY_LEVEL`, §7); which mode is the author's. `noScale` keeps a shin or shoe's shape under a scaled parent but still takes the parent's rotation: a shoe that must also stay level needs a separate rotation constraint, for example a transform constraint from an unrotated bone. This does not change the proposed feet's existing inheritance or `FEET_STAY_LEVEL` default. An automatic mesh weighted through a bone with a mode other than `normal` gets no derived amplitude (its row names the bone) and no residual envelope (rig-c's `SKINNING_RANGE_UNSUPPORTED`). Absent, `rig.json` carries no `inherit` and no byte moves |
@@ -5343,7 +5344,8 @@ those settings. Changing a config seed alone cannot change a no-config run.
5343
5344
  `rig-parts comfy plate --mode background|foreground --painting <png>
5344
5345
  --mask-from seethrough:<dir>|figure-mask:<dir> --out <dir>` makes the two plates
5345
5346
  a full-screen scene needs: the background with the figure removed, and a
5346
- foreground matte with alpha. Only these two. It never makes a character part
5347
+ foreground matte with alpha. `--mode occluder` (below) makes a third: the
5348
+ painting with a declared occluder removed. It never makes a character part
5347
5349
  or a joint.
5348
5350
 
5349
5351
  - **The mask comes from a source.** `seethrough:<dir>` is a See-through run in
@@ -5375,6 +5377,19 @@ or a joint.
5375
5377
  measured**.
5376
5378
  - **foreground** runs no box and no model. The plate is the painting's pixels
5377
5379
  inside the mask, transparent zero outside.
5380
+ - **occluder** (issue #1230) takes `--occluder <png>` in place of
5381
+ `--mask-from`: an RGBA image of what stands in front of the character, on
5382
+ the painting's own grid (another size is refused, `PLATE_OCCLUDER_GRID`,
5383
+ before anything is uploaded). The mask is the occluder's alpha above 0; with
5384
+ `--holes`, every pocket the occluder encloses (4-connected) is filled; then it
5385
+ is grown by `--grow` and inpainted exactly as **background** is. The plate is
5386
+ the painting with the occluder removed, and `receipt.json` binds the
5387
+ occluder's path and sha256 beside the painting's. A See-through run on that
5388
+ plate paints the surface behind the occluder that no run on the painting
5389
+ paints. To assemble from it, give that run as `--full` with the real painting
5390
+ as `--source`, and take the occluder's own layers from the run on the painting
5391
+ through `config.assemble.take` (their `st_input` is the painting, so the take's
5392
+ input identity holds).
5378
5393
 
5379
5394
  `<out>` holds `plate.png`, `mask.png` (the mask actually used: opaque white
5380
5395
  inside, opaque black outside) and `receipt.json`, spec
@@ -5405,6 +5420,7 @@ recorded transform. The record fields and sampling formula are specified in
5405
5420
  | <a id="PLATE_MASK_SOURCE"></a><a id="PLATE_MASK_GRID"></a><a id="PLATE_MASK_VALUES"></a><a id="PLATE_MASK_EMPTY"></a>`PLATE_MASK_SOURCE`, `PLATE_MASK_GRID`, `PLATE_MASK_VALUES`, `PLATE_MASK_EMPTY` | no `--mask-from`, or a source directory holding no See-through run or no scene figure mask; a source on another grid without a valid recorded `input_transform`; a `figure_rig.png` holding other than opaque white and black; a mask with no figure pixel | run `comfy seethrough` on the original painting to record its input transform, or `assemble` with `config.scene` at the painting grid |
5406
5421
  | <a id="PLATE_MASK_RUN"></a>`PLATE_MASK_RUN` | the figure source has no readable `rig-run/1` assemble `run.json`, or its `figure_rig.png`, `parts.json` or declared `painting_in_rig.png` lacks exactly one matching output hash; or the See-through source has no readable `meta.json` with `image_sha256` and `layers` hashes, or a layer PNG on disk lacks a matching `layers` entry | re-run `assemble` with the source config to regenerate `run.json` and its `rig/` files together, or re-run `comfy seethrough` on the painting to regenerate `meta.json` and `parts/` together |
5407
5422
  | <a id="PLATE_MASK_PAINTING"></a>`PLATE_MASK_PAINTING` | `parts.json` has an invalid `painting_in_rig` declaration, its recorded painting is missing, undecodable or differs from its declared hash, or the supplied painting differs from the recorded opaque RGB; or the supplied painting's SHA-256 differs from a See-through source's `meta.json` `image_sha256` | use the original `--painting` file for this source, or re-run `assemble` or `comfy seethrough` on this painting |
5423
+ | <a id="PLATE_OCCLUDER_PRESENT"></a><a id="PLATE_OCCLUDER_GRID"></a><a id="PLATE_OCCLUDER_ALPHA"></a>`PLATE_OCCLUDER_PRESENT`, `PLATE_OCCLUDER_GRID`, `PLATE_OCCLUDER_ALPHA` | `--mode occluder` with no readable `--occluder` file; an occluder whose size is not the painting's; an occluder with no transparent pixel (it would mask the whole painting) | export the occluder as an RGBA PNG on the painting's grid, transparent outside it |
5408
5424
  | <a id="PLATE_CHECKPOINT_SHA256"></a><a id="PLATE_MODEL_GIVEN"></a>`PLATE_CHECKPOINT_SHA256`, `PLATE_MODEL_GIVEN` | `--checkpoint-sha256` absent or not 64 hex characters; `--graph core` without `--model` | the flag |
5409
5425
  | <a id="PLATE_CORE_CFG"></a>`PLATE_CORE_CFG` | `--cfg` given with `--graph lama`, or not a finite number above 0 | `--cfg`, `--graph` |
5410
5426
  | <a id="PLATE_CORE_GRID"></a>PLATE_CORE_GRID *(retired)* | retired in 2.x: off-grid `core` now pads to the next multiple of 8 and records `grid_transform`; see [STABILITY](STABILITY.md#plate-receipt) | none |
@@ -5474,6 +5490,8 @@ recorded transform. The record fields and sampling formula are specified in
5474
5490
  | <a id="ASSEMBLE_PART_OPAQUE"></a>`ASSEMBLE_PART_OPAQUE` | a part ended with no opaque pixel | drop the entry, or take the tag from the other run |
5475
5491
  | <a id="ASSEMBLE_HIDDEN_EXTENT_ABSENT"></a>`ASSEMBLE_HIDDEN_EXTENT_ABSENT` | a declared `assemble.require_hidden_extent` pair has an enclosed own-source hole beneath its resolved front-most occluder, before cleanup or any writer. The refusal names both source parts, original run:tag, absent/overlap rig-cell counts, soft/opaque cover counts and the first cell | supply a source that already carries the extent, such as a second decomposition run. That source-production route is separate from this change. [Original hidden extent required](#original-hidden-extent-required) states the domain and open-cuff boundary |
5476
5492
  | <a id="ASSEMBLE_HIDDEN_EXTENT_UNMEASURED"></a>`ASSEMBLE_HIDDEN_EXTENT_UNMEASURED` | a declared pair has no filled own-support overlap under the resolved occluder, empty own support, unavailable original selected-layer ancestry or a partial alternate replacing only one declared source family | resolve the original sources and their declared overlap. An empty hole set with measurable overlap is a valid zero reading. See [Original hidden extent required](#original-hidden-extent-required) |
5493
+ | <a id="ASSEMBLE_OCCLUDER_COLOUR_COPIED"></a>`ASSEMBLE_OCCLUDER_COLOUR_COPIED` (WARN) | a selected layer of an `assemble.occluders` `under` part holds pixels (alpha above 128) under the occluder (alpha above 0) within `rgb_within` of the painting: the decomposition painted the thing in front into the surface behind it | take the part from a See-through run on the painting with the occluder removed (`comfy plate --mode occluder`), as `--full`, with the occluder's own layers taken from the painting's run through `assemble.take` |
5494
+ | <a id="CONFIG_OCCLUDER_SHA256"></a><a id="ASSEMBLE_OCCLUDER_GRID"></a>`CONFIG_OCCLUDER_SHA256`, `ASSEMBLE_OCCLUDER_GRID` | an `assemble.occluders` image whose bytes do not hash to its declared `sha256`; an occluder image whose size is not the painting's | declare the bytes the image holds; export the occluder on the painting's grid |
5477
5495
  | <a id="CONFIG_HIDDEN_EXTENT_ORDER"></a>`CONFIG_HIDDEN_EXTENT_ORDER` | `assemble.require_hidden_extent` names the same source as part and occluder, or names an occluder drawn before the part | declare distinct source parts with the occluder after the part in `assemble.plan` |
5478
5496
  | <a id="DIFF_COMPONENTS_INPUT"></a>`DIFF_COMPONENTS_INPUT` | `diff-components` needs readable PNG stack/painting/part images, a writable `--out` target when writing, explicit parts, positive integer floor, a finite threshold in [0,255], matching native dimensions and valid coverage/domain inputs; flags must be unique and known, with nonempty values | Supply the named missing or invalid input; no resampling or inferred figure is performed |
5479
5497
  | <a id="DIFF_COMPONENTS_BINDING"></a>`DIFF_COMPONENTS_BINDING` | The shipped report reader found a stack or painting SHA-256 different from the supplied bytes; found and required digests are printed | Regenerate against the bytes being reviewed |
package/docs/STABILITY.md CHANGED
@@ -214,7 +214,7 @@ defaults are the help's too, and none is guessed from the art.
214
214
  | `inputs` | `--source`, `--config`, `--out` |
215
215
  | `comfy` | `paint`: `--config`, `--out`; optional `--seeds`, `--seed0`, `--host`, `--wait`, `--timeout`, `--poll` |
216
216
  | `comfy` | `matte`: `--painting`, `--out`, `--node` (`BiRefNetRMBG`), `--model` (`BiRefNet-HR-matting`), `--checkpoint-sha256` (declared, required); optional `--resolution`, `--threshold`, `--host`, `--wait`, `--timeout`, `--poll` (issue #1204) |
217
- | `comfy` | `plate`: `--mode` (`background` or `foreground`), `--painting`, `--mask-from` (`seethrough:<dir>`, `figure-mask:<dir>` or `matte:<dir>`), `--out`; with `--mode background` also `--checkpoint-sha256` (required) and optional `--graph` (`lama` or `core`), `--model` (required with `core`), `--seed`, `--grow`, `--steps`, `--cfg`, `--prompt`, `--negative` (the last four with `core` only), `--host`, `--wait`, `--timeout`, `--poll` (issue #1183, additive) |
217
+ | `comfy` | `plate`: `--mode` (`background`, `foreground` or `occluder`), `--painting`, `--mask-from` (`seethrough:<dir>`, `figure-mask:<dir>` or `matte:<dir>`; not with `occluder`), `--out`; with `--mode occluder`, `--occluder` (an RGBA PNG on the painting's grid, required) and optional `--holes` (issue #1230), and the background flags below; with `--mode background` also `--checkpoint-sha256` (required) and optional `--graph` (`lama` or `core`), `--model` (required with `core`), `--seed`, `--grow`, `--steps`, `--cfg`, `--prompt`, `--negative` (the last four with `core` only), `--host`, `--wait`, `--timeout`, `--poll` (issue #1183, additive) |
218
218
  | `comfy` | `seethrough`: `--image`, `--out`; optional `--resolution`, `--steps`, `--seed`, `--prefix`, `--offload`, `--no-offload`, `--lama`, `--nf4`, `--host`, `--wait`, `--timeout`, `--poll`, `--config` (See-through settings and optional depth block), `--depth-run` (`full` or `head`, required with the block) |
219
219
  | `assemble` | `--propose-plan` with `--source`, `--full`, `--config`, optional `--head`, `--write-config` (must name `--config` itself, issue #356); or `--source`, `--full`, `--config`, `--out`, optional `--head` (without it, one run: issue #359), `--seam` (near-white, silhouette), `--project` (core, visible), `--alt` (repeatable `<name>=<source>:<full>[:<head>]`, issue #454), `--requirements` (source-bound cut-material evidence only when the file declares `cut_material`, issue #888) |
220
220
  | `head-crop-preflight` | `--full`, `--canvas`, `--head-box` (JSON coordinate array); read-only advice before the head run, original alpha > 8 samples (issue #1082) |
@@ -314,8 +314,9 @@ part name, `[i]` an entry of a list):
314
314
  - **generation.control.skeleton**, the pose file it names when it is no built-in — required: `width`, `height`, `points`
315
315
  - **seethrough** — required: `resolution`, `steps`, `seed`, `offload`; optional: `head_box`, `arm_split`
316
316
  - **seethrough.arm_split** — required: `order`, and one of `x` or `line` (issue #439)
317
- - **assemble** — required: `rig_scale`, `plan` (from plain `assemble` on); optional: `seam`, `project`, `ground`, `extend_below_crop`, `patches`, `cuts`, `alternates`, `head_face_underfill`, `yield_refused`, `stage_map`, `keep_under_occluders`, `require_hidden_extent`, `extend_painting`, `take`, `editor_receipts`
317
+ - **assemble** — required: `rig_scale`, `plan` (from plain `assemble` on); optional: `seam`, `project`, `ground`, `extend_below_crop`, `patches`, `cuts`, `alternates`, `head_face_underfill`, `yield_refused`, `stage_map`, `keep_under_occluders`, `require_hidden_extent`, `extend_painting`, `take`, `editor_receipts`, `occluders`
318
318
  - **assemble.require_hidden_extent[i]** — required: `part`, `occluder`
319
+ - **assemble.occluders[i]** — required: `image`, `sha256`, `under`, `rgb_within` (issue #1230)
319
320
  - **assemble.alternates[i]** — required: `name`; optional: `source`, `full`, `head`, `parts`, `hides`, `head_box`, `cuts`, `skeleton`, `anchor`, `base_anchor`, `drawn`, `tips`, `front`, `layers`, `focus` (issues #454, #465, #543, #652, #664, #666, #719, #780). `focus` is an authored finite `[x, y]` in painting pixels, within its canvas (edges included), refused as CONFIG_SET_FOCUS before assemble output when invalid; only when present, `parts.json` records it with focus_source_size and `sets.json` emits it in setup-world coordinates through the actual rig resize and `src/coords.ts`. Absent focus emits no field or inferred value; changing/removing it requires reassembly
320
321
  - **assemble.extend_below_crop[i]** — required: `part`, `run`, `tag`; optional: `edges` (left, right, top, bottom; explicit edges require a head-run part and the full-run layer of the same tag)
321
322
  - **assemble.extend_painting[i]** — required: `part`, `band`, `max_distance`; optional: `min_held_alpha`, `order` (issues #848, #854, #855; explicit final layer-derived part, half-open rig band and positive four-neighbour path bound; base painting only). The optional integer floor 1–255 admits exact original alpha-over coverage strictly below it; absent or 1 blocks every nonzero holder. Only selected target samples below the floor may take painting RGBA; target samples at/above the floor and every non-target holder remain exact. By default overlapping reachable claims refuse. With `order` on every entry (unique nonnegative safe integers), lower values claim first and their gains block later candidates and paths; array order never breaks ties or supplies missing precedence
@@ -444,14 +445,15 @@ new spec version. Every path in it is relative to the receipt's directory.
444
445
  | key | meaning |
445
446
  | --- | --- |
446
447
  | `spec` | `"rig-parts-plate-receipt/1"` |
447
- | `mode` | `"background"` or `"foreground"` |
448
+ | `mode` | `"background"`, `"foreground"` or `"occluder"` (issue #1230, additive) |
448
449
  | `painting` | `{ path, sha256 }` of the painting read |
449
- | `mask` | `{ source, from, path, sha256 }`: `source` is `"seethrough"`, `"figure-mask"` or `"matte"`, `from` the source directory, `path` the mask actually used (`mask.png`, opaque white inside) |
450
+ | `occluder` | present only when `mode` is `"occluder"` (issue #1230, additive), right after `painting`: `{ path, sha256 }` of the `--occluder` RGBA image the mask was read from |
451
+ | `mask` | `{ source, from, path, sha256 }`: `source` is `"seethrough"`, `"figure-mask"`, `"matte"` or `"occluder"`, `from` the source directory (the occluder image for `"occluder"`), `path` the mask actually used (`mask.png`, opaque white inside) |
450
452
  | `model` | `{ name, checkpoint_sha256, checkpoint_sha256_from }`, or null for `foreground`. `checkpoint_sha256` is the `--checkpoint-sha256` value, lower-cased; `checkpoint_sha256_from` is `"declared"`: the box reports no hash, so this field is **declared, not measured** |
451
- | `params` | every parameter of the run, the seed included, in a fixed order per graph: `lama` — `seed`, `grow_px`, `mask_alpha_above`; `core` — `seed`, `grow_px`, `steps`, `cfg` (`--cfg`, default `6`), `sampler`, `scheduler`, `denoise`, `prompt`, `negative`, `mask_alpha_above`; `foreground` — `mask_alpha_above` |
453
+ | `params` | every parameter of the run, the seed included, in a fixed order per graph: `lama` — `seed`, `grow_px`, `mask_alpha_above`; `core` — `seed`, `grow_px`, `steps`, `cfg` (`--cfg`, default `6`), `sampler`, `scheduler`, `denoise`, `prompt`, `negative`, `mask_alpha_above`; `foreground` — `mask_alpha_above`; `occluder` — the `lama` or `core` list with `holes` (`--holes`) before `mask_alpha_above`, which is `0` |
452
454
  | `graph` | `"rig-parts/plate-background-lama/1"`, `"rig-parts/plate-background-core/1"`, or the fixed local id `"rig-parts/plate-foreground/1"` |
453
455
  | `gpu` | the device `/system_stats` names (the same seed gives other pixels on another GPU), or null for `foreground` |
454
- | `grid_transform` | present only when `mode` is `"background"`, `graph` is `"rig-parts/plate-background-core/1"`, and the painting's width or height is not a multiple of 8 (issue #1209, additive): `{ source: [width, height], padded: [ceil(width/8)*8, ceil(height/8)*8], pad: [0, 0], pad_fill: "edge-replicate", mask_pad_fill: "zero", inverse: "crop" }` in that key order. Omitted for on-grid `core`, `lama`, and `foreground` |
456
+ | `grid_transform` | present only when `mode` is `"background"` or `"occluder"`, `graph` is `"rig-parts/plate-background-core/1"`, and the painting's width or height is not a multiple of 8 (issue #1209, additive): `{ source: [width, height], padded: [ceil(width/8)*8, ceil(height/8)*8], pad: [0, 0], pad_fill: "edge-replicate", mask_pad_fill: "zero", inverse: "crop" }` in that key order. Omitted for on-grid `core`, `lama`, and `foreground` |
455
457
  | `output` | `{ path, sha256 }` of `plate.png` |
456
458
 
457
459
  The receipt is read back against the bytes on disk before `<out>` is published;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spine-parts",
3
- "version": "2.30.2",
3
+ "version": "2.31.0",
4
4
  "description": "AI-authored 2D character parts from one anime painting and its See-through layer decomposition: measured parts, weighted-mesh rig specs, verified through rig-c before anything is written. A CLI for agents that cannot see the image.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/build.ts CHANGED
@@ -15,6 +15,7 @@ import { CUT_MATERIAL_LINE, CUT_MATERIAL_SOURCES, materialLines } from './cutmat
15
15
  import { ART_JOINT_LINE, artJointLines } from './artjoints.ts';
16
16
  import { applyTakenLayers } from './takelayers.ts';
17
17
  import { applyTakenRegions } from './takeregion.ts';
18
+ import { occluderStageWarnings } from './occluderstage.ts';
18
19
  import { ownershipLines, provenanceLines } from './provenance.ts';
19
20
  import { readSourceDepth } from './depthsheets.ts';
20
21
  import { orderedBaseAnchor } from './baseanchor.ts';
@@ -389,6 +390,11 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
389
390
  const result = assemble({ allocationEvent: input.allocationEvent === undefined ? undefined : event => input.allocationEvent!(event, null), source: src, full: runs.full, head: runs.head, depth: cfg.depth, depthBase: dirname(resolve(input.config)), ...fields, under, ...(declaredFigure === undefined ? {} : { declaredFigure }),
390
391
  ...((cfg.assemble.take ?? []).some(t => 'part' in t) ? { regionTake: { full: takenRuns.full, head: takenRuns.head, targets: (cfg.assemble.take ?? []).flatMap(t => 'part' in t ? [t.part] : []) } } : {}), ...(caps === undefined ? {} : { caps }), seamRule: seam, projectRule: project });
391
392
 
393
+ // Issue #1230: copied occluder colour in a declared occluder's under parts is named, never kept silent; no byte moves.
394
+ if (cfg.assemble.occluders !== undefined) {
395
+ for (const line of occluderStageWarnings({ occluders: cfg.assemble.occluders, configPath: input.config, source: src, runs, plan: fields.plan, resolution: fields.resolution, headBox: fields.headBox, rigScale: fields.rigScale })) log(line);
396
+ }
397
+
392
398
  const sourceCoverSets = [sourceCoverSet(result, null, input.source, read)];
393
399
 
394
400
  // Issue #454: alternate painting sets, assembled before any file is written.
@@ -37,11 +37,19 @@ export function resolveHost(flag: string | undefined, env: string | undefined):
37
37
  return host.replace(/\/+$/, '');
38
38
  }
39
39
 
40
+ export const OBJECT_INFO_REQUEST_FACTOR = 4;
41
+
40
42
  export interface ClientTiming {
41
43
  /** Seconds between two polls of /queue or /history. */
42
44
  poll: number;
43
45
  /** Seconds one plain request may take. */
44
46
  request: number;
47
+ /** Seconds GET /object_info may take (default: 4x `request`, bounded by `wait` when given). */
48
+ objectInfo?: number;
49
+ /** Seconds the run allows before a queued job starts (`--wait`), used to bound `/object_info`. */
50
+ wait?: number;
51
+ /** Milliseconds per nominal second (default: 1000); tests may inject a smaller unit to scale waits down. */
52
+ unitMs?: number;
45
53
  }
46
54
 
47
55
  export interface OutputImage {
@@ -55,7 +63,18 @@ export interface HistoryEntry {
55
63
  status: { status_str?: string; completed?: boolean; messages?: unknown[] } | null;
56
64
  }
57
65
 
58
- const sleep = (s: number): Promise<void> => new Promise((r) => setTimeout(r, s * 1000));
66
+ function isTimeoutError(err: unknown): boolean {
67
+ return err instanceof Error && (err.name === 'TimeoutError' || err.name === 'AbortError');
68
+ }
69
+
70
+ function isTimeoutRefusal(err: unknown): boolean {
71
+ return (
72
+ err instanceof PartsError &&
73
+ err.problems.length === 1 &&
74
+ err.problems[0].code === 'COMFY_REACHABLE' &&
75
+ err.problems[0].detail.startsWith('did not answer within ')
76
+ );
77
+ }
59
78
 
60
79
  function asObject(v: unknown): Record<string, unknown> | null {
61
80
  return typeof v === 'object' && v !== null && !Array.isArray(v) ? (v as Record<string, unknown>) : null;
@@ -72,19 +91,54 @@ export class ComfyClient {
72
91
  readonly timing: ClientTiming,
73
92
  ) {}
74
93
 
94
+ private get unitMs(): number {
95
+ return this.timing.unitMs ?? 1000;
96
+ }
97
+
98
+ private nowSeconds(): number {
99
+ return Date.now() / this.unitMs;
100
+ }
101
+
102
+ private sleepSeconds(s: number): Promise<void> {
103
+ return s <= 0 ? Promise.resolve() : new Promise((r) => setTimeout(r, s * this.unitMs));
104
+ }
105
+
106
+ /**
107
+ * Seconds `GET /object_info` may take: `timing.objectInfo` when set, else
108
+ * four times `timing.request` (120 s at the 30 s default, since a real box's
109
+ * multi-megabyte `/object_info` takes 24.5–36.5 s), bounded by `--wait`.
110
+ */
111
+ objectInfoSeconds(wait?: number): number {
112
+ const cap = this.timing.objectInfo ?? this.timing.request * OBJECT_INFO_REQUEST_FACTOR;
113
+ const bound = wait ?? this.timing.wait;
114
+ return bound === undefined ? cap : Math.min(cap, bound);
115
+ }
116
+
75
117
  private async call(method: 'GET' | 'POST', path: string, body?: string | FormData, headers?: Record<string, string>, seconds = this.timing.request): Promise<Response> {
118
+ const label = Number(seconds.toFixed(1));
119
+ try {
120
+ return await fetch(`${this.host}${path}`, { method, body, headers, signal: AbortSignal.timeout(Math.max(1, seconds * this.unitMs)) });
121
+ } catch (err) {
122
+ const e = err as Error;
123
+ const what = isTimeoutError(e) ? `did not answer within ${label} s` : `could not be reached (${e.message})`;
124
+ return refuse('COMFY_REACHABLE', `${method} ${this.host}${path}`, `${what}; a running ComfyUI at this host is required`);
125
+ }
126
+ }
127
+
128
+ private async readText(res: Response, method: 'GET' | 'POST', path: string, seconds: number): Promise<string> {
76
129
  try {
77
- return await fetch(`${this.host}${path}`, { method, body, headers, signal: AbortSignal.timeout(seconds * 1000) });
130
+ return await res.text();
78
131
  } catch (err) {
79
132
  const e = err as Error;
80
- const what = e.name === 'TimeoutError' ? `did not answer within ${seconds} s` : `could not be reached (${e.message})`;
133
+ const label = Number(seconds.toFixed(1));
134
+ const what = isTimeoutError(e) ? `did not answer within ${label} s` : `could not be reached (${e.message})`;
81
135
  return refuse('COMFY_REACHABLE', `${method} ${this.host}${path}`, `${what}; a running ComfyUI at this host is required`);
82
136
  }
83
137
  }
84
138
 
85
- private async json(method: 'GET' | 'POST', path: string, body?: string | FormData, headers?: Record<string, string>): Promise<unknown> {
86
- const res = await this.call(method, path, body, headers);
87
- const text = await res.text();
139
+ private async json(method: 'GET' | 'POST', path: string, body?: string | FormData, headers?: Record<string, string>, seconds = this.timing.request): Promise<unknown> {
140
+ const res = await this.call(method, path, body, headers, seconds);
141
+ const text = await this.readText(res, method, path, seconds);
88
142
  if (!res.ok) refuse('COMFY_REQUEST_OK', `${method} ${this.host}${path}`, `answered HTTP ${res.status}: ${brief(text)}; HTTP 200 is required`);
89
143
  try {
90
144
  return JSON.parse(text);
@@ -94,8 +148,8 @@ export class ComfyClient {
94
148
  }
95
149
 
96
150
  /** `GET /system_stats`, or a refusal naming the host. */
97
- async systemStats(): Promise<Record<string, unknown>> {
98
- const s = asObject(await this.json('GET', '/system_stats'));
151
+ async systemStats(seconds = this.timing.request): Promise<Record<string, unknown>> {
152
+ const s = asObject(await this.json('GET', '/system_stats', undefined, undefined, seconds));
99
153
  if (s === null) refuse('COMFY_REQUEST_OK', `GET ${this.host}/system_stats`, 'answered with something that is not an object');
100
154
  return s;
101
155
  }
@@ -110,8 +164,8 @@ export class ComfyClient {
110
164
  }
111
165
 
112
166
  /** Free VRAM per device in GB (two decimals), keyed by the device name's first word; empty when the box does not say. */
113
- async vramFree(): Promise<Record<string, number>> {
114
- const s = await this.systemStats();
167
+ async vramFree(seconds = this.timing.request): Promise<Record<string, number>> {
168
+ const s = await this.systemStats(seconds);
115
169
  const out: Record<string, number> = {};
116
170
  const devices = Array.isArray(s.devices) ? s.devices : [];
117
171
  for (const d of devices) {
@@ -122,14 +176,23 @@ export class ComfyClient {
122
176
  return out;
123
177
  }
124
178
 
179
+ private async vramFreeBestEffort(seconds = this.timing.request): Promise<Record<string, number>> {
180
+ try {
181
+ return await this.vramFree(seconds);
182
+ } catch (err) {
183
+ if (isTimeoutRefusal(err)) return {};
184
+ throw err;
185
+ }
186
+ }
187
+
125
188
  /**
126
189
  * Where a queued prompt stands: `running` when the box is running it,
127
190
  * `pending` with its 1-based place among the pending jobs (and how many are
128
191
  * ahead of it, running included), or `absent` when the queue no longer lists
129
192
  * it (finished, or never queued).
130
193
  */
131
- async queuePlace(id: string): Promise<{ state: 'running' } | { state: 'pending'; place: number; pending: number; ahead: number } | { state: 'absent' }> {
132
- const q = asObject(await this.json('GET', '/queue'));
194
+ async queuePlace(id: string, seconds = this.timing.request): Promise<{ state: 'running' } | { state: 'pending'; place: number; pending: number; ahead: number } | { state: 'absent' }> {
195
+ const q = asObject(await this.json('GET', '/queue', undefined, undefined, seconds));
133
196
  if (q === null || !Array.isArray(q.queue_running) || !Array.isArray(q.queue_pending)) {
134
197
  refuse('COMFY_REQUEST_OK', `GET ${this.host}/queue`, 'answered without queue_running and queue_pending arrays');
135
198
  }
@@ -147,37 +210,79 @@ export class ComfyClient {
147
210
  * Wait for a prompt queued behind other jobs (it was appended, never put in
148
211
  * front, and nothing here reorders or cancels anyone's job): at most `wait`
149
212
  * seconds while it is still pending, then at most `timeout` seconds once the
150
- * box is running it. A wait past either bound is refused naming the job's
151
- * place in the queue; the job is left on the box.
213
+ * box is running it. A timed-out `/queue` poll is retried while time remains
214
+ * in the active bound. A wait past either bound is refused naming `prompt <id>`
215
+ * and its place in the queue; the job is left on the box.
152
216
  */
153
217
  async waitBehind(id: string, wait: number, timeout: number, say: (line: string) => void): Promise<{ entry: HistoryEntry; vramLow: Record<string, number>; elapsed: number }> {
154
- const t0 = Date.now();
218
+ const t0 = this.nowSeconds();
155
219
  let started: number | null = null;
156
220
  let lastPlace = -1;
221
+ let lastPending: { place: number; pending: number; ahead: number } | null = null;
222
+ let pollTimeouts = 0;
157
223
  // The minimum free VRAM while this job runs: sampled on every running poll,
158
224
  // then merged with what `waitHistory` reads, so it is not a post-run reading.
159
225
  const vramLow: Record<string, number> = {};
160
226
  for (;;) {
161
- const place = await this.queuePlace(id);
227
+ const phaseStart = started ?? t0;
228
+ const phaseBound = started === null ? wait : timeout;
229
+ const remaining = phaseBound - (this.nowSeconds() - phaseStart);
230
+ const pollCap = this.timing.request * 2 ** pollTimeouts;
231
+ const reqSeconds = remaining > 0 ? Math.min(pollCap, remaining) : Math.min(this.timing.request, Math.max(0.001, phaseBound));
232
+ let place: Awaited<ReturnType<ComfyClient['queuePlace']>>;
233
+ try {
234
+ place = await this.queuePlace(id, reqSeconds);
235
+ pollTimeouts = 0;
236
+ } catch (err) {
237
+ if (!isTimeoutRefusal(err)) throw err;
238
+ pollTimeouts++;
239
+ const now = this.nowSeconds();
240
+ if (started !== null) {
241
+ const runningFor = now - started;
242
+ if (runningFor >= timeout) {
243
+ refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was still running ${runningFor.toFixed(1)} s after it started; it is required within --timeout ${timeout} s (the job was left on the box, not cancelled)`);
244
+ }
245
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, timeout - runningFor)));
246
+ continue;
247
+ }
248
+ const waited = now - t0;
249
+ if (waited >= wait) {
250
+ const where =
251
+ lastPending !== null
252
+ ? `, at place ${lastPending.place} of ${lastPending.pending} pending with ${lastPending.ahead} job(s) ahead of it`
253
+ : ` (GET ${this.host}/queue timed out while waiting)`;
254
+ refuse(
255
+ 'COMFY_QUEUE_BEHIND',
256
+ `prompt ${id}`,
257
+ `was still pending after ${waited.toFixed(1)} s${where}; it is required to start within --wait ${wait} s — the job was left in its place on the box, not moved and not cancelled, and no one else's job was touched`,
258
+ );
259
+ }
260
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, wait - waited)));
261
+ continue;
262
+ }
162
263
  if (place.state === 'absent') {
163
- const done = await this.waitHistory(id, Math.max(this.timing.request, this.timing.poll * 2));
264
+ const spentRunning = started === null ? 0 : this.nowSeconds() - started;
265
+ const historyBudget = Math.max(timeout - spentRunning, Math.min(this.timing.request, timeout), this.timing.poll * 2);
266
+ const done = await this.waitHistory(id, historyBudget, timeout);
164
267
  for (const [k, v] of Object.entries(done.vramLow)) if (!(k in vramLow) || v < vramLow[k]) vramLow[k] = v;
165
- return { entry: done.entry, vramLow, elapsed: (Date.now() - t0) / 1000 };
268
+ return { entry: done.entry, vramLow, elapsed: this.nowSeconds() - t0 };
166
269
  }
167
- const now = Date.now();
270
+ const now = this.nowSeconds();
168
271
  if (place.state === 'running') {
169
- for (const [k, v] of Object.entries(await this.vramFree())) if (!(k in vramLow) || v < vramLow[k]) vramLow[k] = v;
272
+ for (const [k, v] of Object.entries(await this.vramFreeBestEffort())) if (!(k in vramLow) || v < vramLow[k]) vramLow[k] = v;
170
273
  if (started === null) {
171
274
  started = now;
172
275
  say(` prompt ${id} is running`);
173
276
  }
174
- if ((now - started) / 1000 >= timeout) {
175
- refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was still running ${((now - started) / 1000).toFixed(1)} s after it started; it is required within --timeout ${timeout} s (the job was left on the box, not cancelled)`);
277
+ if (now - started >= timeout) {
278
+ refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was still running ${(now - started).toFixed(1)} s after it started; it is required within --timeout ${timeout} s (the job was left on the box, not cancelled)`);
176
279
  }
280
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, timeout - (this.nowSeconds() - started))));
177
281
  } else {
282
+ lastPending = place;
178
283
  if (place.place !== lastPlace) say(` prompt ${id} queued behind ${place.ahead} job(s) (pending place ${place.place} of ${place.pending})`);
179
284
  lastPlace = place.place;
180
- const waited = (now - t0) / 1000;
285
+ const waited = now - t0;
181
286
  if (waited >= wait) {
182
287
  refuse(
183
288
  'COMFY_QUEUE_BEHIND',
@@ -185,14 +290,14 @@ export class ComfyClient {
185
290
  `was still pending after ${waited.toFixed(1)} s, at place ${place.place} of ${place.pending} pending with ${place.ahead} job(s) ahead of it; it is required to start within --wait ${wait} s — the job was left in its place on the box, not moved and not cancelled, and no one else's job was touched`,
186
291
  );
187
292
  }
293
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, wait - (this.nowSeconds() - t0))));
188
294
  }
189
- await sleep(this.timing.poll);
190
295
  }
191
296
  }
192
297
 
193
298
  /** `GET /object_info`: every node class the box has, with its inputs. */
194
- async objectInfo(): Promise<Record<string, unknown>> {
195
- const o = asObject(await this.json('GET', '/object_info'));
299
+ async objectInfo(wait?: number): Promise<Record<string, unknown>> {
300
+ const o = asObject(await this.json('GET', '/object_info', undefined, undefined, this.objectInfoSeconds(wait)));
196
301
  if (o === null) refuse('COMFY_REQUEST_OK', `GET ${this.host}/object_info`, 'answered with something that is not an object of node classes');
197
302
  return o;
198
303
  }
@@ -212,7 +317,7 @@ export class ComfyClient {
212
317
  /** `POST /prompt`; returns the prompt id. A rejection is refused with the box's own error text. */
213
318
  async submit(graph: unknown, clientId: string): Promise<string> {
214
319
  const res = await this.call('POST', '/prompt', JSON.stringify({ prompt: graph, client_id: clientId }), { 'Content-Type': 'application/json' });
215
- const text = await res.text();
320
+ const text = await this.readText(res, 'POST', '/prompt', this.timing.request);
216
321
  if (!res.ok) refuse('COMFY_PROMPT_ACCEPTED', `POST ${this.host}/prompt`, `rejected the graph with HTTP ${res.status}: ${brief(text, 1500)}`);
217
322
  let id: unknown;
218
323
  try {
@@ -226,24 +331,42 @@ export class ComfyClient {
226
331
 
227
332
  /**
228
333
  * Poll `/history/<id>` until the job is there, for at most `timeout`
229
- * seconds, and check the box is still answering between polls. Returns the
230
- * entry and the lowest free VRAM seen per device. An entry whose status is
231
- * an error is refused with the box's messages; so is an entry with no
232
- * outputs at all, because a finished job that saved nothing is not a result.
334
+ * seconds, and check the box is still answering between polls. A timed-out
335
+ * poll is retried while time remains in `timeout`. Returns the entry and the
336
+ * lowest free VRAM seen per device. An entry whose status is an error is
337
+ * refused with the box's messages; so is an entry with no outputs at all,
338
+ * because a finished job that saved nothing is not a result.
233
339
  */
234
- async waitHistory(id: string, timeout: number): Promise<{ entry: HistoryEntry; vramLow: Record<string, number>; elapsed: number }> {
235
- const t0 = Date.now();
340
+ async waitHistory(id: string, timeout: number, reportedTimeout = timeout): Promise<{ entry: HistoryEntry; vramLow: Record<string, number>; elapsed: number }> {
341
+ const t0 = this.nowSeconds();
236
342
  const vramLow: Record<string, number> = {};
343
+ let pollTimeouts = 0;
237
344
  for (;;) {
238
- for (const [k, v] of Object.entries(await this.vramFree())) if (!(k in vramLow) || v < vramLow[k]) vramLow[k] = v;
239
- const h = asObject(await this.json('GET', `/history/${encodeURIComponent(id)}`));
345
+ const remaining = timeout - (this.nowSeconds() - t0);
346
+ const pollCap = this.timing.request * 2 ** pollTimeouts;
347
+ const reqSeconds = remaining > 0 ? Math.min(pollCap, remaining) : Math.min(this.timing.request, Math.max(0.001, timeout));
348
+ for (const [k, v] of Object.entries(await this.vramFreeBestEffort(reqSeconds))) if (!(k in vramLow) || v < vramLow[k]) vramLow[k] = v;
349
+ let h: Record<string, unknown> | null;
350
+ try {
351
+ h = asObject(await this.json('GET', `/history/${encodeURIComponent(id)}`, undefined, undefined, reqSeconds));
352
+ pollTimeouts = 0;
353
+ } catch (err) {
354
+ if (!isTimeoutRefusal(err)) throw err;
355
+ pollTimeouts++;
356
+ const waited = this.nowSeconds() - t0;
357
+ if (waited >= timeout) {
358
+ refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was not in /history after ${waited.toFixed(1)} s; it is required within --timeout ${reportedTimeout} s (the job was left on the box, not cancelled)`);
359
+ }
360
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, timeout - waited)));
361
+ continue;
362
+ }
240
363
  const raw = h === null ? null : asObject(h[id]);
241
364
  if (raw !== null) {
242
365
  const entry: HistoryEntry = {
243
366
  outputs: (asObject(raw.outputs) ?? {}) as HistoryEntry['outputs'],
244
367
  status: asObject(raw.status) as HistoryEntry['status'],
245
368
  };
246
- const elapsed = (Date.now() - t0) / 1000;
369
+ const elapsed = this.nowSeconds() - t0;
247
370
  if (entry.status?.status_str === 'error') {
248
371
  refuse('COMFY_RUN_OK', `prompt ${id}`, `ended with status "error": ${brief(JSON.stringify(entry.status.messages ?? []), 2000)}`);
249
372
  }
@@ -256,11 +379,11 @@ export class ComfyClient {
256
379
  }
257
380
  return { entry, vramLow, elapsed };
258
381
  }
259
- const waited = (Date.now() - t0) / 1000;
382
+ const waited = this.nowSeconds() - t0;
260
383
  if (waited >= timeout) {
261
- refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was not in /history after ${waited.toFixed(1)} s; it is required within --timeout ${timeout} s (the job was left on the box, not cancelled)`);
384
+ refuse('COMFY_HISTORY_WITHIN', `prompt ${id}`, `was not in /history after ${waited.toFixed(1)} s; it is required within --timeout ${reportedTimeout} s (the job was left on the box, not cancelled)`);
262
385
  }
263
- await sleep(Math.min(this.timing.poll, Math.max(0, timeout - waited)));
386
+ await this.sleepSeconds(Math.min(this.timing.poll, Math.max(0, timeout - waited)));
264
387
  }
265
388
  }
266
389
 
@@ -30,7 +30,7 @@ export async function runMatte(connect: () => ComfyClient, run: MatteRun, say: (
30
30
  const client = connect();
31
31
  const gpu = gpuName(await client.systemStats(), client.host);
32
32
  const prefix = `rig_parts_matte_${crypto.randomUUID().slice(0, 8)}`;
33
- const info = await client.objectInfo();
33
+ const info = await client.objectInfo(run.wait);
34
34
  refuseIfAny(checkGraph(matteGraph(`${prefix}.png`, prefix), info, client.host, new Set(['1.image'])));
35
35
  const files = await client.modelFiles('RMBG');
36
36
  if (!files.some(f => f.replaceAll('\\', '/') === MATTE_MODEL_FILE)) refuse('MATTE_MODEL_INSTALLED', MATTE_MODEL_FILE, 'is absent from /models/RMBG; a selector entry alone does not establish installed weights, and automatic download is not permitted');
@@ -74,7 +74,7 @@ export async function runPainting(client: ComfyClient, run: PaintRun, say: (line
74
74
  const drawn = control === null ? null : controlImage(control.skeleton, run.pose, w, h);
75
75
  const skeletonName = drawn === null ? null : `spine_parts_${drawn.label}_${w}x${h}.png`;
76
76
  const probe = paintingGraph(g, seeds[0], prompts, `spine_parts_${run.config.key}_${seeds[0]}`, skeletonName);
77
- refuseIfAny(checkGraph(probe, await client.objectInfo(), client.host, new Set(['30.image'])));
77
+ refuseIfAny(checkGraph(probe, await client.objectInfo(run.wait), client.host, new Set(['30.image'])));
78
78
  say(' box: every node, input and model of the painting graph is on its /object_info');
79
79
 
80
80
  let skeletonRef: string | null = null;
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * `comfy plate --mode background`: the figure-removed background plate of a
3
3
  * painting, inpainted on a ComfyUI box over a source mask (issue #1183).
4
+ * `--mode occluder` (issue #1230) runs the same route over the mask a declared
5
+ * occluder image gives (`occluderMask`): the painting with what stands in
6
+ * front of the character inpainted away, for a See-through run to read the
7
+ * surface behind it from. Its receipt binds the occluder too.
4
8
  *
5
9
  * The route, in order, each step refusing by name:
6
10
  *
@@ -39,6 +43,9 @@ import {
39
43
  LAMA_MODEL,
40
44
  lamaPlateGraph,
41
45
  maskImage,
46
+ type MaskSource,
47
+ OCCLUDER_ALPHA_ABOVE,
48
+ occluderMask,
42
49
  parseMaskFrom,
43
50
  pasteInside,
44
51
  PLATE_GRAPH_IDS,
@@ -46,6 +53,7 @@ import {
46
53
  type PlateReceipt,
47
54
  prepareCoreGrid,
48
55
  publishPlate,
56
+ readOccluder,
49
57
  readPlatePainting,
50
58
  readSourceMask,
51
59
  } from '../plate.ts';
@@ -54,8 +62,14 @@ import { decodePngBytes, encodePngBytes } from '../raster/png.ts';
54
62
  import { ComfyClient, historyImages, refuse } from './client.ts';
55
63
 
56
64
  export interface BackgroundRun {
65
+ /** Absent: `background`. */
66
+ mode?: 'background' | 'occluder';
57
67
  painting: string;
58
68
  maskFrom: string | undefined;
69
+ /** `--mode occluder`: the RGBA occluder image on the painting's grid. */
70
+ occluder?: string;
71
+ /** `--mode occluder --holes`: fill the pockets the occluder encloses before growing. */
72
+ holes?: boolean;
59
73
  out: string;
60
74
  graph: PlateGraph;
61
75
  /** The model file the graph loads: the LaMa model for `lama`, the checkpoint for `core`. */
@@ -85,10 +99,13 @@ export function gpuName(stats: Record<string, unknown>, host: string): string {
85
99
  export async function runBackground(connect: () => ComfyClient, run: BackgroundRun, say: (line: string) => void): Promise<PlateReceipt> {
86
100
  // Local first: no source mask, a bad checksum or a missing checkpoint is refused before the box is reached.
87
101
  const cfg = coreCfg(run.cfg, run.graph);
88
- const from = parseMaskFrom(run.maskFrom);
102
+ const occluderMode = run.mode === 'occluder';
103
+ if (occluderMode && run.occluder === undefined) refuse('PLATE_OCCLUDER_PRESENT', '--occluder', 'is absent; --mode occluder reads its mask from an RGBA occluder image on the painting\'s grid');
104
+ const from: { source: MaskSource; dir: string } = occluderMode ? { source: 'occluder', dir: run.occluder! } : parseMaskFrom(run.maskFrom);
89
105
  const painting = readPlatePainting(run.painting);
90
106
  const { width: W, height: H } = painting.raster;
91
- const source: FigureMask = readSourceMask(from.source, from.dir, W, H, painting.raster, painting.bytes);
107
+ const occluder = occluderMode ? readOccluder(run.occluder!) : undefined;
108
+ const source: FigureMask = occluder !== undefined ? occluderMask(occluder.raster, run.occluder!, W, H, run.holes === true) : readSourceMask(from.source, from.dir, W, H, painting.raster, painting.bytes);
92
109
  const checkpoint = declaredChecksum(run.checkpointSha256);
93
110
  if (run.graph === 'core' && run.model === undefined) {
94
111
  refuse('PLATE_MODEL_GIVEN', '--model', 'is absent; the core graph needs the checkpoint the box loads, by its name on the box — there is no default checkpoint');
@@ -106,7 +123,7 @@ export async function runBackground(connect: () => ComfyClient, run: BackgroundR
106
123
 
107
124
  const client = connect();
108
125
  const gpu = gpuName(await client.systemStats(), client.host);
109
- const info = await client.objectInfo();
126
+ const info = await client.objectInfo(run.wait);
110
127
  refuseIfAny(checkGraph(build(`${prefix}_painting.png`, `${prefix}_mask.png`), info, client.host, new Set(['1.image', '2.image'])));
111
128
  say(` box: every node, input and model of the ${run.graph} graph is on its /object_info; GPU ${gpu}`);
112
129
 
@@ -133,7 +150,7 @@ export async function runBackground(connect: () => ComfyClient, run: BackgroundR
133
150
  return publishPlate({
134
151
  out: run.out,
135
152
  stageTag: prefix,
136
- mode: 'background',
153
+ mode: occluderMode ? 'occluder' : 'background',
137
154
  paintingPath: run.painting,
138
155
  paintingBytes: painting.bytes,
139
156
  source: from.source,
@@ -141,9 +158,10 @@ export async function runBackground(connect: () => ComfyClient, run: BackgroundR
141
158
  mask,
142
159
  plate: pasteInside(painting.raster, inpainted, mask),
143
160
  model: { name: model, checkpoint_sha256: checkpoint, checkpoint_sha256_from: 'declared' },
144
- params: run.graph === 'lama' ? { ...lama, mask_alpha_above: OPAQUE_ALPHA_ABOVE } : { ...core, mask_alpha_above: OPAQUE_ALPHA_ABOVE },
161
+ params: { ...(run.graph === 'lama' ? lama : core), ...(occluderMode ? { holes: run.holes === true, mask_alpha_above: OCCLUDER_ALPHA_ABOVE } : { mask_alpha_above: OPAQUE_ALPHA_ABOVE }) },
145
162
  graph: PLATE_GRAPH_IDS[run.graph],
146
163
  gpu,
147
164
  gridTransform: prepared.transform,
165
+ ...(occluder === undefined ? {} : { occluder: { path: run.occluder!, bytes: occluder.bytes } }),
148
166
  });
149
167
  }
@@ -92,7 +92,7 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
92
92
  const uploadName = `${prefix}_input.png`;
93
93
  const template: unknown = JSON.parse(readFileSync(SEETHROUGH_WORKFLOW, 'utf8'));
94
94
  const params: SeeThroughParams = { image: uploadName, seed: run.seed, resolution: run.resolution, steps: run.steps, quant: run.quant, offload: run.offload, lama: run.lama, prefix };
95
- const info = await client.objectInfo();
95
+ const info = await client.objectInfo(run.wait);
96
96
  refuseIfAny(checkGraph(fillSeeThrough(template, params), info, client.host, new Set(['1.image'])));
97
97
  say(` box: every node and input of workflows/seethrough.json is on its /object_info`);
98
98
 
package/src/config.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { hiddenExtentNameProblems, type HiddenExtentDeclaration } from './hiddenextent.ts';
2
+ import { occluderFileProblems, occluderNameProblems, type OccluderDeclaration } from './occluders.ts';
2
3
  import { SEAM_RULES, type SeamRule } from './seamrule.ts';
3
4
  import { DEFAULT_PROJECT_RULE, GROUNDS, type Ground, PROJECT_RULES, type ProjectRule } from './projectrule.ts';
4
5
  import type { TakeDeclaration } from './takeregion.ts';
@@ -339,6 +340,8 @@ export interface Assemble {
339
340
  keep_under_occluders?: string[];
340
341
  /** Require original material in enclosed source holes beneath a named moving cover. */
341
342
  require_hidden_extent?: HiddenExtentDeclaration[];
343
+ /** Issue #1230: declared RGBA occluders on the painting's grid and the plan parts they stand over. */
344
+ occluders?: OccluderDeclaration[];
342
345
  }
343
346
 
344
347
  export interface SingleBone {
@@ -1476,6 +1479,13 @@ export function configFileReferences(raw: unknown): ConfigFileReference[] {
1476
1479
  const target = parent;
1477
1480
  out.push({ at: field.path.join('.'), value, set: (next) => { target[key] = next; } });
1478
1481
  }
1482
+ // Issue #1230: each declared occluder's image, an array field the single-path table above cannot name.
1483
+ const assemble = isPlainObject(raw) ? raw.assemble : undefined;
1484
+ const occluders = isPlainObject(assemble) && Array.isArray(assemble.occluders) ? assemble.occluders : [];
1485
+ occluders.forEach((entry: unknown, i: number) => {
1486
+ if (!isPlainObject(entry) || typeof entry.image !== 'string' || entry.image === '') return;
1487
+ out.push({ at: `assemble.occluders[${i}].image`, value: entry.image, set: (next) => { entry.image = next; } });
1488
+ });
1479
1489
  return out;
1480
1490
  }
1481
1491
 
@@ -1702,7 +1712,7 @@ export interface EarlyConfig {
1702
1712
  depth?: DepthConfig;
1703
1713
  key: string;
1704
1714
  seethrough: SeeThrough;
1705
- assemble: { rig_scale: number; seam?: SeamRule; project?: ProjectRule; ground?: Ground; take?: TakeDeclaration[]; editor_receipts?: EditorReceipts; head_face_underfill?: boolean; yield_refused?: boolean; stage_map?: boolean; keep_under_occluders?: string[]; require_hidden_extent?: HiddenExtentDeclaration[]; extend_painting?: PaintingExtension[] };
1715
+ assemble: { rig_scale: number; seam?: SeamRule; project?: ProjectRule; ground?: Ground; take?: TakeDeclaration[]; editor_receipts?: EditorReceipts; head_face_underfill?: boolean; yield_refused?: boolean; stage_map?: boolean; keep_under_occluders?: string[]; require_hidden_extent?: HiddenExtentDeclaration[]; extend_painting?: PaintingExtension[]; occluders?: OccluderDeclaration[] };
1706
1716
  }
1707
1717
 
1708
1718
  /** What plain `assemble` reads: the early config plus the plan `--propose-plan` printed. */
@@ -1780,7 +1790,7 @@ function parseEarlyConfigFrom(raw: Json, door: EarlyDoor, base: string | null, p
1780
1790
  if (door === 'layers') {
1781
1791
  if ('seethrough' in t) checkSeeThrough(c, t.seethrough);
1782
1792
  if ('assemble' in t) {
1783
- const a = c.object('config.assemble', t.assemble, sectionRequired(door, 'assemble'), ['plan', 'extend_below_crop', 'patches', 'cuts', 'alternates', 'head_face_underfill', 'yield_refused', 'stage_map', 'keep_under_occluders', 'require_hidden_extent', 'extend_painting', 'take', 'seam', 'project', 'ground', 'editor_receipts']);
1793
+ const a = c.object('config.assemble', t.assemble, sectionRequired(door, 'assemble'), ['plan', 'extend_below_crop', 'patches', 'cuts', 'alternates', 'head_face_underfill', 'yield_refused', 'stage_map', 'keep_under_occluders', 'require_hidden_extent', 'extend_painting', 'take', 'seam', 'project', 'ground', 'editor_receipts', 'occluders']);
1784
1794
  if (a !== null) {
1785
1795
  checkAssembleRules(c, a);
1786
1796
  if ('rig_scale' in a) c.number('config.assemble.rig_scale', a.rig_scale, 'positive');
@@ -1792,6 +1802,7 @@ function parseEarlyConfigFrom(raw: Json, door: EarlyDoor, base: string | null, p
1792
1802
  }
1793
1803
  checkKeepUnderOccluders(c, a, null);
1794
1804
  checkRequiredHiddenExtent(c, a, null);
1805
+ checkOccluders(c, a, null, base);
1795
1806
  if ('extend_painting' in a) checkPaintingExtensions(c, a.extend_painting);
1796
1807
  if ('take' in a) checkTakeLayers(c, a.take);
1797
1808
  if ('editor_receipts' in a) checkEditorReceipts(c, a.editor_receipts);
@@ -1807,6 +1818,7 @@ function parseEarlyConfigFrom(raw: Json, door: EarlyDoor, base: string | null, p
1807
1818
  if (busyVisible && 'requirements' in t && c.string('config.requirements', t.requirements) && base !== null) checkRequirementsFile(c, configFilePath('requirements', base, t.requirements) as string);
1808
1819
  if ('seethrough' in t) checkSeeThrough(c, t.seethrough);
1809
1820
  const made = 'assemble' in t ? checkAssemble(c, t.assemble, door) : { parts: [], patches: [], cuts: [] };
1821
+ if ('assemble' in t) checkOccluderFiles(c, t.assemble, base);
1810
1822
  checkProposedRigScale(c, t);
1811
1823
  if ('scene' in t) checkScene(c, t.scene);
1812
1824
  // Issue #349: the limbs' joints are their bones' origins, so the bones are read with them, and only then.
@@ -2038,6 +2050,7 @@ function parseConfigFrom(raw: Json, base: string | null): { config: CharacterCon
2038
2050
  if ('generation' in t) checkGeneration(c, t.generation);
2039
2051
  if ('seethrough' in t) checkSeeThrough(c, t.seethrough);
2040
2052
  const { parts, patches, cuts } = 'assemble' in t ? checkAssemble(c, t.assemble, 'full') : { parts: [], patches: [], cuts: [] };
2053
+ if ('assemble' in t) checkOccluderFiles(c, t.assemble, base);
2041
2054
  checkProposedRigScale(c, t);
2042
2055
  const names = 'bones' in t ? checkBones(c, t.bones) : { bones: new Set<string>([ROOT_BONE]), chains: new Map<string, number>(), parents: new Map<string, string>() };
2043
2056
  const limbs = 'limbs' in t ? checkLimbs(c, t.limbs, { parts, patches, cuts }, 'bones' in t ? names : null) : null;
@@ -2539,6 +2552,40 @@ function checkRequiredHiddenExtent(c: Check, a: Record<string, Json>, names: rea
2539
2552
  c.problems.push(...hiddenExtentNameProblems(declarations, names));
2540
2553
  }
2541
2554
 
2555
+ /**
2556
+ * Issue #1230: `config.assemble.occluders`, `[{image, sha256, under, rgb_within}]`. Shape at every door; `under`
2557
+ * resolved against the plan where the door reads it; the file and its sha256 where the door knows the config's
2558
+ * directory (`checkOccluderFiles`).
2559
+ */
2560
+ function checkOccluders(c: Check, a: Record<string, Json>, names: readonly string[] | null, base: string | null): void {
2561
+ if (!('occluders' in a)) return;
2562
+ const at = 'config.assemble.occluders';
2563
+ if (!c.array(at, a.occluders, false)) return;
2564
+ const declarations: OccluderDeclaration[] = [];
2565
+ (a.occluders as Json[]).forEach((value, i) => {
2566
+ const p = `${at}[${i}]`;
2567
+ const row = c.object(p, value, ['image', 'sha256', 'under', 'rgb_within'], []);
2568
+ if (row === null) return;
2569
+ const imageOk = c.string(`${p}.image`, row.image);
2570
+ const shaOk = typeof row.sha256 === 'string' && SHA256_PATTERN.test(row.sha256);
2571
+ if (!shaOk) c.fail('CONFIG_FIELD_TYPE', `${p}.sha256`, `is ${show(row.sha256)}; ${SHA256_RULE} is required`);
2572
+ let underOk = c.array(`${p}.under`, row.under, true);
2573
+ if (underOk) (row.under as Json[]).forEach((name, j) => { if (!c.string(`${p}.under[${j}]`, name)) underOk = false; });
2574
+ const rgbOk = typeof row.rgb_within === 'number' && Number.isInteger(row.rgb_within) && row.rgb_within >= 0 && row.rgb_within <= 255;
2575
+ if (!rgbOk) c.fail('CONFIG_FIELD_TYPE', `${p}.rgb_within`, `is ${show(row.rgb_within)}; an integer from 0 to 255 is required`);
2576
+ if (imageOk && shaOk && underOk && rgbOk) declarations.push({ image: row.image as string, sha256: row.sha256 as string, under: row.under as string[], rgb_within: row.rgb_within as number });
2577
+ });
2578
+ c.problems.push(...occluderNameProblems(declarations, names));
2579
+ if (base !== null) c.problems.push(...occluderFileProblems(declarations, base));
2580
+ }
2581
+
2582
+ /** The occluder files, at the doors that know the config's directory but read the plan elsewhere. */
2583
+ function checkOccluderFiles(c: Check, assemble: Json, base: string | null): void {
2584
+ if (base === null || !isPlainObject(assemble) || !Array.isArray(assemble.occluders)) return;
2585
+ const declarations = (assemble.occluders as Json[]).filter((d): d is OccluderDeclaration => isPlainObject(d) && typeof d.image === 'string' && d.image !== '' && typeof d.sha256 === 'string' && SHA256_PATTERN.test(d.sha256));
2586
+ c.problems.push(...occluderFileProblems(declarations, base));
2587
+ }
2588
+
2542
2589
  function checkKeepUnderOccluders(c: Check, a: Record<string, Json>, plan: readonly string[] | null): void {
2543
2590
  if (!('keep_under_occluders' in a)) return;
2544
2591
  const at = 'config.assemble.keep_under_occluders';
@@ -2658,7 +2705,7 @@ function checkAssembleRules(c: Check, a: Record<string, Json>): void {
2658
2705
 
2659
2706
  function checkAssemble(c: Check, v: Json, door: 'assemble' | 'full'): { parts: string[]; patches: string[]; cuts: string[] } {
2660
2707
  const required = sectionRequired(door, 'assemble');
2661
- const a = c.object('config.assemble', v, required.filter((k) => k !== 'plan'), ['plan', 'extend_below_crop', 'patches', 'cuts', 'alternates', 'head_face_underfill', 'yield_refused', 'stage_map', 'keep_under_occluders', 'require_hidden_extent', 'extend_painting', 'take', 'seam', 'project', 'ground', 'editor_receipts']);
2708
+ const a = c.object('config.assemble', v, required.filter((k) => k !== 'plan'), ['plan', 'extend_below_crop', 'patches', 'cuts', 'alternates', 'head_face_underfill', 'yield_refused', 'stage_map', 'keep_under_occluders', 'require_hidden_extent', 'extend_painting', 'take', 'seam', 'project', 'ground', 'editor_receipts', 'occluders']);
2662
2709
  if (a === null) return { parts: [], patches: [], cuts: [] };
2663
2710
  checkAssembleRules(c, a);
2664
2711
  if (required.includes('plan') && !('plan' in a)) {
@@ -2698,6 +2745,7 @@ function checkAssemble(c: Check, v: Json, door: 'assemble' | 'full'): { parts: s
2698
2745
  }
2699
2746
  checkKeepUnderOccluders(c, a, parts);
2700
2747
  checkRequiredHiddenExtent(c, a, parts);
2748
+ checkOccluders(c, a, parts, null);
2701
2749
  if ('extend_painting' in a) checkPaintingExtensions(c, a.extend_painting);
2702
2750
  if ('take' in a) checkTakeLayers(c, a.take, parts);
2703
2751
  if ('editor_receipts' in a) checkEditorReceipts(c, a.editor_receipts);
@@ -0,0 +1,102 @@
1
+ /**
2
+ * Declared occluders (issue #1230): `config.assemble.occluders`, an opt-in
3
+ * list of RGBA images of what stands in front of the character, each on the
4
+ * painting's grid, bound by sha256, with the plan parts it stands over.
5
+ *
6
+ * Today one reading uses it: a pixel of an `under` part's selected source
7
+ * layer (after any take, before projection or cleanup) with alpha above
8
+ * {@link COPIED_ALPHA_ABOVE}, where the occluder's alpha is above 0, whose RGB
9
+ * lies within `rgb_within` (max absolute channel difference) of the painting
10
+ * at that pixel, is the occluder's own colour copied into the layer: the run
11
+ * painted the thing in front into the surface behind it. It is reported as a
12
+ * WARN with {@link OCCLUDER_COLOUR_COPIED}, never silently kept and never
13
+ * repaired.
14
+ *
15
+ * Nothing here reads a stage or an assembly, so the config doors can share
16
+ * the name and file checks without an import cycle.
17
+ */
18
+ import { existsSync, statSync } from 'node:fs';
19
+ import { resolve } from 'node:path';
20
+ import { fileSha256, SHA256_PATTERN } from './editreceipt.ts';
21
+ import type { Problem } from './errors.ts';
22
+ import type { Raster } from './raster/types.ts';
23
+
24
+ export interface OccluderDeclaration {
25
+ /** An RGBA PNG on the painting's grid, relative to the config's directory. */
26
+ image: string;
27
+ /** The image file's sha256, 64 lowercase hex digits. */
28
+ sha256: string;
29
+ /** Plan parts the occluder stands in front of. */
30
+ under: string[];
31
+ /** Max absolute channel difference from the painting at which an `under` pixel reads as the occluder's colour. */
32
+ rgb_within: number;
33
+ }
34
+
35
+ export const OCCLUDER_COLOUR_COPIED = 'ASSEMBLE_OCCLUDER_COLOUR_COPIED';
36
+ /** An `under` layer pixel counts at alpha above this: the half-opaque floor `deriveTakenRegion` reads. */
37
+ export const COPIED_ALPHA_ABOVE = 128;
38
+
39
+ const AT = 'config.assemble.occluders';
40
+
41
+ /** Every `under` entry names a plan part (`CONFIG_NAME_RESOLVES`); `names` null skips the check (a door without the plan). */
42
+ export function occluderNameProblems(declarations: readonly OccluderDeclaration[], names: readonly string[] | null): Problem[] {
43
+ if (names === null) return [];
44
+ const problems: Problem[] = [];
45
+ declarations.forEach((d, i) => d.under.forEach((name, j) => {
46
+ if (!names.includes(name)) problems.push({ code: 'CONFIG_NAME_RESOLVES', object: `${AT}[${i}].under[${j}]`, detail: `names "${name}"; a part in config.assemble.plan is required` });
47
+ }));
48
+ return problems;
49
+ }
50
+
51
+ /** The image is a file beside the config (`CONFIG_FILE_PRESENT`) whose bytes hash to `sha256` (`CONFIG_OCCLUDER_SHA256`). */
52
+ export function occluderFileProblems(declarations: readonly OccluderDeclaration[], base: string): Problem[] {
53
+ const problems: Problem[] = [];
54
+ declarations.forEach((d, i) => {
55
+ const path = resolve(base, d.image);
56
+ if (!existsSync(path) || !statSync(path).isFile()) {
57
+ problems.push({ code: 'CONFIG_FILE_PRESENT', object: `${AT}[${i}].image`, detail: `names ${d.image}, which is no such file; an RGBA PNG of the occluder on the painting's grid, relative to the config's directory, is required` });
58
+ return;
59
+ }
60
+ const found = fileSha256(path);
61
+ if (SHA256_PATTERN.test(d.sha256) && found !== d.sha256) {
62
+ problems.push({ code: 'CONFIG_OCCLUDER_SHA256', object: `${AT}[${i}].sha256`, detail: `is ${d.sha256}; ${d.image} has sha256 ${found} — declare the bytes the occluder image holds` });
63
+ }
64
+ });
65
+ return problems;
66
+ }
67
+
68
+ export interface OccluderCopy {
69
+ part: string;
70
+ px: number;
71
+ /** Rig px, x right and y down: the first copied pixel in row order. */
72
+ first: [number, number];
73
+ }
74
+
75
+ /**
76
+ * Copied occluder colour per `under` part, on the rig grid: `parts` are the
77
+ * selected source layers placed on the rig canvas (`width`x`height`), `painting`
78
+ * the painting on the same grid, `alpha(x, y)` the occluder's alpha at a rig
79
+ * pixel. A part with no copied pixel is left out.
80
+ */
81
+ export function occluderColourCopies(parts: ReadonlyArray<{ name: string; image: Raster }>, painting: Raster, alpha: (x: number, y: number) => number, rgbWithin: number): OccluderCopy[] {
82
+ const out: OccluderCopy[] = [];
83
+ const { width, height } = painting;
84
+ for (const { name, image } of parts) {
85
+ let px = 0, first = -1;
86
+ for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
87
+ const p = y * width + x;
88
+ if (image.data[p * 4 + 3] <= COPIED_ALPHA_ABOVE || alpha(x, y) <= 0) continue;
89
+ let delta = 0;
90
+ for (let c = 0; c < 3; c++) delta = Math.max(delta, Math.abs(image.data[p * 4 + c] - painting.data[p * 4 + c]));
91
+ if (delta > rgbWithin) continue;
92
+ if (first < 0) first = p;
93
+ px++;
94
+ }
95
+ if (px > 0) out.push({ part: name, px, first: [first % width, Math.floor(first / width)] });
96
+ }
97
+ return out;
98
+ }
99
+
100
+ export function occluderColourWarning(index: number, copy: OccluderCopy, rgbWithin: number): string {
101
+ return `WARN ${OCCLUDER_COLOUR_COPIED}: ${AT}[${index}] part "${copy.part}" — ${copy.px} rig px of its selected layer at alpha > ${COPIED_ALPHA_ABOVE} under the occluder (alpha > 0) lie within ${rgbWithin} of the painting (max channel); first at ${copy.first[0]},${copy.first[1]}; the layer holds the occluder's own colour — take this part from a See-through run on the painting with the occluder removed (comfy plate --mode occluder)`;
102
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * The assemble stage's reading of `config.assemble.occluders` (issue #1230):
3
+ * each declaration's image read and held to its sha256 and to the painting's
4
+ * size, each `under` part's selected source layer placed on the rig grid the
5
+ * way the hidden-extent reading places it (after any take, before projection
6
+ * or cleanup), and every copied-colour pixel counted ({@link occluderColourCopies}).
7
+ * It returns WARN lines and changes no output byte.
8
+ */
9
+ import { createHash } from 'node:crypto';
10
+ import { readFileSync } from 'node:fs';
11
+ import { dirname, resolve } from 'node:path';
12
+ import { checkGeometry, layerToRig, padSingleRun, placeLayer, sourceInRig } from './assemble.ts';
13
+ import type { PlanEntry } from './config.ts';
14
+ import { PartsError } from './errors.ts';
15
+ import type { LayerSet } from './layers.ts';
16
+ import { type OccluderDeclaration, occluderColourCopies, occluderColourWarning } from './occluders.ts';
17
+ import { decodePngBytes } from './raster/png.ts';
18
+ import type { Raster } from './raster/types.ts';
19
+
20
+ function refuse(code: string, object: string, detail: string): never {
21
+ throw new PartsError([{ code, object, detail }]);
22
+ }
23
+
24
+ export interface OccluderStageInput {
25
+ occluders: readonly OccluderDeclaration[];
26
+ configPath: string;
27
+ source: Raster;
28
+ runs: { full: LayerSet; head: LayerSet; single: boolean };
29
+ plan: readonly PlanEntry[];
30
+ resolution: number;
31
+ headBox: [number, number, number, number];
32
+ rigScale: number;
33
+ }
34
+
35
+ export function occluderStageWarnings(input: OccluderStageInput): string[] {
36
+ const { source, runs } = input;
37
+ const frame = checkGeometry({ sourceW: source.width, sourceH: source.height, resolution: input.resolution, headBox: input.headBox, rigScale: input.rigScale, ...(runs.single ? { single: true } : {}) }, { full: runs.full, head: runs.head });
38
+ const full = runs.single ? padSingleRun(runs.full, frame, input.resolution) : runs.full;
39
+ const painting = sourceInRig(source, frame.W, frame.H);
40
+ const lines: string[] = [];
41
+ input.occluders.forEach((d, i) => {
42
+ const at = `config.assemble.occluders[${i}]`;
43
+ const path = resolve(dirname(resolve(input.configPath)), d.image);
44
+ const bytes = new Uint8Array(readFileSync(path));
45
+ const found = createHash('sha256').update(bytes).digest('hex');
46
+ if (found !== d.sha256) refuse('CONFIG_OCCLUDER_SHA256', `${at}.sha256`, `is ${d.sha256}; ${d.image} has sha256 ${found} — declare the bytes the occluder image holds`);
47
+ const image = decodePngBytes(bytes, path);
48
+ if (image.width !== source.width || image.height !== source.height) {
49
+ refuse('ASSEMBLE_OCCLUDER_GRID', `${at}.image`, `is ${image.width}x${image.height}; the painting's ${source.width}x${source.height} is required — the occluder is read on the painting's grid and never fitted`);
50
+ }
51
+ // A rig pixel reads the occluder at the source pixel its centre falls in.
52
+ const sx = (x: number): number => Math.min(source.width - 1, Math.floor((x + 0.5) * source.width / frame.W));
53
+ const sy = (y: number): number => Math.min(source.height - 1, Math.floor((y + 0.5) * source.height / frame.H));
54
+ const alpha = (x: number, y: number): number => image.data[(sy(y) * source.width + sx(x)) * 4 + 3];
55
+ const parts = d.under.flatMap((name) => {
56
+ const row = input.plan.find(([part]) => part === name);
57
+ if (row === undefined) return [];
58
+ const layer = (row[1] === 'full' ? full : runs.head).layers.find((l) => l.name === row[2]);
59
+ return layer === undefined ? [] : [{ name, image: layerToRig(placeLayer(layer, input.resolution), frame, row[1]) }];
60
+ });
61
+ for (const copy of occluderColourCopies(parts, painting, alpha, d.rgb_within)) lines.push(occluderColourWarning(i, copy, d.rgb_within));
62
+ });
63
+ return lines;
64
+ }
package/src/plate.ts CHANGED
@@ -17,7 +17,10 @@
17
17
  * `scene`, the figure being the `figure_rig.png` assemble wrote beside it
18
18
  * (opaque white inside the figure, opaque black outside);
19
19
  * - `matte:<dir>`: a model mask whose receipt binds both raw and final masks
20
- * to the exact painting bytes and the recorded threshold/transform.
20
+ * to the exact painting bytes and the recorded threshold/transform;
21
+ * - `--occluder <png>` (`--mode occluder`, issue #1230): a declared RGBA
22
+ * image of what stands in front of the character, on the painting's grid,
23
+ * the mask being its alpha above 0 (enclosed holes filled under `--holes`).
21
24
  *
22
25
  * The mask is taken on its own grid and never fitted: a source whose grid is
23
26
  * not the painting's size is refused by name. With no source the command
@@ -39,7 +42,8 @@ import type { Graph } from './graphs.ts';
39
42
  import { type LayerSet, OPAQUE_ALPHA_ABOVE, readWrapperLayers } from './layers.ts';
40
43
  import { FIGURE_MASK_FILE, PAINTING_IN_RIG_FILE } from './parts.ts';
41
44
  import { decodePngBytes, encodePngBytes } from './raster/png.ts';
42
- import { newRaster, type Raster } from './raster/types.ts';
45
+ import { fillHoles } from './raster/morph.ts';
46
+ import { newMask, newRaster, type Raster } from './raster/types.ts';
43
47
 
44
48
  export const PLATE_RECEIPT_SPEC = 'rig-parts-plate-receipt/1';
45
49
  export const PLATE_RECEIPT_FILE = 'receipt.json';
@@ -50,12 +54,16 @@ export const PLATE_OUTPUT_FILE = 'plate.png';
50
54
  export const PLATE_RECEIPT_KEYS = ['spec', 'mode', 'painting', 'mask', 'model', 'params', 'graph', 'gpu', 'output'] as const;
51
55
  /** The receipt's top-level keys when `grid_transform` is present (off-grid `core`), in the order they are written. */
52
56
  export const PLATE_RECEIPT_GRID_TRANSFORM_KEYS = ['spec', 'mode', 'painting', 'mask', 'model', 'params', 'graph', 'gpu', 'grid_transform', 'output'] as const;
57
+ /** `--mode occluder` (issue #1230): the keys above with the declared occluder image bound after the painting. */
58
+ export const PLATE_RECEIPT_OCCLUDER_KEYS = ['spec', 'mode', 'painting', 'occluder', 'mask', 'model', 'params', 'graph', 'gpu', 'output'] as const;
59
+ /** `--mode occluder` with `grid_transform` (off-grid `core`). */
60
+ export const PLATE_RECEIPT_OCCLUDER_GRID_TRANSFORM_KEYS = ['spec', 'mode', 'painting', 'occluder', 'mask', 'model', 'params', 'graph', 'gpu', 'grid_transform', 'output'] as const;
53
61
  /** The `grid_transform` object's keys, in the order they are written. */
54
62
  export const CORE_GRID_TRANSFORM_KEYS = ['source', 'padded', 'pad', 'pad_fill', 'mask_pad_fill', 'inverse'] as const;
55
63
 
56
- export type PlateMode = 'background' | 'foreground';
64
+ export type PlateMode = 'background' | 'foreground' | 'occluder';
57
65
  export type PlateGraph = 'lama' | 'core';
58
- export type MaskSource = 'seethrough' | 'figure-mask' | 'matte';
66
+ export type MaskSource = 'seethrough' | 'figure-mask' | 'matte' | 'occluder';
59
67
 
60
68
  export interface CoreGridTransform {
61
69
  source: [number, number];
@@ -82,6 +90,8 @@ export interface PlateReceipt {
82
90
  spec: typeof PLATE_RECEIPT_SPEC;
83
91
  mode: PlateMode;
84
92
  painting: { path: string; sha256: string };
93
+ /** `--mode occluder` only: the declared occluder image the mask is read from. */
94
+ occluder?: { path: string; sha256: string };
85
95
  mask: { source: MaskSource; from: string; path: string; sha256: string };
86
96
  model: { name: string; checkpoint_sha256: string; checkpoint_sha256_from: 'declared' } | null;
87
97
  params: Record<string, string | number | boolean>;
@@ -315,6 +325,40 @@ export function readSourceMask(source: MaskSource, dir: string, width: number, h
315
325
  return { width, height, inside };
316
326
  }
317
327
 
328
+ /** `--occluder <png>`: its bytes as on disk (hashed into the receipt) and its pixels; a missing file is refused by name. */
329
+ export function readOccluder(path: string): { bytes: Uint8Array; raster: Raster } {
330
+ if (!existsSync(path) || !statSync(path).isFile()) fail('PLATE_OCCLUDER_PRESENT', path, 'no such file; an RGBA PNG of what stands in front of the character, on the painting\'s grid, is required');
331
+ const bytes = new Uint8Array(readFileSync(path));
332
+ return { bytes, raster: decodePngBytes(bytes, path) };
333
+ }
334
+
335
+ /** The occluder's alpha above this is inside the mask: every pixel the occluder touches, soft edge included. */
336
+ export const OCCLUDER_ALPHA_ABOVE = 0;
337
+
338
+ /**
339
+ * The `--mode occluder` mask (issue #1230): the occluder's alpha above
340
+ * {@link OCCLUDER_ALPHA_ABOVE}, on the painting's own grid, never fitted; with
341
+ * `holes`, every pocket the occluder encloses (4-connected, as `fillHoles`
342
+ * reads it) is filled. Refused by name: an occluder whose size is not the
343
+ * painting's, one with no pixel above the floor, and one with no pixel at or
344
+ * below it (an image with no transparency would mask the whole painting).
345
+ */
346
+ export function occluderMask(occluder: Raster, path: string, width: number, height: number, holes: boolean): FigureMask {
347
+ if (occluder.width !== width || occluder.height !== height) {
348
+ fail('PLATE_OCCLUDER_GRID', path, `is ${occluder.width}x${occluder.height}; the painting's ${width}x${height} is required — the occluder is read on the painting's own grid and never fitted`);
349
+ }
350
+ const support = newMask(width, height);
351
+ let clear = 0;
352
+ for (let p = 0; p < width * height; p++) {
353
+ if (occluder.data[p * 4 + 3] > OCCLUDER_ALPHA_ABOVE) support.data[p] = 1;
354
+ else clear++;
355
+ }
356
+ if (clear === width * height) fail('PLATE_MASK_EMPTY', path, `holds no pixel with alpha above ${OCCLUDER_ALPHA_ABOVE}; an occluder to remove is required`);
357
+ if (clear === 0) fail('PLATE_OCCLUDER_ALPHA', path, `holds no pixel with alpha ${OCCLUDER_ALPHA_ABOVE}; an RGBA image transparent outside the occluder is required — an opaque one would mask the whole painting`);
358
+ const inside = holes ? fillHoles(support).data : support.data;
359
+ return { width, height, inside: Uint8Array.from(inside) };
360
+ }
361
+
318
362
  /** The mask grown by a square of side `2r+1` (two separable passes): a halo of the figure's outline is inpainted too. */
319
363
  export function growMask(mask: FigureMask, r: number): FigureMask {
320
364
  if (r === 0) return { ...mask, inside: mask.inside.slice() };
@@ -521,11 +565,16 @@ export function cropCoreGrid(padded: Raster, transform: CoreGridTransform): Rast
521
565
  // the receipt
522
566
  // ---------------------------------------------------------------------------
523
567
 
568
+ /** The receipt's key order: occluder mode binds the occluder, an off-grid `core` run its `grid_transform`. */
569
+ export function receiptKeys(occluder: boolean, gridTransform: boolean): readonly string[] {
570
+ if (occluder) return gridTransform ? PLATE_RECEIPT_OCCLUDER_GRID_TRANSFORM_KEYS : PLATE_RECEIPT_OCCLUDER_KEYS;
571
+ return gridTransform ? PLATE_RECEIPT_GRID_TRANSFORM_KEYS : PLATE_RECEIPT_KEYS;
572
+ }
573
+
524
574
  /** The receipt's JSON text, its keys in `PLATE_RECEIPT_KEYS` (or `PLATE_RECEIPT_GRID_TRANSFORM_KEYS` when `grid_transform` is present) order. */
525
575
  export function receiptText(r: PlateReceipt): string {
526
576
  const ordered: Record<string, unknown> = {};
527
- const keys = r.grid_transform === undefined ? PLATE_RECEIPT_KEYS : PLATE_RECEIPT_GRID_TRANSFORM_KEYS;
528
- for (const k of keys) ordered[k] = r[k];
577
+ for (const k of receiptKeys(r.mode === 'occluder', r.grid_transform !== undefined)) ordered[k] = (r as unknown as Record<string, unknown>)[k];
529
578
  return `${JSON.stringify(ordered, null, 2)}\n`;
530
579
  }
531
580
 
@@ -554,11 +603,11 @@ export function verifyPlateReceipt(receiptFile: string): Problem[] {
554
603
  }
555
604
  if (r.spec !== PLATE_RECEIPT_SPEC) problems.push({ code: 'PLATE_RECEIPT_SPEC', object: receiptFile, detail: `has spec ${JSON.stringify(r.spec)}; "${PLATE_RECEIPT_SPEC}" is required` });
556
605
  const keys = Object.keys(r).join(',');
557
- const expectedKeys = 'grid_transform' in r ? PLATE_RECEIPT_GRID_TRANSFORM_KEYS : PLATE_RECEIPT_KEYS;
606
+ const expectedKeys = receiptKeys(r.mode === 'occluder', 'grid_transform' in r);
558
607
  if (keys !== expectedKeys.join(',')) problems.push({ code: 'PLATE_RECEIPT_KEYS', object: receiptFile, detail: `has keys ${keys}; ${expectedKeys.join(',')} in that order is required` });
559
608
  const base = dirname(receiptFile);
560
609
  let paintingRaster: Raster | undefined;
561
- for (const field of ['painting', 'mask', 'output'] as const) {
610
+ for (const field of r.mode === 'occluder' ? ['painting', 'occluder', 'mask', 'output'] as const : ['painting', 'mask', 'output'] as const) {
562
611
  const f = r[field] as { path?: unknown; sha256?: unknown } | undefined;
563
612
  if (typeof f?.path !== 'string' || typeof f.sha256 !== 'string') {
564
613
  problems.push({ code: 'PLATE_RECEIPT_KEYS', object: `${receiptFile} ${field}`, detail: 'lacks a path and a sha256' });
@@ -577,7 +626,8 @@ export function verifyPlateReceipt(receiptFile: string): Problem[] {
577
626
  }
578
627
  }
579
628
  if (paintingRaster !== undefined) {
580
- const expectedTransform = r.mode === 'background' && r.graph === PLATE_GRAPH_IDS.core ? coreGridTransform(paintingRaster.width, paintingRaster.height) : undefined;
629
+ const inpainted = r.mode === 'background' || r.mode === 'occluder';
630
+ const expectedTransform = inpainted && r.graph === PLATE_GRAPH_IDS.core ? coreGridTransform(paintingRaster.width, paintingRaster.height) : undefined;
581
631
  if (expectedTransform !== undefined) {
582
632
  if (!('grid_transform' in r) || r.grid_transform === undefined) {
583
633
  problems.push({
@@ -596,7 +646,7 @@ export function verifyPlateReceipt(receiptFile: string): Problem[] {
596
646
  problems.push({
597
647
  code: 'PLATE_RECEIPT_GRID_TRANSFORM',
598
648
  object: `${receiptFile} grid_transform`,
599
- detail: `is ${JSON.stringify(r.grid_transform)}; no grid_transform is permitted for ${r.mode === 'background' && r.graph === PLATE_GRAPH_IDS.core ? `on-grid painting ${paintingRaster.width}x${paintingRaster.height}` : `mode ${JSON.stringify(r.mode)} graph ${JSON.stringify(r.graph)}`}`,
649
+ detail: `is ${JSON.stringify(r.grid_transform)}; no grid_transform is permitted for ${inpainted && r.graph === PLATE_GRAPH_IDS.core ? `on-grid painting ${paintingRaster.width}x${paintingRaster.height}` : `mode ${JSON.stringify(r.mode)} graph ${JSON.stringify(r.graph)}`}`,
600
650
  });
601
651
  }
602
652
  } else if ('grid_transform' in r && (typeof r.grid_transform !== 'object' || r.grid_transform === null || Array.isArray(r.grid_transform) || Object.keys(r.grid_transform as object).join(',') !== CORE_GRID_TRANSFORM_KEYS.join(','))) {
@@ -643,6 +693,8 @@ export interface PlateWrite {
643
693
  graph: string;
644
694
  gpu: string | null;
645
695
  gridTransform?: CoreGridTransform;
696
+ /** `--mode occluder` only: the occluder file, bound into the receipt by path and sha256. */
697
+ occluder?: { path: string; bytes: Uint8Array };
646
698
  }
647
699
 
648
700
  let foregroundStageSeq = 0;
@@ -666,6 +718,7 @@ export function publishPlate(w: PlateWrite): PlateReceipt {
666
718
  spec: PLATE_RECEIPT_SPEC,
667
719
  mode: w.mode,
668
720
  painting: { path: receiptPath(w.out, w.paintingPath), sha256: sha256(w.paintingBytes) },
721
+ ...(w.occluder === undefined ? {} : { occluder: { path: receiptPath(w.out, w.occluder.path), sha256: sha256(w.occluder.bytes) } }),
669
722
  mask: { source: w.source, from: receiptPath(w.out, w.sourceDir), path: PLATE_MASK_FILE, sha256: mask.sha256 },
670
723
  model: w.model,
671
724
  params: w.params,