@rig-c/parts 1.67.0 → 1.69.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/src/assemble.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { bindDepthRun, copyDepthMaterial, registerDepthRun, transportPartDepth, type DepthConfig, type RegisteredDepth } from './depthsheets.ts';
1
2
  /**
2
3
  * The assemble stage: two See-through runs over one painting — a FULL-body run
3
4
  * and a HEAD-crop run — merged into one set of rig-space parts, each part one
@@ -101,7 +102,7 @@
101
102
  */
102
103
  import { sourceLimbTarget, nearestTargetPart } from './limbownership.ts';
103
104
  import { type ArmSplit, type BoneEntry, type CharacterConfig, type ControlSkeleton, controlSkeletonOf, type Cut, type EarlyConfig, type Extend, type Limb, type Patch, type PlanEntry, type Run, type SeeThrough } from './config.ts';
104
- import { type Problem, refuseIfAny } from './errors.ts';
105
+ import { PartsError, type Problem, refuseIfAny } from './errors.ts';
105
106
  import { CORE_ALPHA, figuresPhrase, implausibleRules, type Layer, type LayerFigures, layerFigures, type LayerSet, NEAR_WHITE_MIN, OPAQUE_ALPHA_ABOVE, ruleSummary } from './layers.ts';
106
107
  import { padToSquare, squarePad } from './inputs.ts';
107
108
  import type { MotionSeamDeclaration } from './motionseams.ts';
@@ -308,6 +309,9 @@ export function singleRunPadRecord(single: boolean, sourceW: number, sourceH: nu
308
309
  }
309
310
 
310
311
  export interface AssembleInput {
312
+ depth?: DepthConfig;
313
+ /** Config-relative user sheet paths; caller supplies its actual base. */
314
+ depthBase?: string;
311
315
  /** The painting. Its alpha is ignored, as the reference's `convert("RGB")` drops it. */
312
316
  source: Raster;
313
317
  full: LayerSet;
@@ -1360,10 +1364,11 @@ export function applyArmSplit(set: LayerSet, resolution: number, armSplit: ArmSp
1360
1364
  ]);
1361
1365
  }
1362
1366
  const combinedRaw = newRaster(resolution, resolution);
1363
- for (const l of handLayers) {
1367
+ const scalarOwners = handLayers.some(l => l.depthSheet !== undefined) ? new Int32Array(resolution * resolution).fill(-1) : null;
1368
+ for (const [layerIndex, l] of handLayers.entries()) {
1364
1369
  const { canvas } = cleanGhosts(placeLayer(l, resolution));
1365
1370
  for (let p = 0; p < resolution * resolution; p++) {
1366
- if (canvas.data[p * 4 + 3] > 0) combinedRaw.data.set(canvas.data.subarray(p * 4, p * 4 + 4), p * 4);
1371
+ if (canvas.data[p * 4 + 3] > 0) { combinedRaw.data.set(canvas.data.subarray(p * 4, p * 4 + 4), p * 4); if (scalarOwners !== null) scalarOwners[p] = layerIndex; }
1367
1372
  }
1368
1373
  }
1369
1374
  const { canvas: combined } = cleanGhosts(combinedRaw);
@@ -1470,7 +1475,7 @@ export function applyArmSplit(set: LayerSet, resolution: number, armSplit: ArmSp
1470
1475
  }
1471
1476
  }
1472
1477
  const refLayer = handLayers.reduce((best, l) => (l.opaquePx > best.opaquePx ? l : best), handLayers[0]);
