@ikijs/mcp 0.8.0 → 0.9.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/README.md CHANGED
@@ -22,8 +22,8 @@ Turns a set of role-named, full-canvas transparent PNG layers into a renderable,
22
22
 
23
23
  - **`layers`** — array of `{ path, fileName? }`. `path` is a PNG file path (resolved against the server's working directory); the role is derived from `fileName ?? basename(path)`. Required roles: `face`, `eye_L`, `eye_R`, `mouth`. Optional roles include `iris_L`/`iris_R`, `brow_L`/`brow_R`, `hair_front`/`hair_back`, `lash_L`/`lash_R`, etc. All layers must share the same canvas size (taken from the first layer).
24
24
  - **`outputPath`** — optional `.iki` output path (resolved against the working directory; the parent directory must already exist, and the path must end in `.iki`). Defaults to `auto-rigged-model.iki`.
25
- - **`turnTargets`** — optional head-turn cues to fit the rig to, in the units `measure_turn_reference` reports off a front/turned reference pair: `eyeShift` (plus `noseShift` and `mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width — a **magnitude**, since the rig turns both ways and the sign is the turn's own business; `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest; `silhouetteRatio` the head's half-width turned over at rest. An omitted field falls back to a built-in default, which is _clamped_ to what the layer set can do; a target you **do** pass that it cannot reach is refused, because that number is a measurement rather than a preference. Only a layer set carrying a `nose` solves a turn at all.
26
- - **Result** — on success the result text is the written file path and `structuredContent` carries `{ ok: true, path, canvas, partCount, atlasBytes, headHalfWidth?, headHalfWidthApplied, headEdges?, turn? }`. `headHalfWidth` is the head measured here rather than asked for: the layers' own opaque union at the eye row, under the same `alpha >= 128` rule and ±10-row band `measure_turn_reference` spans a render with, so the rig's own rest render measures that span back; it is **absent** when that union has no span there at all (translucent art painted below alpha 128 — still a valid layer set, just with nothing to measure). `headHalfWidthApplied` is `false` when the measured value is absent, or no wider than the face plate (a hairless set, or one widest at the plate itself) — the generator will not take such a head, so the plate stands in for it and the rig is still produced either way. `headEdges` is a companion to a measured, applied `headHalfWidth` (absent otherwise): every role with an opaque pixel in that same eye-row band, per side, each with its own rest x there — passed straight to the generator's own `options.headEdges` so it can tell which part's own deformation actually carries the union's outermost pixel through the turn. `turn` is what the solve settled on — `{ radius, holdBase, depths, achieved: { eyeShift, farEyeRatio, silhouetteRatio }, clamped }`, where `clamped` names the _defaulted_ targets this layer set could not reach and `achieved` is what the rig does reach; it is absent without a `nose` layer. The (potentially multi-MB) model is written to disk, not inlined. Invalid input (unknown/missing role, empty layer, mismatched sizes, bad path, oversized atlas, a `turnTargets` field that is not a number or that this layer set cannot reach — the message names the field and the attainable range) returns `{ ok: false, error }` with a `INVALID: …` text — not a protocol error.
25
+ - **`turnTargets`** — optional head-turn cues to fit the rig to, in the units `measure_turn_reference` reports off a front/turned reference pair: `eyeShift` (plus `noseShift` and `mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width — a **magnitude**, since the rig turns both ways and the sign is the turn's own business; `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest; `silhouetteRatio` the head's half-width turned over at rest. An omitted field falls back to a built-in default, which is _clamped_ to what the layer set can do, and so is an `eyeShift` you pass that runs past the room the art leaves the far eye — the face plate's edge, or the bangs' side strand over the far iris — since that room is a fact about these layers, not about the reference. Any other target you **do** pass that it cannot reach is refused, including an `eyeShift` smaller than the slide the face's own turn already gives the eyes, because that number is a measurement rather than a preference. Only a layer set carrying a `nose` solves a turn at all.
26
+ - **Result** — on success the result text is the written file path and `structuredContent` carries `{ ok: true, path, canvas, partCount, atlasBytes, headHalfWidth?, headHalfWidthApplied, headEdges?, strandEdges?, turn? }`. `headHalfWidth` is the head measured here rather than asked for: the layers' own opaque union at the eye row, under the same `alpha >= 128` rule and ±10-row band `measure_turn_reference` spans a render with, so the rig's own rest render measures that span back; it is **absent** when that union has no span there at all (translucent art painted below alpha 128 — still a valid layer set, just with nothing to measure). `headHalfWidthApplied` is `false` when the measured value is absent, or no wider than the face plate (a hairless set, or one widest at the plate itself) — the generator will not take such a head, so the plate stands in for it and the rig is still produced either way. `headEdges` is a companion to a measured, applied `headHalfWidth` (absent otherwise): every role with an opaque pixel in that same eye-row band, per side, each with its own rest x there — passed straight to the generator's own `options.headEdges` so it can tell which part's own deformation actually carries the union's outermost pixel through the turn. `strandEdges` is each side's iris and the `hair_front` run it would slide under on the turn, measured whenever the layers include `hair_front`, `iris_L` and `iris_R`: the iris's opaque span and the run's outer and face-side ends as pixel edges on the row holding the iris's centre (a run that reaches the other iris's centre, a fringe spanning the face, has `runFace: null`; a run narrower than half the iris's painted width on that row is hair detail the iris may cross, and is skipped as if clear) — passed straight to the generator's own `options.strandEdges`, which keeps the far iris from sliding under the bangs any further than it is painted. `turn` is what the solve settled on — `{ radius, holdBase, depths, achieved: { eyeShift, farEyeRatio, silhouetteRatio }, clamped, strandOverlap? }`, where `achieved` is what the rig does reach and `clamped` names every target the rig cut down: each _defaulted_ target this layer set could not reach, plus a passed `eyeShift` that the face plate's edge or the strand cut short. `strandOverlap` gives, per side whose far iris the bangs still cover at some far stop, the covered width — how much of the iris's painted row the run covers where that is largest (`deg`, `px`, and `hh` in head half-widths), how much it covers at rest (`restPx`), and whether the bound held (`held`: `true` means the iris is kept no deeper under the run than it is painted; `false` means the fitted radius could not do that — always so for a fringe spanning the face — so the eyes' turn depth is 0 and `px` is what stays covered); it is absent when nothing is covered. `turn` is absent without a `nose` layer. The (potentially multi-MB) model is written to disk, not inlined. Invalid input (unknown/missing role, empty layer, mismatched sizes, bad path, oversized atlas, a `turnTargets` field that is not a number or that this layer set cannot reach — any passed field other than an `eyeShift` past the art's room, and a passed `eyeShift` below the face's own slide; the message names the field and the attainable range) returns `{ ok: false, error }` with a `INVALID: …` text — not a protocol error.
27
27
 
28
28
  The decode/atlas pipeline runs in Node via `sharp` (a native dependency confined to this package); it mirrors the browser editor app's import flow and reuses the pure `@ikijs/editor` model + atlas math, so both paths produce the same rig.
29
29
 
@@ -757,6 +757,7 @@ function facePlateHalfOf(layers) {
757
757
  }
758
758
  return face.cropW / 2;
759
759
  }
760
+ var STRAND_MIN_RUN_FRACTION = 0.5;
760
761
  async function autoRigFromLayers(input) {
761
762
  try {
762
763
  const layers = input.layers;
@@ -793,6 +794,7 @@ async function autoRigFromLayers(input) {
793
794
  const layerInputs = [];
794
795
  const crops = [];
795
796
  const rowSpansByRole = [];
797
+ let hairFrontMask;
796
798
  for (let i = 0; i < resolvedLayers.length; i++) {
797
799
  const { resolved, fileName } = resolvedLayers[i];
798
800
  const png = await decodePng(resolved);
@@ -837,17 +839,20 @@ async function autoRigFromLayers(input) {
837
839
  const buffer = await cropToBuffer(png.rgba, png.width, png.height, bbox);
838
840
  const rowLeft = new Int32Array(canvasH).fill(canvasW);
839
841
  const rowRight = new Int32Array(canvasH).fill(-1);
842
+ const ownMask = role === "hair_front" ? new Uint8Array(canvasW * canvasH) : void 0;
840
843
  for (let y = 0; y < canvasH; y++) {
841
844
  for (let x = 0; x < canvasW; x++) {
842
845
  const p = y * canvasW + x;
843
846
  if (png.rgba[p * 4 + 3] < ALPHA_OPAQUE) continue;
844
847
  opaque[p] = 1;
848
+ if (ownMask !== void 0) ownMask[p] = 1;
845
849
  if (x < rowLeft[y]) rowLeft[y] = x;
846
850
  if (x > rowRight[y]) rowRight[y] = x;
847
851
  }
848
852
  }
849
853
  rowSpansByRole.push({ role, rowLeft, rowRight });
850
- layerInputs.push({
854
+ if (ownMask !== void 0) hairFrontMask = ownMask;
855
+ const layer = {
851
856
  role,
852
857
  fileName,
853
858
  canvasW,
@@ -855,7 +860,17 @@ async function autoRigFromLayers(input) {
855
860
  bbox,
856
861
  cropW: bbox.w,
857
862
  cropH: bbox.h
858
- });
863
+ };
864
+ if (role === "face") {
865
+ const rowHalfWidths = [];
866
+ for (let y = bbox.y; y < bbox.y + bbox.h; y++) {
867
+ rowHalfWidths.push(
868
+ rowRight[y] >= rowLeft[y] ? headHalfOf({ left: rowLeft[y], right: rowRight[y] }) : 0
869
+ );
870
+ }
871
+ layer.rowHalfWidths = rowHalfWidths;
872
+ }
873
+ layerInputs.push(layer);
859
874
  crops.push({ id: role, buffer, width: bbox.w, height: bbox.h });
860
875
  }
861
876
  const eyeRow = eyeRowOf(layerInputs);
@@ -882,6 +897,63 @@ async function autoRigFromLayers(input) {
882
897
  }
883
898
  headEdges = { left, right };
884
899
  }
900
+ let strandEdges;
901
+ const irisLayers = layerInputs.filter((l) => l.role === "iris_L" || l.role === "iris_R").sort((a, b) => a.bbox.x + a.bbox.w / 2 - (b.bbox.x + b.bbox.w / 2));
902
+ if (hairFrontMask !== void 0 && irisLayers.length === 2) {
903
+ const mask = hairFrontMask;
904
+ const half = canvasW / 2;
905
+ const strandOn = (iris, other, side) => {
906
+ const r = Math.floor(iris.bbox.y + iris.bbox.h / 2);
907
+ const centre = iris.bbox.x + iris.bbox.w / 2;
908
+ const otherX = other.bbox.x + other.bbox.w / 2 - half;
909
+ const c = side < 0 ? Math.ceil(centre) - 1 : Math.floor(centre);
910
+ const span2 = rowSpansByRole.find((s) => s.role === iris.role);
911
+ if (span2.rowRight[r] < span2.rowLeft[r]) return void 0;
912
+ const minRun = STRAND_MIN_RUN_FRACTION * (span2.rowRight[r] + 1 - span2.rowLeft[r]);
913
+ const strand = new Uint8Array(canvasW);
914
+ for (let a = 0; a < canvasW; ) {
915
+ if (mask[r * canvasW + a] !== 1) {
916
+ a++;
917
+ continue;
918
+ }
919
+ let b = a;
920
+ while (b < canvasW && mask[r * canvasW + b] === 1) b++;
921
+ if (b - a >= minRun) strand.fill(1, a, b);
922
+ a = b;
923
+ }
924
+ const strandAt = (x2) => x2 >= 0 && x2 < canvasW && strand[x2] === 1;
925
+ const outerBoundary = (x2) => (side < 0 ? x2 : x2 + 1) - half;
926
+ let x = c;
927
+ let runFace;
928
+ if (!strandAt(c)) {
929
+ while (x >= 0 && x < canvasW && !strandAt(x)) x += side;
930
+ if (x < 0 || x >= canvasW) return void 0;
931
+ runFace = outerBoundary(x - side);
932
+ } else {
933
+ let f = c - side;
934
+ while (strandAt(f)) f -= side;
935
+ const face = outerBoundary(f);
936
+ runFace = side * (face - otherX) > 0 ? face : null;
937
+ }
938
+ while (strandAt(x)) x += side;
939
+ return {
940
+ y: canvasH / 2 - (r + 0.5),
941
+ irisOuter: (side < 0 ? span2.rowLeft[r] : span2.rowRight[r] + 1) - half,
942
+ irisInner: (side < 0 ? span2.rowRight[r] + 1 : span2.rowLeft[r]) - half,
943
+ runOuter: outerBoundary(x - side),
944
+ runFace
945
+ };
946
+ };
947
+ const [low, high] = irisLayers;
948
+ const left = strandOn(low, high, -1);
949
+ const right = strandOn(high, low, 1);
950
+ if (left !== void 0 || right !== void 0) {
951
+ strandEdges = {
952
+ ...left === void 0 ? {} : { left },
953
+ ...right === void 0 ? {} : { right }
954
+ };
955
+ }
956
+ }
885
957
  let turn;
886
958
  let model;
887
959
  try {
@@ -901,7 +973,8 @@ async function autoRigFromLayers(input) {
901
973
  onTurnSolved: (report) => {
902
974
  turn = report;
903
975
  },
904
- ...headEdges === void 0 ? {} : { headEdges }
976
+ ...headEdges === void 0 ? {} : { headEdges },
977
+ ...strandEdges === void 0 ? {} : { strandEdges }
905
978
  }
906
979
  );
907
980
  } catch (e) {
@@ -948,6 +1021,7 @@ async function autoRigFromLayers(input) {
948
1021
  ...headHalfWidth === void 0 ? {} : { headHalfWidth },
949
1022
  headHalfWidthApplied,
950
1023
  ...headEdges === void 0 ? {} : { headEdges },
1024
+ ...strandEdges === void 0 ? {} : { strandEdges },
951
1025
  ...turn === void 0 ? {} : { turn }
952
1026
  };
953
1027
  } catch (err) {
@@ -1491,7 +1565,7 @@ async function composeLayersFromParts(input) {
1491
1565
  import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
1492
1566
  import { z } from "zod";
1493
1567
  function createIkiMcpServer() {
1494
- const server = new McpServer({ name: "iki", version: "0.8.0" });
1568
+ const server = new McpServer({ name: "iki", version: "0.9.0" });
1495
1569
  server.registerTool(
1496
1570
  "validate_iki",
1497
1571
  {
@@ -1579,7 +1653,7 @@ function createIkiMcpServer() {
1579
1653
  noseShift: z.number().optional(),
1580
1654
  mouthShift: z.number().optional()
1581
1655
  }).optional().describe(
1582
- "What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do (the result's `turn.clamped` lists the ones that were, `turn.achieved` what the rig reaches); a target you DO pass that this layer set cannot reach comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference."
1656
+ "What the head turn is fitted to, as `measure_turn_reference` reports it off a front/turned reference pair. `eyeShift` (and the optional `noseShift`/`mouthShift`, derived from it when absent) is how far that feature slides across the head at full turn, as a fraction of the head's half-width \u2014 a MAGNITUDE: the rig turns both ways, so the sign is ignored. `farEyeRatio` is the far/near eye width at full turn over that same ratio at rest, `silhouetteRatio` the head's half-width turned over at rest. The half-width the shifts are fractions of is measured off the layers themselves, so it is not an input. An omitted field falls back to a default, which is clamped to what this layer set can do, and so is a passed `eyeShift` past the room the art leaves the far eye \u2014 the face plate's edge, or the bangs' side strand over the far iris (the result's `turn.clamped` lists every clamped field, `turn.achieved` what the rig reaches, and `turn.strandOverlap` how much of a far iris the bangs still cover). Any other passed target this layer set cannot reach, including an `eyeShift` smaller than the slide the face's own turn already gives the eyes, comes back as `INVALID: \u2026` naming the field and the attainable range, since it is a measurement rather than a preference."
1583
1657
  )
1584
1658
  }
1585
1659
  },
@@ -1720,4 +1794,4 @@ export {
1720
1794
  composeLayersFromParts,
1721
1795
  createIkiMcpServer
1722
1796
  };
1723
- //# sourceMappingURL=chunk-JPWDKP43.mjs.map
1797
+ //# sourceMappingURL=chunk-R6LYJVYC.mjs.map