@rig-c/parts 1.58.0 → 1.60.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/rig.ts CHANGED
@@ -172,7 +172,7 @@ import { coalesceLimbSlots } from './limbslots.ts';
172
172
  import { layoutAlias, placeLimbLayouts, type LayoutPlan } from './limblayout.ts';
173
173
  import { artCoverage, ART_ALPHA, latticeCells, latticeMesh, latticeStray, ONE_LOOP_PASSES } from './mesh.ts';
174
174
  import { bindFaceSurfaceDeform, BLINK, blinkHoldMisses, blinkSpan, CONTROL_SUFFIX, controlledBones, IDLE_FPS, idleMotion, type MotionSpec, moveKeysToControls } from './motion.ts';
175
- import { type AlternateSetMap, PAINTING_RUN, type PartsFile, readFrom, tagOf } from './parts.ts';
175
+ import { type AlternateSetMap, FRONT_SUFFIX, PAINTING_RUN, type PartsFile, readFrom, tagOf } from './parts.ts';
176
176
  import { readTag } from './tags.ts';
177
177
  import { EYE_GROUP_TAGS } from './propose.ts';
178
178
  import { alphaAbove, crop, pad, type Raster } from './raster/index.ts';
@@ -185,6 +185,11 @@ import { DISTANCE_EXPONENT, type FloorForm, type Influence, influences, type Inf
185
185
  /** Transparent pixels added round every part image — the reference's `PAD`. */
186
186
  export const PAD = 4;
187
187
 
188
+ /** Issue #671: the MIGRATION entry for 1.37.0's arm split (#490), by its heading's anchor. */
189
+ export const ARM_SPLIT_MIGRATION = 'docs/MIGRATION.md#after-1370-merged-arm-layers-are-split-issue-490';
190
+ /** Issue #671: the command that adds only what a config lacks for the arm split's parts: their plan rows and their entries. */
191
+ export const ARM_SPLIT_REMEDY = 'rig-parts propose --parts <parts> --source <painting> --out <dir> --write-config <config> --only-missing';
192
+
188
193
  /**
189
194
  * The places a bone's rotation and every local offset (a bone's `x, y`, a
190
195
  * weight's bind `x, y`, a region's `x, y` and `rotation`) are written to.
@@ -507,7 +512,8 @@ export interface RigSpec {
507
512
  images: 'images';
508
513
  skeleton: { x: number; y: number; width: number; height: number };
509
514
  bones: RigBone[];
510
- slots: Array<{ name: string; bone: string; attachment: string }>;
515
+ /** `attachment` null only on a limb segment's front slot (issue #664), which shows nothing at setup. */
516
+ slots: Array<{ name: string; bone: string; attachment: string | null }>;
511
517
  skins: { default: Record<string, Record<string, MeshAttachment | RegionAttachment>> };
512
518
  /**
513
519
  * `config.constraints`, in the order written, each as the config wrote it
@@ -669,6 +675,7 @@ function applyTurn(
669
675
  slots: RigSpec['slots'],
670
676
  skin: RigSpec['skins']['default'],
671
677
  constraints: readonly ConfigConstraint[],
678
+ frontSlots: ReadonlySet<string> = new Set(),
672
679
  ): { constraints: Array<Record<string, unknown>>; detached: DetachedRule[]; rows: TurnRow[] } {
673
680
  const problems: Problem[] = [];
674
681
  const taken = new Set(bones.map((b) => b.name));
@@ -679,7 +686,8 @@ function applyTurn(
679
686
  const row: TurnRow = { ...f, twins: [] };
680
687
  rows.push(row);
681
688
  if (f.fraction === 0) continue;
682
- const mine = slots.filter((sl) => sl.name === f.part || sl.name === `${f.part}${STILL_SUFFIX}`);
689
+ // A declared front slot (issue #664) rides its part; a part merely named `<part>_front` is a part of its own.
690
+ const mine = slots.filter((sl) => sl.name === f.part || sl.name === `${f.part}${STILL_SUFFIX}` || (sl.name === `${f.part}${FRONT_SUFFIX}` && frontSlots.has(sl.name)));
683
691
  const bound: string[] = [];
684
692
  const bind = (b: string): void => {
685
693
  if (!bound.includes(b)) bound.push(b);
@@ -706,7 +714,7 @@ function applyTurn(
706
714
  const twinOf = new Map(row.twins.map((t) => [t.bone, t.twin]));
707
715
  for (const t of row.twins) bones.push({ name: t.twin, parent: t.bone, x: 0, y: 0 });
708
716
  for (const sl of slots) {
709
- if (sl.name !== row.part && sl.name !== `${row.part}${STILL_SUFFIX}`) continue;
717
+ if (sl.name !== row.part && sl.name !== `${row.part}${STILL_SUFFIX}` && !(sl.name === `${row.part}${FRONT_SUFFIX}` && frontSlots.has(sl.name))) continue;
710
718
  sl.bone = twinOf.get(sl.bone) as string;
711
719
  const atts = skin[sl.name] ?? {};
712
720
  for (const [k, att] of Object.entries(atts)) {
@@ -1398,6 +1406,11 @@ export function buildRig(
1398
1406
  }
1399
1407
 
1400
1408
  // ---- parts ↔ config ----------------------------------------------------
1409
+ // Issue #671: a part 1.37.0's arm split (#490) makes from the full run is new to a config proposed before it.
1410
+ const armSplitHint = (from: string): string =>
1411
+ /^full:(drape|hand)-[rl]$/.test(from)
1412
+ ? ` — the full run's merged arm layer was split into this part (issue #490, from 1.37.0), which a config proposed before 1.37.0 does not name; see ${ARM_SPLIT_MIGRATION}, and add only its plan row and entry with: ${ARM_SPLIT_REMEDY}`
1413
+ : '';
1401
1414
  const where = (part: string): string => `parts.json holds ${parts.parts.length} part(s): ${parts.parts.map((p) => p.name).join(', ')}`;
1402
1415
  for (const part of Object.keys(cfg.meshes)) if (!byPart.has(part)) fail('RIG_PART_PRESENT', `config.meshes.${part}`, `names the part "${part}", which is not in parts.json; ${where(part)}`);
1403
1416
  for (const [part, bone] of Object.entries(cfg.regions)) {
@@ -1418,7 +1431,7 @@ export function buildRig(
1418
1431
  for (const p of parts.parts) {
1419
1432
  const inM = p.name in cfg.meshes;
1420
1433
  const inR = p.name in cfg.regions;
1421
- if (!inM && !inR) fail('RIG_PART_ATTACHED', `part "${p.name}"`, 'is in parts.json and has neither a config.meshes nor a config.regions entry; exactly one is required');
1434
+ if (!inM && !inR) fail('RIG_PART_ATTACHED', `part "${p.name}"`, `is in parts.json and has neither a config.meshes nor a config.regions entry; exactly one is required${armSplitHint(p.from)}`);
1422
1435
  if (inM && inR) fail('RIG_PART_ATTACHED', `part "${p.name}"`, 'has both a config.meshes and a config.regions entry; exactly one is required');
1423
1436
  else if (inM && readFrom(p.from)?.run === PAINTING_RUN) {
1424
1437
  fail('RIG_PART_ATTACHED', `part "${p.name}"`, `is a painting patch (${p.from}) and has a config.meshes entry; a patch is a region — config.regions.${p.name} names its bone`);
@@ -2394,6 +2407,28 @@ export function buildRig(
2394
2407
  });
2395
2408
  }
2396
2409
  refuseIfAny(problems);
2410
+ // ---- front slots (issue #664) ------------------------------------------
2411
+ // Only when a limb declares `front`: each named segment part gets `<part>_front` on its base slot's bone, showing
2412
+ // nothing at setup. They ride with every slot through stops and turn, but stay out of the limb closure (#646) and
2413
+ // are placed after their target's family once the setup order is final.
2414
+ const frontSlots: RigSpec['slots'] = [];
2415
+ const frontTargets: Array<{ slot: string; after: string; at: string }> = [];
2416
+ for (const [limbName, limb] of Object.entries(cfg.limbs ?? {})) {
2417
+ if (limb.front === undefined) continue;
2418
+ for (const part of limb.front.parts) {
2419
+ const at = `config.limbs.${limbName}.front`;
2420
+ const base = slots.find((sl) => sl.name === part);
2421
+ if (base === undefined) {
2422
+ fail('RIG_NAME_RESOLVES', `${at}.parts`, `names "${part}", which has no slot in the base rig; a front slot rides its segment's base slot bone`);
2423
+ continue;
2424
+ }
2425
+ frontSlots.push({ name: `${part}${FRONT_SUFFIX}`, bone: base.bone, attachment: null });
2426
+ frontTargets.push({ slot: `${part}${FRONT_SUFFIX}`, after: limb.front.after, at });
2427
+ }
2428
+ }
2429
+ refuseIfAny(problems);
2430
+ // Only declared front slots are front slots: a part authored as `<part>_front` is an ordinary part (review of #667).
2431
+ const declaredFront = new Set(frontSlots.map((sl) => sl.name));
2397
2432
  // ---- alternate painting sets (issues #454, #465) ------------------------
2398
2433
  // Only when `parts.alternates` is present: each alternate set's `<set>_<part>`
2399
2434
  // attachments are added to `skins.default[<part>]` on the existing bones and
@@ -2413,13 +2448,21 @@ export function buildRig(
2413
2448
  fail('RIG_ALT_TIP_POINT', `parts.json alternates[${altIdx}].tips.${bone}`, 'requires a finite [x, y] endpoint in rig-crop pixels');
2414
2449
  }
2415
2450
  }
2451
+ // Issue #664: one segment is drawn by one slot of a set, its base slot or its front slot, never both.
2452
+ for (const slotName of Object.keys(alt.slots)) {
2453
+ if (declaredFront.has(`${slotName}${FRONT_SUFFIX}`) && Object.hasOwn(alt.slots, `${slotName}${FRONT_SUFFIX}`)) {
2454
+ fail('RIG_SET_FRONT_BOTH', `parts.json alternates[${altIdx}] (set "${alt.name}") part "${slotName}"`, `maps both slot "${slotName}" and its front slot "${slotName}${FRONT_SUFFIX}"; a set draws a segment on its base slot or on its front slot, not both — re-run assemble`);
2455
+ }
2456
+ }
2457
+ const altFront = new Set(alt.front ?? []);
2416
2458
  refuseIfAny(problems);
2417
2459
  const explicitDrawn = alt.drawn ?? cfgAlt?.drawn;
2418
2460
  const isSubsetSet = alt.parts !== undefined || cfgAlt?.parts !== undefined;
2419
2461
  let coveredBones: Set<string> | null = null;
2420
2462
  if (isSubsetSet) {
2421
2463
  coveredBones = new Set<string>();
2422
- for (const pName of Object.keys(alt.slots)) {
2464
+ for (const slotName of Object.keys(alt.slots)) {
2465
+ const pName = declaredFront.has(slotName) && altFront.has(slotName.slice(0, -FRONT_SUFFIX.length)) ? slotName.slice(0, -FRONT_SUFFIX.length) : slotName;
2423
2466
  if (pName in cfg.regions) coveredBones.add(cfg.regions[pName]);
2424
2467
  for (const s of meshSegments.get(pName) ?? []) coveredBones.add(s.bone);
2425
2468
  const m = cfg.meshes[pName];
@@ -2477,13 +2520,16 @@ export function buildRig(
2477
2520
  const attName = `${alt.name}_${p.name}`;
2478
2521
  const rawImg = images.get(attName);
2479
2522
  if (rawImg === undefined) continue;
2523
+ // Issue #664: a part the set draws on its front slot files its attachment under that slot; bones are shared.
2524
+ const skinSlot = altFront.has(p.name) ? `${p.name}${FRONT_SUFFIX}` : p.name;
2525
+ skin[skinSlot] ??= {};
2480
2526
  const img = pad(rawImg, PAD, PAD, PAD, PAD, [0, 0, 0, 0]);
2481
2527
  const file = `${attName}.png`;
2482
2528
  outImages.push([file, img]);
2483
2529
  const mesh = cfg.meshes[p.name];
2484
2530
  if (mesh === undefined) {
2485
2531
  const bone = B.get(cfg.regions[p.name]) as Bone;
2486
- skin[p.name][attName] = altRegion(file, bone.name, p.x + p.w / 2, p.y + p.h / 2);
2532
+ skin[skinSlot][attName] = altRegion(file, bone.name, p.x + p.w / 2, p.y + p.h / 2);
2487
2533
  continue;
2488
2534
  }
2489
2535
  const fullSegs = meshSegments.get(p.name) as Segment[];
@@ -2535,7 +2581,7 @@ export function buildRig(
2535
2581
  const r = 'r' in mesh ? mesh.r : 80;
2536
2582
  const lm = latticeMesh(attName, art, grid, maxLoopPasses);
2537
2583
  if ('code' in lm) {
2538
- skin[p.name][attName] = altRegion(file, segs[0].bone, p.x + p.w / 2, p.y + p.h / 2);
2584
+ skin[skinSlot][attName] = altRegion(file, segs[0].bone, p.x + p.w / 2, p.y + p.h / 2);
2539
2585
  continue;
2540
2586
  }
2541
2587
  const ox = p.x - PAD;
@@ -2598,7 +2644,7 @@ export function buildRig(
2598
2644
  }
2599
2645
  const uvs: number[] = [];
2600
2646
  for (const [x, y] of lm.vertices) uvs.push(pyRound(x / w, 6), pyRound(y / h, 6));
2601
- skin[p.name][attName] = { type: 'mesh', image: file, width: w, height: h, uvs, triangles: lm.triangles, hull: lm.hull, weights };
2647
+ skin[skinSlot][attName] = { type: 'mesh', image: file, width: w, height: h, uvs, triangles: lm.triangles, hull: lm.hull, weights };
2602
2648
  }
2603
2649
  const slotMap: Record<string, string> = {};
2604
2650
  for (const [slotName, att] of Object.entries(alt.slots)) slotMap[slotName] = att;
@@ -2635,11 +2681,12 @@ export function buildRig(
2635
2681
  // ---- leg stops (issue #404) --------------------------------------------
2636
2682
  // Only when the config has `stops`: each link after the first (and any bone hanging from a stopped link) hangs
2637
2683
  // from the drawn link `<link>_d` above it, and slots and weights on a stopped link bind `<link>_d`.
2638
- const stops = cfg.stops === undefined ? null : applyStops(cfg.stops, chains, bones, slots, skin, (original, emitted) => layoutAlias(explicitLayout.plan, original, emitted));
2684
+ const rebound = frontSlots.length === 0 ? slots : [...slots, ...frontSlots];
2685
+ const stops = cfg.stops === undefined ? null : applyStops(cfg.stops, chains, bones, rebound, skin, (original, emitted) => layoutAlias(explicitLayout.plan, original, emitted));
2639
2686
  // ---- the turn control (issue #251) -------------------------------------
2640
2687
  // Only when the config has `turn`: every other build adds nothing here, so no byte moves.
2641
2688
  if (cfg.turn !== undefined) refuseIfAny(turnRiderProblems(cfg.turn, cfg, parts.parts));
2642
- const turned = cfg.turn === undefined ? null : applyTurn(cfg.turn, parts.parts.map((p) => p.name), bones, slots, skin, cfg.constraints ?? []);
2689
+ const turned = cfg.turn === undefined ? null : applyTurn(cfg.turn, parts.parts.map((p) => p.name), bones, rebound, skin, cfg.constraints ?? [], declaredFront);
2643
2690
  for (const row of turned?.rows ?? []) for (const t of row.twins) layoutAlias(explicitLayout.plan, t.bone, t.twin);
2644
2691
  // issue #536: every row names the bones the emitted attachment binds, read once the turn and the stops have
2645
2692
  // rebound the weights; a rig with neither rebinds nothing, so this reads back the names each row already holds.
@@ -2655,7 +2702,7 @@ export function buildRig(
2655
2702
  images: 'images',
2656
2703
  skeleton: { x: -CX, y: 0, width: W, height: H },
2657
2704
  bones,
2658
- slots: explicitLayout.slots,
2705
+ slots: frontSlots.length === 0 ? explicitLayout.slots : spliceFrontSlots(explicitLayout.slots, frontSlots, frontTargets, slotFamilies),
2659
2706
  skins: { default: skin },
2660
2707
  };
2661
2708
  if (cfg.constraints !== undefined) rig.constraints = cfg.constraints.map(constraintForRig);
@@ -2712,6 +2759,32 @@ export function buildRig(
2712
2759
  };
2713
2760
  }
2714
2761
 
2762
+ /**
2763
+ * Issue #664: the final setup slot order with each front slot placed
2764
+ * immediately after its target part's whole slot family (the part's slot and
2765
+ * its `<part>_still`), wherever the closure (#646) left that family's last
2766
+ * member; several front slots after one family keep the order the config
2767
+ * declares them in. A target with no slot in the order is refused by name.
2768
+ */
2769
+ function spliceFrontSlots(order: RigSpec['slots'], fronts: RigSpec['slots'], targets: ReadonlyArray<{ slot: string; after: string; at: string }>, families: ReadonlyMap<string, readonly string[]>): RigSpec['slots'] {
2770
+ const problems: Problem[] = [];
2771
+ const out = [...order];
2772
+ for (const [i, f] of fronts.entries()) {
2773
+ const t = targets[i];
2774
+ const family = families.get(t.after) ?? [];
2775
+ const last = Math.max(-1, ...family.map((n) => out.findIndex((s) => s.name === n)));
2776
+ if (last < 0) {
2777
+ problems.push({ code: 'RIG_NAME_RESOLVES', object: `${t.at}.after`, detail: `names "${t.after}", which has no slot in the rig's setup order; a front slot is placed after a part's slot family` });
2778
+ continue;
2779
+ }
2780
+ let at = last + 1;
2781
+ while (at < out.length && fronts.slice(0, i).includes(out[at])) at++;
2782
+ out.splice(at, 0, f);
2783
+ }
2784
+ refuseIfAny(problems);
2785
+ return out;
2786
+ }
2787
+
2715
2788
  /**
2716
2789
  * Issue #473: expand `{"t": …, "set": "<name>" | null}` keys on slot attachment
2717
2790
  * tracks in `merged.animations` into `{"t": …, "v": "<attachment>" | null}`
@@ -2844,6 +2917,23 @@ export function expandSetAttachmentKeys(rig: RigSpec, sets: AttachmentSetsFile |
2844
2917
  });
2845
2918
  return;
2846
2919
  }
2920
+ // Issue #664: a set that draws this track's part on its front slot moves the part in the draw order, which a
2921
+ // keyed attachment swap cannot do; keyed draw-order changes are out of scope.
2922
+ // A declared front slot is the one slot the rig writes with no setup attachment.
2923
+ const isFront = (name: string): boolean => rig.slots.some((sl) => sl.name === name && sl.attachment === null);
2924
+ const frontBase = slot.endsWith(FRONT_SUFFIX) && isFront(slot) ? slot.slice(0, -FRONT_SUFFIX.length) : null;
2925
+ const drawsFront =
2926
+ (isFront(`${slot}${FRONT_SUFFIX}`) && Object.hasOwn(entry.slots, `${slot}${FRONT_SUFFIX}`) && entry.hides?.includes(slot) === true) ||
2927
+ (frontBase !== null && Object.hasOwn(entry.slots, slot) && entry.hides?.includes(frontBase) === true);
2928
+ if (drawsFront) {
2929
+ const part = frontBase ?? slot;
2930
+ problems.push({
2931
+ code: 'CONFIG_SET_FRONT_KEYED',
2932
+ object: keyAt,
2933
+ detail: `names alternate set "${setVal}" on slot "${slot}", and set "${setVal}" draws "${part}" on its front slot "${part}${FRONT_SUFFIX}" (hiding "${part}"); a keyed set switch on a front part would change the draw order mid-clip, which is out of scope — show the set with setSet, or key a part the set draws on its base slot`,
2934
+ });
2935
+ return;
2936
+ }
2847
2937
  if (Object.hasOwn(entry.slots, slot)) {
2848
2938
  resolvedV = entry.slots[slot];
2849
2939
  } else if (entry.hides?.includes(slot) === true) {
package/src/scene.ts CHANGED
@@ -1115,7 +1115,7 @@ export function composeScene(
1115
1115
  const sized = s === 1 ? {} : { x: scaledBy(bone.x, s), y: scaledBy(bone.y, s), ...(bone.length === undefined ? {} : { length: scaledBy(bone.length, s) }) };
1116
1116
  bones.push({ ...bone, name: P(id, bone.name), ...(bone.parent === undefined ? {} : { parent: P(id, bone.parent) }), ...sized, ...(first ? { x: placed(bone.x, dx, s), y: placed(bone.y, dy, s) } : {}) });
1117
1117
  }
1118
- for (const s of b.rig.slots) slotDefs.set(prefixed(id, s.name), { ...s, name: prefixed(id, s.name), bone: P(id, s.bone), attachment: prefixed(id, s.attachment) });
1118
+ for (const s of b.rig.slots) slotDefs.set(prefixed(id, s.name), { ...s, name: prefixed(id, s.name), bone: P(id, s.bone), attachment: s.attachment === null ? null : prefixed(id, s.attachment) });
1119
1119
  // Everything the character places in root's own frame moves with it: a region on a slot root carries, and a
1120
1120
  // weight bound to root. Every other offset is local to a bone the shift already moved.
1121
1121
  const slotBone = new Map(b.rig.slots.map((s) => [s.name, s.bone]));
@@ -1305,7 +1305,8 @@ export function unprefixedNames(rig: RigSpec, motion: MotionSpec, ids: readonly
1305
1305
  for (const s of rig.slots) {
1306
1306
  name('slot', s.name);
1307
1307
  name(`slot ${s.name} bone`, s.bone);
1308
- name(`slot ${s.name} attachment`, s.attachment);
1308
+ // Issue #664: a front slot shows nothing at setup; null names nothing.
1309
+ if (s.attachment !== null) name(`slot ${s.name} attachment`, s.attachment);
1309
1310
  }
1310
1311
  for (const [slot, atts] of Object.entries(rig.skins.default)) {
1311
1312
  name('skin slot', slot);
@@ -199,5 +199,5 @@ export function stageConfigWrite(target: ConfigWriteTarget | FreshConfigWrite, p
199
199
  }
200
200
  /** Print to stderr so proposal JSON on stdout remains machine-readable. */
201
201
  export function configWriteLines(target: ConfigWriteTarget, changes: readonly ConfigWriteChange[]): string[] {
202
- return changes.length === 0 ? [`config ${target.path}: unchanged`] : changes.map((c) => `config ${target.path}: ${c.kind} ${c.path}`);
202
+ return changes.length === 0 ? [`config ${target.path}: unchanged`] : changes.map((c) => `config ${target.path}: ${c.kind} ${c.path}${c.value === undefined ? '' : ` ${c.value}`}`);
203
203
  }