1473
- const makeLayer = (name: 'handwear-r' | 'handwear-l', canvas: Raster, m: ReturnType<typeof maskOf>): Layer => ({
1478
+ const makeLayer = (name: 'handwear-r' | 'handwear-l', canvas: Raster, m: ReturnType<typeof maskOf>): Layer => copyDepthMaterial({
1474
1479
  name,
1475
1480
  tag: readLayerTag(name) as LayerTagReading,
1476
1481
  file: refLayer.file,
@@ -1482,7 +1487,7 @@ export function applyArmSplit(set: LayerSet, resolution: number, armSplit: ArmSp
1482
1487
  depth: refLayer.depth,
1483
1488
  drawOrder: 0,
1484
1489
  opaquePx: m.opaque,
1485
- });
1490
+ }, handLayers, (x, y) => { const owner = scalarOwners?.[Math.floor(y) * resolution + Math.floor(x)] ?? -1; return owner < 0 ? [] : [{ layer: handLayers[owner], x, y, weight: 1 }]; });
1486
1491
  const byTag = {
1487
1492
  'handwear-r': makeLayer('handwear-r', rightCanvas, mr),
1488
1493
  'handwear-l': makeLayer('handwear-l', leftCanvas, ml),
@@ -1779,7 +1784,7 @@ export function splitMergedArmRunsWithNotes(
1779
1784
  }
1780
1785
  }
1781
1786
  if (opaque === 0 || x1 < x0) return null;
1782
- return {
1787
+ return copyDepthMaterial({
1783
1788
  name,
1784
1789
  tag: readLayerTag(name) as LayerTagReading,
1785
1790
  file: hwLayer.file,
@@ -1791,7 +1796,7 @@ export function splitMergedArmRunsWithNotes(
1791
1796
  depth: hwLayer.depth,
1792
1797
  drawOrder: 0,
1793
1798
  opaquePx: opaque,
1794
- };
1799
+ }, [hwLayer], (x, y) => [{ layer: hwLayer, x, y, weight: 1 }]);
1795
1800
  };
1796
1801
  const drapeLayer = buildSplitLayer(`drape-${s}`, drapeCanvas);
1797
1802
  const sleeveLayer = buildSplitLayer(tag, sleeveCanvas);
@@ -2085,9 +2090,14 @@ function prepareHeadFace(
2085
2090
  bottom: y1 + 1,
2086
2091
  opaquePx: compOpaquePx,
2087
2092
  };
2093
+ const copiedLayer = copyDepthMaterial(replacedLayer, [headFaceLayer, fullFaceLayer], (x, y) => {
2094
+ const ta = cleanedHead.canvas.data[(Math.floor(y) * resolution + Math.floor(x)) * 4 + 3] / 255;
2095
+ const sx = f32(frame.fullK / frame.headK), tx = f32(-(frame.headBox[0] + frame.fullPad) / frame.headK), ty = frame.fullPadTop === 0 && frame.headBox[1] === 0 ? 0 : f32(-(frame.headBox[1] + frame.fullPadTop) / frame.headK);
2096
+ return [{ layer: headFaceLayer, x, y, weight: ta }, { layer: fullFaceLayer, x: (x - 0.5 - tx) / sx + 0.5, y: (y - 0.5 - ty) / sx + 0.5, weight: 1 - ta }];
2097
+ });
2088
2098
  activeHead = {
2089
2099
  ...head,
2090
- layers: head.layers.map((l) => (l.name === 'face' ? replacedLayer : l)),
2100
+ layers: head.layers.map((l) => (l.name === 'face' ? copiedLayer : l)),
2091
2101
  };
2092
2102
  if (underfillPx > 0) {
2093
2103
  underfillNote = `face: head_face_underfill composited full:face under head:face on the head canvas, filling ${underfillPx} transparent px (${rawStats.headOv} / ${fullPx} px, ${(rawStats.cov * 100).toFixed(1)}% -> ${postStats.headOv} / ${fullPx} px, ${(postStats.cov * 100).toFixed(1)}%)`;
@@ -2559,6 +2569,7 @@ export function figureSilhouette(srcr: Raster): Mask {
2559
2569
  }
2560
2570
 
2561
2571
  export interface PlacedPart {
2572
+ depthImage?: Raster;
2562
2573
  record: PartRecord;
2563
2574
  image: Raster;
2564
2575
  }
@@ -4313,6 +4324,7 @@ export interface AssembleResult {
4313
4324
  hidden: HiddenArtUse[];
4314
4325
  seamRule: SeamRule;
4315
4326
  projectRule: ProjectRule;
4327
+ depthReadings?: Array<{ from: string; reading: import('./depthsheets.ts').DepthReading }>;
4316
4328
  /** Issue #492: head-canvas pixels filled by `config.assemble.head_face_underfill: true` when > 0. */
4317
4329
  headFaceUnderfillPx?: number;
4318
4330
  /** Issue #587: what each `exclude` entry took, in entry order; empty without one. */
@@ -4475,8 +4487,17 @@ export function assemble(input: AssembleInput): AssembleResult {
4475
4487
  { sourceW: source.width, sourceH: source.height, resolution: input.resolution, headBox: input.headBox, rigScale: input.rigScale, ...(single ? { single: true } : {}) },
4476
4488
  { full: input.full, head: input.head },
4477
4489
  );
4490
+ if (input.depthBase === undefined && (input.depth?.full !== undefined || input.depth?.head !== undefined)) refuseIfAny([{ code: 'DEPTH_SOURCE_BASE', object: 'assemble depthBase', detail: 'user sheet paths require the actual config-directory base; no process-cwd guess is made' }]);
4491
+ const depthInputProblems: Problem[] = [];
4492
+ const bind = (set: LayerSet, run: 'full' | 'head'): LayerSet => {
4493
+ try { return bindDepthRun(set, run, input.depth, input.depthBase ?? ''); }
4494
+ catch (error) { if (error instanceof PartsError) { depthInputProblems.push(...error.problems); return set; } throw error; }
4495
+ };
4496
+ const depthFullInput = bind(input.full, 'full');
4497
+ const depthHeadInput = single ? depthFullInput : bind(input.head, 'head');
4498
+ refuseIfAny(depthInputProblems);
4478
4499
  // Issue #366: one run's file, padded to the resolution square; read as both runs, as before.
4479
- const fullPadded = single ? padSingleRun(input.full, frame, input.resolution) : input.full;
4500
+ const fullPadded = single ? padSingleRun(depthFullInput, frame, input.resolution) : depthFullInput;
4480
4501
  // Issue #439: when seethrough.arm_split is declared, split the full run's overlapping handwear into handwear-r and handwear-l.
4481
4502
  const fullArmSplit = input.armSplit === undefined ? fullPadded : applyArmSplit(fullPadded, input.resolution, input.armSplit);
4482
4503
  // Issue #490: when generation.control.skeleton is present, split merged arm layers (or refuse when sleeve and drape share one colour).
@@ -4494,10 +4515,13 @@ export function assemble(input: AssembleInput): AssembleResult {
4494
4515
  !single &&
4495
4516
  (plan.some(([, r, t]) => r === 'head' && t === 'face') || (plan.some(([, r]) => r === 'full') && plan.some(([, r]) => r === 'head')));
4496
4517
  const headFacePrep = checkHeadFace
4497
- ? prepareHeadFace(full, input.head, frame, input.resolution, input.headFaceUnderfill === true)
4498
- : { head: single ? full : input.head, underfillPx: 0, underfillNote: null };
4518
+ ? prepareHeadFace(full, depthHeadInput, frame, input.resolution, input.headFaceUnderfill === true)
4519
+ : { head: single ? full : depthHeadInput, underfillPx: 0, underfillNote: null };
4499
4520
  const head = headFacePrep.head;
4500
4521
  const padRecord = singleRunPadRecord(single, source.width, source.height, input.resolution, frame);
4522
+ const depthFull = registerDepthRun(full, 'full', input.depth, input.depthBase ?? '');
4523
+ const depthHead = single ? new Map<string, RegisteredDepth>() : registerDepthRun(head, 'head', input.depth, input.depthBase ?? '');
4524
+ const registeredDepth = new Map([...depthFull, ...depthHead]);
4501
4525
  if (single) checkSingleRun(full, plan, extend);
4502
4526
  checkPlanAgainstRuns(sourcePlan, extend, full, head);
4503
4527
  const { W, H } = frame;
@@ -5074,10 +5098,30 @@ export function assemble(input: AssembleInput): AssembleResult {
5074
5098
  };
5075
5099
  if (pushedHere.length > 0) record.fringe_pushed_back = pushedHere;
5076
5100
  if (limb !== undefined) record.limb = limb;
5077
- set({ record, image: crop(r, x0, y0, record.w, record.h) });
5101
+ const image = crop(r, x0, y0, record.w, record.h);
5102
+ const limbIndex = limb === undefined ? -1 : lim.parts.findIndex(q => q.record.name === limb.name && q.record.segment === limb.segment);
5103
+ const upperIndex = limb === undefined ? -1 : lim.parts.findIndex(q => q.record.name === limb.name && q.record.segment === limb.segment - 1);
5104
+ const capSource = limbIndex < 0 ? null : lim.capFrom[limbIndex];
5105
+ const scalar = transportPartDepth(record, image, registeredDepth, {
5106
+ map: run => runMap(frame, run), width: W, extend: extendKeys, owner: trace.from,
5107
+ ring: trace.ring.data, region: region?.data ?? null,
5108
+ copiedSource: (x, y) => {
5109
+ if (capSource === null || upperIndex < 0) return null;
5110
+ const [bx, by, bw, bh] = capSource.box;
5111
+ if (x < bx || y < by || x >= bx + bw || y >= by + bh || capSource.codes[(y - by) * bw + x - bx] !== CAP_FROM_UPPER) return null;
5112
+ const upper = limbPieces[upperIndex], p = y * W + x, e = upper.trace.from[p];
5113
+ return { key: e < 0 ? upper.key : upper.extendKeys[e], ring: upper.trace.ring.data[p] !== 0 };
5114
+ },
5115
+ ...((limb?.cap?.image_px ?? 0) > 0 || (limb?.cap?.lower_px ?? 0) > 0 ? { unsupported: `cap copies ${(limb?.cap?.image_px ?? 0) + (limb?.cap?.lower_px ?? 0)} pixels from a separate cap image whose scalar grid is undeclared` } : {}),
5116
+ });
5117
+ if (scalar !== undefined) record.depth = scalar.reference;
5118
+ set({ record, image, ...(scalar === undefined ? {} : { depthImage: scalar.image }) });
5078
5119
  });
5079
5120
  refuseIfAny(empty);
5080
5121
  const placed: PlacedPart[] = order.map((d) => ('plan' in d ? planned[d.plan] : 'cut' in d ? cutPlaced[d.cut] : 'limb' in d ? limbPlaced[d.limb] : cut[d.patch]) as PlacedPart);
5122
+ const artFiles = new Set(placed.map(p => `${p.record.name}.png`));
5123
+ for (const p of placed) if (p.record.depth !== undefined && artFiles.has(p.record.depth.image)) refuseIfAny([{ code: 'DEPTH_SHEET_UNAMBIGUOUS', object: p.record.depth.image, detail: 'collides with a part artwork filename; choose noncolliding part names before sheet emission' }]);
5124
+
5081
5125
 
5082
5126
  seamOverride(placed, srcr, seamRule, seamRule === 'silhouette' ? silhouette : null, keepInOrder);
5083
5127
  const can = recomposite(placed, W, H);
@@ -5115,6 +5159,7 @@ export function assemble(input: AssembleInput): AssembleResult {
5115
5159
  ...(padRecord === null ? {} : { single_run_pad: padRecord }),
5116
5160
  },
5117
5161
  images: placed,
5162
+ ...(registeredDepth.size === 0 ? {} : { depthReadings: ([['full', depthFullInput], ...(single ? [] : [['head', depthHeadInput]])] as Array<['full' | 'head', LayerSet]>).flatMap(([run, set]) => set.layers.flatMap(layer => layer.depthSheet === undefined ? [] : [{ from: `${run}:${layer.name}`, reading: layer.depthSheet.reading }])) }),
5118
5163
  recomposite: can,
5119
5164
  errorMap: recompositeErrorMap(can, srcr, placed, inside),
5120
5165
  figure: inside === null ? null : figureImage(inside, W, H),
package/src/automotion.ts CHANGED
@@ -84,7 +84,7 @@
84
84
  * Pure: no clock, no randomness, nothing read or written.
85
85
  */
86
86
  import { type AcceptedOperation, MeshReductionError, type AlphaMask, type ArtFitBounds, type MeasureRow, type MeshQualityReport, type MotionAmplitude, type MotionBounds, type MotionSchedule, writeMeshQualityReport } from 'rig-c/mesh';
87
- import { compareMeshesInMotion, type MotionComparisonInput } from 'rig-c/meshcompare';
87
+ import { compareMeshesInMotion, comparePreparedMotion, prepareMotionReference, type MotionComparisonInput, type MotionReferenceInput, type PreparedMotionReference } from 'rig-c/meshcompare';
88
88
  import type { AutoMotionSpec } from './config.ts';
89
89
  import type { Problem } from './errors.ts';
90
90
  import { IDLE_FPS, type MotionSpec, type MotionTrack } from './motion.ts';
@@ -334,6 +334,26 @@ export function runComparison(object: string, input: MotionComparisonInput): Mes
334
334
  }
335
335
  }
336
336
 
337
+ /** Bind the exact reference-side input for the sequential replay candidates. */
338
+ export function prepareComparison(object: string, input: MotionReferenceInput): PreparedMotionReference | Problem {
339
+ try {
340
+ return prepareMotionReference(input);
341
+ } catch (err) {
342
+ if (!(err instanceof MeshReductionError)) throw err;
343
+ return { code: 'AUTO_MESH_MOTION_INPUT', object, detail: `rig-c's compareMeshesInMotion refused the comparison's input (${err.code}): ${err.message}; nothing is built in the part's place` };
344
+ }
345
+ }
346
+
347
+ /** Compare a fresh gated candidate under the prepared reference and its bound schedule/art/options. */
348
+ export function runPreparedComparison(object: string, handle: PreparedMotionReference, input: MotionComparisonInput): MeshQualityReport | Problem {
349
+ try {
350
+ return comparePreparedMotion(handle, input);
351
+ } catch (err) {
352
+ if (!(err instanceof MeshReductionError)) throw err;
353
+ return { code: 'AUTO_MESH_MOTION_INPUT', object, detail: `rig-c's compareMeshesInMotion refused the comparison's input (${err.code}): ${err.message}; nothing is built in the part's place` };
354
+ }
355
+ }
356
+
337
357
  /** A gated row in one phrase: code, region, state, value against its bound, the worst frame. */
338
358
  export function motionRowText(r: MeasureRow): string {
339
359
  const region = r.object.region === null ? '' : ` [region "${r.object.region}"]`;
@@ -0,0 +1,59 @@
1
+ /** Explicit alternate region placement; source pixels and crop provenance never change. */
2
+ import { toBoneLocalVector, toWorld, type BoneTransform } from './coords.ts';
3
+ import { refuseIfAny, type Problem } from './errors.ts';
4
+
5
+ export type BaseAnchor = Record<string, { axes: Array<'x' | 'y'>; paired?: string[] }>;
6
+ const object = (v: unknown): v is Record<string, unknown> => typeof v === 'object' && v !== null && !Array.isArray(v);
7
+
8
+ /** Both config doors, serialized parts, and direct builders validate the same explicit rule. */
9
+ export function baseAnchorProblems(value: unknown, at: string, covered: ReadonlySet<string>, patches?: ReadonlySet<string>, regions?: ReadonlySet<string>, code = 'CONFIG_SET_BASE_ANCHOR'): Problem[] {
10
+ const problems: Problem[] = [];
11
+ const fail = (where: string, detail: string): void => { problems.push({ code, object: where, detail }); };
12
+ if (!object(value) || Object.keys(value).length === 0) return [{ code, object: at, detail: 'requires a nonempty region-name map of {axes: ["x" and/or "y"], paired?: [patch names]}; omit base_anchor for art-measured placement' }];
13
+ const paired = new Set<string>();
14
+ for (const [name, rule] of Object.entries(value)) {
15
+ const path = `${at}.${name}`;
16
+ if (!covered.has(name) || (regions !== undefined && !regions.has(name))) fail(path, `names "${name}"; a covered, nonhidden region with a base attachment is required, never a mesh`);
17
+ if (!object(rule)) { fail(path, 'requires {axes: ["x" and/or "y"], paired?: [patch names]}'); continue; }
18
+ for (const key of Object.keys(rule)) if (key !== 'axes' && key !== 'paired') fail(`${path}.${key}`, 'is unknown; only axes and paired are accepted');
19
+ if (!Array.isArray(rule.axes) || rule.axes.length === 0 || rule.axes.some(a => a !== 'x' && a !== 'y') || new Set(rule.axes).size !== rule.axes.length) fail(`${path}.axes`, `is ${JSON.stringify(rule.axes)}; one or both distinct local axes "x", "y" are required`);
20
+ if (rule.paired !== undefined) {
21
+ if (!Array.isArray(rule.paired) || rule.paired.length === 0) { fail(`${path}.paired`, 'requires a nonempty list of covered painting patches'); continue; }
22
+ for (const patch of rule.paired) {
23
+ if (typeof patch !== 'string' || !covered.has(patch) || (patches !== undefined && !patches.has(patch)) || (regions !== undefined && !regions.has(patch))) fail(`${path}.paired`, `names ${JSON.stringify(patch)}; a covered, nonhidden painting-patch region is required`);
24
+ else if (patch === name || Object.hasOwn(value, patch) || paired.has(patch)) fail(`${path}.paired`, `names "${patch}" more than once or also as an anchor; one anchor owns each paired patch`);
25
+ else paired.add(patch);
26
+ }
27
+ }
28
+ }
29
+ return problems;
30
+ }
31
+
32
+ export function orderedBaseAnchor(value: BaseAnchor): BaseAnchor {
33
+ return Object.fromEntries(Object.keys(value).sort().map(name => [name, { axes: (['x', 'y'] as const).filter(a => value[name].axes.includes(a)), ...(value[name].paired === undefined ? {} : { paired: [...value[name].paired!].sort() }) }]));
34
+ }
35
+
36
+ export interface AnchorRegion { bone: string; x: number; y: number }
37
+ /** Local displacement for every owner and explicitly paired region; pairs share a world displacement. */
38
+ export function baseAnchorMoves(rules: BaseAnchor, base: ReadonlyMap<string, AnchorRegion>, source: ReadonlyMap<string, AnchorRegion>, world: ReadonlyMap<string, BoneTransform>): Map<string, { local: [number, number]; world: [number, number] }> {
39
+ const problems: Problem[] = [];
40
+ const out = new Map<string, { local: [number, number]; world: [number, number] }>();
41
+ for (const name of Object.keys(rules).sort()) {
42
+ const original = base.get(name), art = source.get(name), rule = rules[name];
43
+ if (original === undefined || art === undefined || !world.has(art.bone) || original.bone !== art.bone) {
44
+ problems.push({ code: 'RIG_ALT_BASE_ANCHOR', object: `base_anchor.${name}`, detail: `requires matching base and alternate region attachments on the same declared bone; base ${original?.bone ?? 'absent'}, alternate ${art?.bone ?? 'absent'}` }); continue;
45
+ }
46
+ const frame = world.get(art.bone)!;
47
+ const dx = rule.axes.includes('x') ? original.x - art.x : 0, dy = rule.axes.includes('y') ? original.y - art.y : 0;
48
+ const origin = toWorld(frame, 0, 0), target = toWorld(frame, dx, dy);
49
+ const delta: [number, number] = [target[0] - origin[0], target[1] - origin[1]];
50
+ out.set(name, { local: [dx, dy], world: delta });
51
+ for (const pair of rule.paired ?? []) {
52
+ const region = source.get(pair), pairFrame = region === undefined ? undefined : world.get(region.bone);
53
+ if (pairFrame === undefined) problems.push({ code: 'RIG_ALT_BASE_ANCHOR', object: `base_anchor.${name}.paired`, detail: `paired patch "${pair}" has no alternate region or bone frame; re-run assemble with the patch covered` });
54
+ else out.set(pair, { local: toBoneLocalVector(pairFrame, ...delta), world: delta });
55
+ }
56
+ }
57
+ refuseIfAny(problems);
58
+ return out;
59
+ }
package/src/build.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { readSourceDepth } from './depthsheets.ts';
2
+ import { orderedBaseAnchor } from './baseanchor.ts';
1
3
  import { alternateLayerSources, alternateLayerIslands, alternateLayerOthers } from './alternatelayers.ts';
2
4
  import { setFocusProblems } from './setfocus.ts';
3
5
  /**
@@ -49,7 +51,7 @@ import { type AnimFrame, EncodeError, encodeApng, encodeIndexedApng, INDEXED_DEF
49
51
  import { assemble, type AssembleResult, cutLines, type ExcludedCount, excludedLines, setExclusions, islandLines, setIslandSources, type IslandCount, hiddenLines, SINGLE_RUN_FACE_TAGS, singleRunHeadBox, driftLine, limbLines, FIGURE_MASK_FILE, figuresLine, holeKindsLine, holeLines, measureRecomposite, type ProjectRule, recomposite, recompositeRecord, scenePhrase, type SeamRule, sourceInRig, stageFields, uncoveredLine } from './assemble.ts';
50
52
  import { allocationClause, type Reducer, stageBClause, unboundedClause } from './automesh.ts';
51
53
  import { residualClause } from './autoenvelope.ts';
52
- import { type AutoMotionCase, failingRows, idleSchedule, localRow, motionClause, motionDeformation, motionDocument, motionInput, motionRowText, motionStimulus, motionVerdict, noStimulusProblem, type ReplayCandidate, runComparison, sourceSelfFailure, stimulusMotion, withStimulus } from './automotion.ts';
54
+ import { type AutoMotionCase, failingRows, idleSchedule, localRow, motionClause, motionDeformation, motionDocument, motionInput, motionRowText, motionStimulus, motionVerdict, noStimulusProblem, prepareComparison, type ReplayCandidate, runComparison, runPreparedComparison, sourceSelfFailure, stimulusMotion, withStimulus } from './automotion.ts';
53
55
  import {
54
56
  bisectAccepted,
55
57
  budgetProblem,
@@ -321,7 +323,7 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
321
323
  const fields = stageFields(cfg, runs.single ? { w: src.width, h: src.height } : null);
322
324
  const under = readUnders(input.config, fields.cuts);
323
325
  const caps = fields.limbs === undefined ? undefined : readCaps(input.config, fields.limbs);
324
- const result = assemble({ source: src, full: runs.full, head: runs.head, ...fields, under, ...(caps === undefined ? {} : { caps }), seamRule: input.seam, projectRule: input.project });
326
+ const result = assemble({ source: src, full: runs.full, head: runs.head, depth: cfg.depth, depthBase: dirname(resolve(input.config)), ...fields, under, ...(caps === undefined ? {} : { caps }), seamRule: input.seam, projectRule: input.project });
325
327
 
326
328
  // Issue #454: alternate painting sets, assembled before any file is written.
327
329
  const configDir = dirname(resolve(input.config));
@@ -331,6 +333,7 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
331
333
  for (const a of cliAlts) cliByName.set(a.name, a);
332
334
  const mergedAlts: Array<{
333
335
  name: string;
336
+ base_anchor?: NonNullable<typeof cfg.assemble.alternates>[number]['base_anchor'];
334
337
  at: string;
335
338
  source?: string;
336
339
  full?: string;
@@ -351,6 +354,7 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
351
354
  if (cli !== undefined) cliByName.delete(a.name);
352
355
  return {
353
356
  name: a.name,
357
+ base_anchor: a.base_anchor,
354
358
  at: `config.assemble.alternates[${i}] (set "${a.name}")`,
355
359
  source: cli?.source ?? (a.source === undefined ? undefined : resolve(configDir, a.source)),
356
360
  full: cli?.full ?? (a.full === undefined ? undefined : resolve(configDir, a.full)),
@@ -635,6 +639,7 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
635
639
  try {
636
640
  altRes = assemble({
637
641
  source: altSrc,
642
+ depth: cfg.depth, depthBase: configDir,
638
643
  limbUnderCover: alt.parts !== undefined && input.setCover !== 'painting',
639
644
  ...(altTips === undefined ? {} : { boneTips: altTips }),
640
645
  capOwnership: input.setCapOwnership ?? 'target',
@@ -674,8 +679,8 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
674
679
  }
675
680
  const keptImages = altRes.images
676
681
  .filter(({ record }) => coveredSet.has(record.name))
677
- .map(({ record, image }) => ({ name: `${alt.name}_${record.name}`, image }));
678
- const keptRecords = altRes.parts.parts.filter((p) => coveredSet.has(p.name));
682
+ .flatMap(({ record, image, depthImage }) => [{ name: `${alt.name}_${record.name}`, image }, ...(depthImage === undefined || record.depth === undefined ? [] : [{ name: `${alt.name}_${record.depth.image.slice(0, -4)}`, image: depthImage }])]);
683
+ const keptRecords = altRes.parts.parts.filter((p) => coveredSet.has(p.name)).map(p => p.depth === undefined ? p : { ...p, depth: { ...p.depth, image: `${alt.name}_${p.depth.image}` } });
679
684
  const slots: Record<string, string> = {};
680
685
  for (const r of keptRecords) slots[altFront.has(r.name) ? `${r.name}${FRONT_SUFFIX}` : r.name] = `${alt.name}_${r.name}`;
681
686
  let altRecomposite = altRes.parts.recomposite as RecompositeRecord;
@@ -692,6 +697,7 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
692
697
  altBuilt.push({
693
698
  record: {
694
699
  name: alt.name,
700
+ ...(alt.base_anchor === undefined ? {} : { base_anchor: orderedBaseAnchor(alt.base_anchor) }),
695
701
  ...(alt.parts === undefined ? {} : { parts: [...alt.parts, ...generatedBands.filter((n) => !(alt.parts as string[]).includes(n))] }),
696
702
  ...(alt.hides === undefined || alt.hides.length === 0 ? (altFront.size === 0 ? {} : { hides: [...altFront] }) : { hides: [...alt.hides, ...[...altFront].filter((p) => !(alt.hides as string[]).includes(p))] }),
697
703
  ...(altFront.size === 0 ? {} : { front: [...altFront] }),
@@ -715,6 +721,26 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
715
721
  capSplits: altRes.capSplits,
716
722
  });
717
723
  }
724
+ const emittedAssets = [
725
+ ...result.images.flatMap(({ record, depthImage }) => [
726
+ `${record.name}.png`,
727
+ ...(depthImage !== undefined && record.depth !== undefined ? [record.depth.image] : []),
728
+ ]),
729
+ ...altBuilt.flatMap((b) => b.images.map(({ name }) => `${name}.png`)),
730
+ ];
731
+ const assetIndices = new Map<string, number>();
732
+ emittedAssets.forEach((file, index) => {
733
+ const previous = assetIndices.get(file);
734
+ if (previous !== undefined) {
735
+ altProblems.push({
736
+ code: 'DEPTH_SHEET_UNAMBIGUOUS',
737
+ object: file,
738
+ detail: `output assets[${previous}] and [${index}] require the same filename; choose distinct base, alternate and sheet names`,
739
+ });
740
+ } else {
741
+ assetIndices.set(file, index);
742
+ }
743
+ });
718
744
  refuseIfAny(altProblems);
719
745
  if (altBuilt.length > 0) {
720
746
  result.parts.alternates = altBuilt.map((b) => b.record);
@@ -726,7 +752,11 @@ export function assembleStage(input: AssembleStageInput, outs: AssembleOutputs,
726
752
  mkdirSync(dirname(outs.recomposite), { recursive: true });
727
753
  mkdirSync(dirname(outs.errorMap), { recursive: true });
728
754
  writeParts(outs.partsJson, result.parts);
729
- for (const { record, image } of result.images) writePng(join(outs.partsDir, `${record.name}.png`), image);
755
+ for (const { record, image, depthImage } of result.images) {
756
+ writePng(join(outs.partsDir, `${record.name}.png`), image);
757
+ if (depthImage !== undefined && record.depth !== undefined) writePng(join(outs.partsDir, record.depth.image), depthImage);
758
+ }
759
+ for (const row of result.depthReadings ?? []) log(` depth reading ${row.from}: ${row.reading.relation}; ordering median ${row.reading.orderingMedian}, source-sheet median ${row.reading.sheetMedian} (half-level quantization; reading only, polarity declared)`);
730
760
  for (const b of altBuilt) {
731
761
  for (const { name, image } of b.images) writePng(join(outs.partsDir, `${name}.png`), image);
732
762
  }
@@ -1206,6 +1236,9 @@ export function replaySearch(
1206
1236
  }
1207
1237
  const selection = motion.selection;
1208
1238
  if (selection !== undefined && selection.maxProbes < maxReplays(N, I)) return refuse(budgetProblem(c.object, selection.maxProbes, N, I));
1239
+ const { candidates: _unused, ...referenceInput } = motionInput(c, motion, refModel, refModel, probeSchedule);
1240
+ const prepared = prepareComparison(c.object, referenceInput);
1241
+ if ('code' in prepared) return refuse(prepared);
1209
1242
  const kept = new Map<number, ReplayCandidate>();
1210
1243
  const probeFrames: string[] = [];
1211
1244
  let fatal: Problem | null = null;
@@ -1234,7 +1267,7 @@ export function replaySearch(
1234
1267
  };
1235
1268
  return refused(`rigc exit ${gate.status}`);
1236
1269
  }
1237
- const cmp = runComparison(c.object, motionInput(c, motion, refModel, model, probeSchedule));
1270
+ const cmp = runPreparedComparison(c.object, prepared, motionInput(c, motion, refModel, model, probeSchedule));
1238
1271
  if ('code' in cmp) {
1239
1272
  fatal = cmp;
1240
1273
  return refused(cmp.code);
@@ -1358,17 +1391,21 @@ export function readRigInputs(config: string, partsDir: string): RigInputs {
1358
1391
  const { config: cfg, animations: from } = loadConfigAndAnimations(config);
1359
1392
  const parts = readParts(join(partsDir, 'parts.json'));
1360
1393
  const images = new Map<string, Raster>();
1394
+ const depthProblems: Problem[] = [];
1361
1395
  for (const p of parts.parts) {
1362
1396
  const png = join(partsDir, 'parts', `${p.name}.png`);
1363
1397
  if (existsSync(png)) images.set(p.name, readPng(png));
1398
+ if (p.depth !== undefined && images.has(p.name)) { try { images.set(p.depth.image, readSourceDepth(join(partsDir, 'parts', p.depth.image), images.get(p.name) as Raster, null).pixels); } catch (error) { if (error instanceof PartsError) depthProblems.push(...error.problems); else throw error; } }
1364
1399
  }
1365
1400
  for (const alt of parts.alternates ?? []) {
1366
1401
  for (const p of alt.records) {
1367
1402
  const attName = `${alt.name}_${p.name}`;
1368
1403
  const png = join(partsDir, 'parts', `${attName}.png`);
1369
1404
  if (existsSync(png)) images.set(attName, readPng(png));
1405
+ if (p.depth !== undefined && images.has(attName)) { try { images.set(p.depth.image, readSourceDepth(join(partsDir, 'parts', p.depth.image), images.get(attName) as Raster, null).pixels); } catch (error) { if (error instanceof PartsError) depthProblems.push(...error.problems); else throw error; } }
1370
1406
  }
1371
1407
  }
1408
+ refuseIfAny(depthProblems);
1372
1409
  return { cfg, from, parts, images };
1373
1410
  }
1374
1411
 
@@ -1380,6 +1417,7 @@ export function rigOutputTexts(r: RigOutput, animations: Parameters<typeof withA
1380
1417
  ['rig.json', rigJsonText(r.rig)],
1381
1418
  ['motion.json', rigJsonText(mergedMotion)],
1382
1419
  ['mesh_report.json', rigJsonText(r.meshReport)],
1420
+ ...(r.depthReferences === undefined ? [] : [['depth_sheets.json', rigJsonText({ spec: 'rig-parts-depth-sheets/1', parts: r.depthReferences })] as [string, string]]),
1383
1421
  ...(r.sets === undefined ? [] : [[SETS_FILE, rigJsonText(r.sets)] as [string, string]]),
1384
1422
  ];
1385
1423
  }
package/src/check.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { baseAnchorMoves, type AnchorRegion } from './baseanchor.ts';
1
2
  /**
2
3
  * The check stage: build a rig through rig-c, gate it under both
3
4
  * profiles, render its idle, and measure the two things the gate cannot see —
@@ -1379,6 +1380,25 @@ export function resolvePlacedAlpha(placements: readonly ReferencePlacement[], pa
1379
1380
  return { name: pl.name, from: pl.box.from, x: pl.box.x, y: pl.box.y, image: islands[pl.islandIndex] ?? raw };
1380
1381
  }
1381
1382
  return { name: pl.name, from: pl.box.from, x: pl.box.x, y: pl.box.y, image: raw };
1383
+ }).map(placed => {
1384
+ if (Number.isInteger(placed.x) && Number.isInteger(placed.y)) return placed;
1385
+ // Explicit anchored placement can be subpixel. Preserve premultiplied edge
1386
+ // colour while moving it onto the integer reference grid, never the PNG.
1387
+ const { image } = placed, premult = newFloatImage(image.width, image.height, 4);
1388
+ for (let i = 0; i < image.width * image.height; i++) {
1389
+ const alpha = image.data[i * 4 + 3];
1390
+ for (let c = 0; c < 3; c++) premult.data[i * 4 + c] = image.data[i * 4 + c] * alpha / 255;
1391
+ premult.data[i * 4 + 3] = alpha;
1392
+ }
1393
+ const x = Math.floor(placed.x) - 1, y = Math.floor(placed.y) - 1;
1394
+ const warped = warpAffine(premult, { sx: 1, sy: 1, tx: placed.x - x, ty: placed.y - y }, image.width + 2, image.height + 2, 'bilinear');
1395
+ const shifted = newRaster(warped.width, warped.height);
1396
+ for (let i = 0; i < shifted.width * shifted.height; i++) {
1397
+ const alpha = warped.data[i * 4 + 3];
1398
+ for (let c = 0; c < 3; c++) shifted.data[i * 4 + c] = alpha > 0 ? warped.data[i * 4 + c] * 255 / alpha : 0;
1399
+ shifted.data[i * 4 + 3] = alpha;
1400
+ }
1401
+ return { ...placed, x, y, image: shifted };
1382
1402
  });
1383
1403
  }
1384
1404
 
@@ -1545,6 +1565,29 @@ export function armSplitSetHint(parts: PartsFile, alt: AlternatePartsRecord): st
1545
1565
  return ` — the set covers a sleeve whose merged arm layer 1.37.0 split (issue #490), and it neither covers nor hides ${open.map((n) => `"${n}"`).join(', ')}, so the base draws ${open.length === 1 ? 'that part' : 'those parts'} beside the set's own art; a set made before 1.37.0 hides it: add ${open.map((n) => `"${n}"`).join(', ')} to the set's hides (see ${ARM_SPLIT_MIGRATION})`;
1546
1566
  }
1547
1567
 
1568
+ /** Expected placement from the authored rule and base, never the candidate alternate's offset. */
1569
+ export function baseAnchoredReference(parts: PartsFile, alt: AlternatePartsRecord, rig: Record<string, unknown>, drawn?: Record<string, number>): AlternatePartsRecord {
1570
+ if (alt.base_anchor === undefined) return alt;
1571
+ const posed = setupWorld(stillRigForSet(rig, alt, drawn ?? alt.drawn));
1572
+ const slots = new Map((Array.isArray(rig.slots) ? rig.slots : []).filter(isRecord).map(s => [String(s.name), String(s.bone)]));
1573
+ const skins = isRecord(rig.skins) && isRecord(rig.skins.default) ? rig.skins.default : {};
1574
+ const source = new Map<string, AnchorRegion>(), base = new Map<string, AnchorRegion>();
1575
+ for (const record of alt.records) {
1576
+ const slot = (alt.front ?? []).includes(record.name) ? `${record.name}${FRONT_SUFFIX}` : record.name;
1577
+ const bone = slots.get(slot), frame = bone === undefined ? undefined : posed.get(bone);
1578
+ const list = skins[record.name], original = isRecord(list) ? list[record.name] : null;
1579
+ if (bone === undefined || frame === undefined) continue;
1580
+ const local = toBoneLocal(frame, record.x + record.w / 2 - parts.rig_size[0] / 2, cropToSpineY(record.y + record.h / 2, parts.rig_size[1]));
1581
+ source.set(record.name, { bone, x: local[0], y: local[1] });
1582
+ if (isRecord(original) && typeof original.x === 'number' && typeof original.y === 'number' && (original.type === undefined || original.type === 'region')) base.set(record.name, { bone, x: original.x, y: original.y });
1583
+ }
1584
+ const moves = baseAnchorMoves(alt.base_anchor, base, source, posed);
1585
+ return { ...alt, records: alt.records.map(record => {
1586
+ const move = moves.get(record.name);
1587
+ return move === undefined ? record : { ...record, x: record.x + move.world[0], y: record.y - move.world[1] };
1588
+ }) };
1589
+ }
1590
+
1548
1591
  /**
1549
1592
  * The flat stack of parts for an alternate set (issue #454), in the order
1550
1593
  * {@link setPlacements} gives: with `slotOrder`, a front part (issue #664) is
@@ -6349,7 +6392,7 @@ export function runCheck(rigDir: string, outDir: string, rigc: RigcRunner, parts
6349
6392
  writeFileSync(join(dir, 'motion.json'), JSON.stringify({ ...inp.motion, animations: setStillAnimations }));
6350
6393
  const built = rigc(['build', '--rig', join(dir, 'rig.json'), '--motion', join(dir, 'motion.json'), '--out', join(dir, 'build'), '--profile', 'spine']);
6351
6394
  if (built.status !== 0) refuseIfAny(rigcFailed(`build (the setup-pose still of set "${alt.name}")`, built, buildGateLines(built.out)));
6352
- return { alt, dir };
6395
+ return { alt, reference: baseAnchoredReference(inp.parts!, alt, inp.rig, drawn), dir };
6353
6396
  });
6354
6397
  const states: SeamStateBounds[] = [seamStateBounds(join(stillDir, 'build'), 'base')];
6355
6398
  for (const { alt, dir } of staged) states.push(seamStateBounds(join(dir, 'build'), alt.name));
@@ -6382,15 +6425,15 @@ export function runCheck(rigDir: string, outDir: string, rigc: RigcRunner, parts
6382
6425
  }
6383
6426
  if (set !== undefined || allSets) {
6384
6427
  const baseSupport = seamSupport(join(stillDir, 'build'), join(stillDir, 'rig.json'), canonicalSet, placed, seamStacks, inp.stage);
6385
- for (const { alt, dir: setDir } of staged) {
6428
+ for (const { alt, reference, dir: setDir } of staged) {
6386
6429
  const asr = rigc(['render', '--candidate', join(setDir, 'build'), '--animation', 'still', '--fps', String(STILL_FPS), '--max', String(maxSide), '--out', join(setDir, 'render')]);
6387
6430
  if (asr.status !== 0) refuseIfAny(rigcFailed(`render (the setup-pose still of set "${alt.name}")`, asr, asr.out.split('\n').filter((l) => l.includes('FAIL'))));
6388
6431
  const altStillSet = readFrameSet(join(setDir, 'render'));
6389
6432
  if (keepStills) cpSync(join(setDir, 'render'), join(out, 'setup_still_auto', alt.name), { recursive: true });
6390
- const altComposite = flatCompositeForSet(inp.parts, alt, inp.partsDir, altStillSet.background, rigSlotOrder);
6433
+ const altComposite = flatCompositeForSet(inp.parts, reference, inp.partsDir, altStillSet.background, rigSlotOrder);
6391
6434
  const altCanonical = canonical(join(setDir, 'build'), join(setDir, 'seam'));
6392
6435
  if (keepStills) cpSync(join(setDir, 'seam'), join(keptDir, alt.name), { recursive: true });
6393
- const altPlaced = resolvePlacedAlpha(setPlacements(inp.parts, alt, rigSlotOrder), inp.partsDir);
6436
+ const altPlaced = resolvePlacedAlpha(setPlacements(inp.parts, reference, rigSlotOrder), inp.partsDir);
6394
6437
  const altSupport = seamSupport(join(setDir, 'build'), join(setDir, 'rig.json'), altCanonical, altPlaced, seamStacks, inp.stage);
6395
6438
  const effects = seamEffectSelection(baseSupport, altSupport);
6396
6439
  const autoAltSeam = seamFigures(altComposite, altStillSet.frames[0].image, inp.stage, altStillSet.viewport, altStillSet.background, undefined, alt.scene !== undefined);
@@ -31,6 +31,7 @@ import { basename, dirname, join } from 'node:path';
31
31
  import { refuseIfAny } from '../errors.ts';
32
32
  import { checkGraph, fillSeeThrough, type SeeThroughParams } from '../graphs.ts';
33
33
  import { readWrapperLayers } from '../layers.ts';
34
+ import type { DepthBinding } from '../depthsheets.ts';
34
35
  import { decodePngBytes } from '../raster/png.ts';
35
36
  import { ComfyClient, historyImages, refuse } from './client.ts';
36
37
 
@@ -50,6 +51,8 @@ export interface SeeThroughRun {
50
51
  quant: 'none' | 'nf4';
51
52
  wait: number;
52
53
  timeout: number;
54
+ /** Explicit config/run context; absent means legacy art-only capture. */
55
+ depth?: DepthBinding;
53
56
  }
54
57
 
55
58
  export interface SeeThroughResult {
@@ -73,6 +76,7 @@ function safeName(name: string): boolean {
73
76
  }
74
77
 
75
78
  export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say: (line: string) => void): Promise<SeeThroughResult> {
79
+ const depth = run.depth?.config === undefined ? undefined : run.depth;
76
80
  if (!existsSync(run.image)) refuse('COMFY_IMAGE_PRESENT', run.image, 'no such file; the PNG See-through is to decompose is required');
77
81
  const bytes = new Uint8Array(readFileSync(run.image));
78
82
  const img = decodePngBytes(bytes, run.image);
@@ -122,7 +126,7 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
122
126
  }
123
127
  const layers = Array.isArray(manifest.layers) ? (manifest.layers as Array<Record<string, unknown>>) : [];
124
128
  if (layers.length === 0) refuse('COMFY_MANIFEST_NAMED', `output/${infoLog}`, 'lists no layers; a See-through run writes one per tag');
125
- const bad = layers.filter((l) => typeof l.name !== 'string' || !safeName(l.name) || typeof l.filename !== 'string' || !safeName(l.filename));
129
+ const bad = layers.filter((l) => typeof l.name !== 'string' || !safeName(l.name) || typeof l.filename !== 'string' || !safeName(l.filename) || (depth !== undefined && 'depth_filename' in l && (typeof l.depth_filename !== 'string' || !safeName(l.depth_filename))));
126
130
  if (bad.length > 0) {
127
131
  refuse('COMFY_LAYER_NAME', `output/${infoLog}`, `names ${bad.length} layer(s) whose name or filename is not a plain file name (first ${JSON.stringify(bad[0]).slice(0, 200)}); each becomes parts/<name>.png`);
128
132
  }
@@ -135,8 +139,9 @@ export async function runSeeThrough(client: ComfyClient, run: SeeThroughRun, say
135
139
  try {
136
140
  for (const p of previews) writeFileSync(join(stage, 'previews', basename(p.filename)), await client.view(p.filename, p.subfolder, p.type));
137
141
  for (const l of layers) writeFileSync(join(stage, 'parts', `${l.name as string}.png`), await client.view(l.filename as string));
142
+ if (depth !== undefined) for (const l of layers) if ('depth_filename' in l) writeFileSync(join(stage, 'parts', `${l.name as string}_depth.png`), await client.view(l.depth_filename as string));
138
143
  writeFileSync(join(stage, 'layers.json'), manifestBytes);
139
- const set = readWrapperLayers(stage);
144
+ const set = readWrapperLayers(stage, depth);
140
145
  const meta = {
141
146
  prompt_id: id,
142
147
  image: run.image,