@openbone/mcp 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +143 -2
- package/dist/atlas-schema.d.ts +149 -0
- package/dist/atlas-schema.js +42 -0
- package/dist/audio-meta.d.ts +6 -0
- package/dist/audio-meta.js +139 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +80 -0
- package/dist/bridge.d.ts +16 -0
- package/dist/bridge.js +151 -0
- package/dist/export.d.ts +97 -0
- package/dist/export.js +310 -0
- package/dist/guide.d.ts +1 -0
- package/dist/guide.js +83 -0
- package/dist/image-load.d.ts +9 -0
- package/dist/image-load.js +40 -0
- package/dist/image-ops.d.ts +77 -0
- package/dist/image-ops.js +235 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +8 -0
- package/dist/local-only.d.ts +7 -0
- package/dist/local-only.js +38 -0
- package/dist/media.d.ts +27 -0
- package/dist/media.js +124 -0
- package/dist/render.d.ts +81 -0
- package/dist/render.js +224 -0
- package/dist/server.d.ts +28 -0
- package/dist/server.js +354 -0
- package/dist/session.d.ts +16 -0
- package/dist/session.js +43 -0
- package/dist/tools-extra.d.ts +4 -0
- package/dist/tools-extra.js +355 -0
- package/dist/tools-image.d.ts +3 -0
- package/dist/tools-image.js +176 -0
- package/package.json +67 -3
package/dist/media.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { decodeWav, encodeWav } from "@openbone/export";
|
|
6
|
+
/**
|
|
7
|
+
* Sound decoding and MP4 / WebM encoding for exports in Node. WAV is decoded here; other audio (MP3, OGG, FLAC…) and
|
|
8
|
+
* the MP4 (H.264 + AAC) and WebM (VP9 + Opus) encoders come from an `ffmpeg` found on the PATH (or OPENBONE_FFMPEG).
|
|
9
|
+
* ffmpeg is used as an external program, not bundled, so this package keeps its permissive licence; without it, WAV
|
|
10
|
+
* sound still works and MP4 / WebM say how to get them (the editor makes them with the browser's encoders).
|
|
11
|
+
*/
|
|
12
|
+
let ffmpegPath;
|
|
13
|
+
/** The ffmpeg executable, or null when there is none. */
|
|
14
|
+
export function findFfmpeg() {
|
|
15
|
+
if (ffmpegPath !== undefined)
|
|
16
|
+
return ffmpegPath;
|
|
17
|
+
const candidate = process.env.OPENBONE_FFMPEG || "ffmpeg";
|
|
18
|
+
try {
|
|
19
|
+
const r = spawnSync(candidate, ["-hide_banner", "-version"], { encoding: "utf8" });
|
|
20
|
+
ffmpegPath = r.status === 0 ? candidate : null;
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
ffmpegPath = null;
|
|
24
|
+
}
|
|
25
|
+
return ffmpegPath;
|
|
26
|
+
}
|
|
27
|
+
/** Run ffmpeg with `input` on stdin; resolves with stdout. */
|
|
28
|
+
function ffmpeg(args, input) {
|
|
29
|
+
const bin = findFfmpeg();
|
|
30
|
+
if (!bin)
|
|
31
|
+
return Promise.reject(new Error("ffmpeg not found"));
|
|
32
|
+
return new Promise((resolve, reject) => {
|
|
33
|
+
const p = spawn(bin, ["-hide_banner", "-loglevel", "error", ...args], { stdio: ["pipe", "pipe", "pipe"] });
|
|
34
|
+
const out = [];
|
|
35
|
+
let err = "";
|
|
36
|
+
p.stdout.on("data", (d) => out.push(d));
|
|
37
|
+
p.stderr.on("data", (d) => (err += d.toString()));
|
|
38
|
+
p.on("error", reject);
|
|
39
|
+
p.on("close", (code) => (code === 0 ? resolve(Buffer.concat(out)) : reject(new Error(`ffmpeg failed (${code}): ${err.trim().slice(0, 500)}`))));
|
|
40
|
+
p.stdin.on("error", () => {
|
|
41
|
+
/* ffmpeg closed its input early: the exit code says why */
|
|
42
|
+
});
|
|
43
|
+
const write = (b) => new Promise((res) => {
|
|
44
|
+
if (p.stdin.write(b))
|
|
45
|
+
res();
|
|
46
|
+
else
|
|
47
|
+
p.stdin.once("drain", () => res());
|
|
48
|
+
});
|
|
49
|
+
if (typeof input === "function")
|
|
50
|
+
void input(write)
|
|
51
|
+
.then(() => p.stdin.end())
|
|
52
|
+
.catch(reject);
|
|
53
|
+
else if (input)
|
|
54
|
+
p.stdin.end(input);
|
|
55
|
+
else
|
|
56
|
+
p.stdin.end();
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Decode an audio file: WAV in-process, anything else through ffmpeg (stereo float at `sampleRate`). Null when it
|
|
61
|
+
* can't be decoded (no ffmpeg, or a broken file).
|
|
62
|
+
*/
|
|
63
|
+
export async function decodeAudio(bytes, sampleRate = 48000) {
|
|
64
|
+
const wav = decodeWav(bytes);
|
|
65
|
+
if (wav)
|
|
66
|
+
return wav;
|
|
67
|
+
if (!findFfmpeg())
|
|
68
|
+
return null;
|
|
69
|
+
try {
|
|
70
|
+
const raw = await ffmpeg(["-i", "pipe:0", "-f", "f32le", "-ac", "2", "-ar", String(sampleRate), "pipe:1"], bytes);
|
|
71
|
+
const f = new Float32Array(raw.buffer, raw.byteOffset, Math.floor(raw.byteLength / 4));
|
|
72
|
+
const n = f.length >> 1;
|
|
73
|
+
const l = new Float32Array(n);
|
|
74
|
+
const r = new Float32Array(n);
|
|
75
|
+
for (let i = 0; i < n; i++) {
|
|
76
|
+
l[i] = f[i * 2];
|
|
77
|
+
r[i] = f[i * 2 + 1];
|
|
78
|
+
}
|
|
79
|
+
return { sampleRate, channels: [l, r] };
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Encode frames (and an optional sound track) as MP4 (H.264 + AAC, yuv420p, plays everywhere) or WebM (VP9 + Opus)
|
|
87
|
+
* with ffmpeg. `quality` 0..1 maps to the encoder's constant-quality setting.
|
|
88
|
+
*/
|
|
89
|
+
export async function encodeVideoFfmpeg(frames, o) {
|
|
90
|
+
if (!findFfmpeg())
|
|
91
|
+
throw new Error(`${o.format.toUpperCase()} export needs ffmpeg on the PATH (or OPENBONE_FFMPEG set to it). Install ffmpeg, or export ${o.format.toUpperCase()} from the editor (it uses the browser's encoders), or use AVI / MOV.`);
|
|
92
|
+
const { width, height } = frames[0];
|
|
93
|
+
// H.264 / yuv420p need even sizes.
|
|
94
|
+
const w = width + (width % 2);
|
|
95
|
+
const h = height + (height % 2);
|
|
96
|
+
const dir = mkdtempSync(path.join(tmpdir(), "openbone-video-"));
|
|
97
|
+
try {
|
|
98
|
+
const out = path.join(dir, `out.${o.format}`);
|
|
99
|
+
const args = ["-y", "-f", "rawvideo", "-pix_fmt", "rgba", "-s", `${width}x${height}`, "-r", String(o.fps), "-i", "pipe:0"];
|
|
100
|
+
if (o.audio) {
|
|
101
|
+
const wav = path.join(dir, "sound.wav");
|
|
102
|
+
writeFileSync(wav, encodeWav(o.audio));
|
|
103
|
+
args.push("-i", wav);
|
|
104
|
+
}
|
|
105
|
+
const q = Math.max(0, Math.min(1, o.quality ?? 0.85));
|
|
106
|
+
if (w !== width || h !== height)
|
|
107
|
+
args.push("-vf", `pad=${w}:${h}:0:0`);
|
|
108
|
+
if (o.format === "mp4")
|
|
109
|
+
args.push("-c:v", "libx264", "-pix_fmt", "yuv420p", "-crf", String(Math.round(35 - q * 20)), "-preset", "medium", "-movflags", "+faststart");
|
|
110
|
+
else
|
|
111
|
+
args.push("-c:v", "libvpx-vp9", "-pix_fmt", "yuv420p", "-b:v", "0", "-crf", String(Math.round(45 - q * 20)), "-row-mt", "1");
|
|
112
|
+
if (o.audio)
|
|
113
|
+
args.push(...(o.format === "mp4" ? ["-c:a", "aac", "-b:a", "192k"] : ["-c:a", "libopus", "-b:a", "160k"]), "-shortest");
|
|
114
|
+
args.push(out);
|
|
115
|
+
await ffmpeg(args, async (write) => {
|
|
116
|
+
for (const f of frames)
|
|
117
|
+
await write(f.data instanceof Uint8Array ? f.data : new Uint8Array(f.data.buffer, f.data.byteOffset, f.data.byteLength));
|
|
118
|
+
});
|
|
119
|
+
return new Uint8Array(readFileSync(out));
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
rmSync(dir, { recursive: true, force: true });
|
|
123
|
+
}
|
|
124
|
+
}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type Canvas, type Image } from "@napi-rs/canvas";
|
|
2
|
+
import { type AtlasData, type OpenBoneDocument } from "@openbone/core";
|
|
3
|
+
import { type AtlasEnv, type AtlasScaleVariant, type BuildAtlasOptions, type BuiltAtlas } from "@openbone/render-canvas";
|
|
4
|
+
import { OpenBoneInstance, type ScriptHost } from "@openbone/runtime";
|
|
5
|
+
import type { ProjectSession } from "./session.js";
|
|
6
|
+
export interface Step {
|
|
7
|
+
/** Seconds to advance after applying this step's inputs. */
|
|
8
|
+
advance?: number;
|
|
9
|
+
setInputs?: Record<string, boolean | number | string>;
|
|
10
|
+
fire?: string[];
|
|
11
|
+
/** Pointer in artboard pixels (y-down, top-left origin). */
|
|
12
|
+
pointer?: {
|
|
13
|
+
type: "down" | "up" | "move";
|
|
14
|
+
x: number;
|
|
15
|
+
y: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface PoseRequest {
|
|
19
|
+
/** Skin to show (name or id); defaults to the default skin only. */
|
|
20
|
+
skin?: string;
|
|
21
|
+
animation?: string;
|
|
22
|
+
time?: number;
|
|
23
|
+
stateMachine?: string;
|
|
24
|
+
steps?: Step[];
|
|
25
|
+
}
|
|
26
|
+
export interface RenderRequest extends PoseRequest {
|
|
27
|
+
scale?: number;
|
|
28
|
+
showBones?: boolean;
|
|
29
|
+
showBoundingBoxes?: boolean;
|
|
30
|
+
wireframe?: boolean;
|
|
31
|
+
background?: string | null;
|
|
32
|
+
}
|
|
33
|
+
export declare function loadImages(session: Pick<ProjectSession, "readImageSrc">, doc: OpenBoneDocument): Promise<Map<string, Image>>;
|
|
34
|
+
export declare function useSkin(inst: OpenBoneInstance, skin?: string): void;
|
|
35
|
+
/** Build a posed instance for the request. Returns logs from state machine simulation. */
|
|
36
|
+
export declare function pose(doc: OpenBoneDocument, req: PoseRequest, scriptHost?: ScriptHost | null): {
|
|
37
|
+
inst: OpenBoneInstance;
|
|
38
|
+
log: string[];
|
|
39
|
+
};
|
|
40
|
+
export declare function renderPng(session: ProjectSession, req: RenderRequest, scriptHost?: ScriptHost | null): Promise<{
|
|
41
|
+
png: Buffer<ArrayBufferLike>;
|
|
42
|
+
log: string[];
|
|
43
|
+
}>;
|
|
44
|
+
/**
|
|
45
|
+
* Largest GIF returned inline by render_gif. A tool result travels as one JSON-RPC message (the image as base64, a
|
|
46
|
+
* third bigger), and MCP clients drop the connection on very large messages: a 14 MB GIF (19 MB of base64) did. Bigger
|
|
47
|
+
* GIFs are still saved to disk; the result then carries a contact sheet of a few frames instead.
|
|
48
|
+
*/
|
|
49
|
+
export declare const MAX_INLINE_GIF: number;
|
|
50
|
+
/** A grid of evenly spaced frames, for a preview of a long render. */
|
|
51
|
+
export declare function contactSheet(frameW: number, frameH: number, count: number, cols?: number, rows?: number): {
|
|
52
|
+
add(canvas: Canvas, index: number): void;
|
|
53
|
+
png: () => Buffer<ArrayBufferLike>;
|
|
54
|
+
picks: number[];
|
|
55
|
+
};
|
|
56
|
+
export declare function renderGif(session: ProjectSession, req: {
|
|
57
|
+
animation?: string;
|
|
58
|
+
stateMachine?: string;
|
|
59
|
+
skin?: string;
|
|
60
|
+
duration: number;
|
|
61
|
+
fps: number;
|
|
62
|
+
scale?: number;
|
|
63
|
+
showBones?: boolean;
|
|
64
|
+
steps?: Step[];
|
|
65
|
+
}, scriptHost?: ScriptHost | null,
|
|
66
|
+
/** Called with each rendered frame (e.g. to build a preview); the canvas is reused, so copy what you need. */
|
|
67
|
+
onFrame?: (canvas: Canvas, index: number, count: number) => void): Promise<Buffer<ArrayBuffer>>;
|
|
68
|
+
/**
|
|
69
|
+
* Lay out every image of `doc` on atlas pages and compose the page PNGs (as data: URIs), with the same code the
|
|
70
|
+
* editor uses (`buildAtlas`). Images that already live only in an atlas are cut back out of their page. The result
|
|
71
|
+
* is ready for the `setAtlas` op.
|
|
72
|
+
*/
|
|
73
|
+
export declare function packAtlas(session: Pick<ProjectSession, "readImageSrc">, doc: OpenBoneDocument, opts?: BuildAtlasOptions): Promise<AtlasData>;
|
|
74
|
+
/** `packAtlas` with the scale used and any warnings (e.g. JPEG pages losing transparency). */
|
|
75
|
+
export declare function packAtlasFull(session: Pick<ProjectSession, "readImageSrc">, doc: OpenBoneDocument, opts?: BuildAtlasOptions): Promise<BuiltAtlas>;
|
|
76
|
+
/** One atlas per scale variant (multi-scale packs), images loaded once. */
|
|
77
|
+
export declare function packAtlasScales(session: Pick<ProjectSession, "readImageSrc">, doc: OpenBoneDocument, opts: BuildAtlasOptions, variants: AtlasScaleVariant[]): Promise<(BuiltAtlas & {
|
|
78
|
+
suffix: string;
|
|
79
|
+
})[]>;
|
|
80
|
+
/** Canvases, image loading and PNG / JPEG encoding for the atlas packer, in Node (@napi-rs/canvas). */
|
|
81
|
+
export declare function atlasEnv(session: Pick<ProjectSession, "readImageSrc">): AtlasEnv;
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { createCanvas } from "@napi-rs/canvas";
|
|
2
|
+
import { decodeImage } from "./image-load.js";
|
|
3
|
+
import { textureAssets } from "@openbone/core";
|
|
4
|
+
import { encodePng } from "@openbone/export";
|
|
5
|
+
import { CanvasRenderer, buildAtlas, buildAtlasScales } from "@openbone/render-canvas";
|
|
6
|
+
import { OpenBoneInstance } from "@openbone/runtime";
|
|
7
|
+
// @ts-expect-error gifenc ships without types (CommonJS)
|
|
8
|
+
import * as gifenc from "gifenc";
|
|
9
|
+
// Node loads the CommonJS build (the functions sit on `default`); bundlers such as Vite and Vitest load the ESM build
|
|
10
|
+
// (the functions are named exports, and `default` is the encoder alone). Take them from whichever has them.
|
|
11
|
+
const gifNs = gifenc;
|
|
12
|
+
const { GIFEncoder, applyPalette, quantize } = (typeof gifNs.GIFEncoder === "function" ? gifNs : gifNs.default);
|
|
13
|
+
export async function loadImages(session, doc) {
|
|
14
|
+
const map = new Map();
|
|
15
|
+
// Atlas pages and loose images, keyed by id (the renderers ask for page ids when images are packed).
|
|
16
|
+
for (const img of textureAssets(doc)) {
|
|
17
|
+
try {
|
|
18
|
+
map.set(img.id, await decodeImage(await session.readImageSrc(img.src)));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
/* missing images render as placeholders */
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return map;
|
|
25
|
+
}
|
|
26
|
+
export function useSkin(inst, skin) {
|
|
27
|
+
if (!skin)
|
|
28
|
+
return;
|
|
29
|
+
inst.skeleton.setSkin(skin);
|
|
30
|
+
if (!inst.skeleton.skin)
|
|
31
|
+
throw new Error(`Skin "${skin}" not found. Available: ${inst.doc.skeleton.skins.map((k) => k.name).join(", ")}`);
|
|
32
|
+
}
|
|
33
|
+
/** Build a posed instance for the request. Returns logs from state machine simulation. */
|
|
34
|
+
export function pose(doc, req, scriptHost) {
|
|
35
|
+
const inst = new OpenBoneInstance(doc, { scriptHost });
|
|
36
|
+
useSkin(inst, req.skin);
|
|
37
|
+
const log = [];
|
|
38
|
+
if (req.stateMachine !== undefined || req.steps?.length) {
|
|
39
|
+
const sm = inst.useStateMachine(req.stateMachine || undefined);
|
|
40
|
+
sm.on("stateChange", (e) => log.push(`t=${t.toFixed(3)} [${e.layer}] ${e.from ?? "∅"} → ${e.to}`));
|
|
41
|
+
sm.on("event", (e) => log.push(`t=${t.toFixed(3)} event ${e.name}${e.animation ? ` (from ${e.animation})` : ""} int=${e.int} float=${e.float}${e.string ? ` "${e.string}"` : ""}`));
|
|
42
|
+
sm.on("inputChange", (e) => log.push(`t=${t.toFixed(3)} input ${e.name} = ${e.value}`));
|
|
43
|
+
sm.on("log", (a) => log.push(`t=${t.toFixed(3)} script: ${a.map((x) => (typeof x === "string" ? x : JSON.stringify(x))).join(" ")}`));
|
|
44
|
+
sm.on("error", (e) => log.push(`t=${t.toFixed(3)} ERROR ${e.message}`));
|
|
45
|
+
const soundName = (id) => doc.audio?.sounds.find((x) => x.id === id)?.name ?? id;
|
|
46
|
+
sm.on("audio", (c) => log.push(`t=${t.toFixed(3)} audio ${c.type === "play" ? `play ${soundName(c.sound)} [${c.id}]${c.loop ? " loop" : ""} vol ${c.volume} (${c.source})` : c.type === "stop" ? `stop [${c.id}]${c.fade ? ` fade ${c.fade}s` : ""}` : c.type === "fade" ? `fade [${c.id}] → ${c.volume}` : `${c.muted ? "mute" : "unmute"} ${c.bus === null ? "master" : (doc.audio?.buses.find((b) => b.id === c.bus)?.name ?? c.bus)}`}`));
|
|
47
|
+
let t = 0;
|
|
48
|
+
sm.advance(0);
|
|
49
|
+
for (const s of req.steps ?? [{ advance: req.time ?? 0 }]) {
|
|
50
|
+
for (const [k, v] of Object.entries(s.setInputs ?? {}))
|
|
51
|
+
sm.setInput(k, v);
|
|
52
|
+
for (const f of s.fire ?? [])
|
|
53
|
+
sm.fire(f);
|
|
54
|
+
if (s.pointer) {
|
|
55
|
+
const [x, y] = inst.artboardToSkeleton(s.pointer.x, s.pointer.y);
|
|
56
|
+
sm.pointer(s.pointer.type, x, y);
|
|
57
|
+
}
|
|
58
|
+
const dt = s.advance ?? 1 / 30;
|
|
59
|
+
// Advance in small steps so transitions/events resolve like real playback.
|
|
60
|
+
let rem = dt;
|
|
61
|
+
sm.advance(0);
|
|
62
|
+
while (rem > 1e-9) {
|
|
63
|
+
const h = Math.min(rem, 1 / 60);
|
|
64
|
+
sm.advance(h);
|
|
65
|
+
t += h;
|
|
66
|
+
rem -= h;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
log.push(`final states: ${JSON.stringify(sm.activeStates)}; inputs: ${JSON.stringify(Object.fromEntries([...sm.inputs].map(([id, v]) => [sm.data.inputs.find((i) => i.id === id)?.name ?? id, v])))}`);
|
|
70
|
+
}
|
|
71
|
+
else if (req.animation) {
|
|
72
|
+
inst.animationState.setAnimation(0, req.animation, true, 0);
|
|
73
|
+
// Step at 60 Hz so physics constraints are simulated up to the requested time.
|
|
74
|
+
inst.advance(0);
|
|
75
|
+
let rem = req.time ?? 0;
|
|
76
|
+
while (rem > 1e-9) {
|
|
77
|
+
const h = Math.min(rem, 1 / 60);
|
|
78
|
+
inst.advance(h);
|
|
79
|
+
rem -= h;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
inst.skeleton.updateWorldTransform();
|
|
84
|
+
}
|
|
85
|
+
return { inst, log };
|
|
86
|
+
}
|
|
87
|
+
export async function renderPng(session, req, scriptHost) {
|
|
88
|
+
const doc = session.doc;
|
|
89
|
+
const images = await loadImages(session, doc);
|
|
90
|
+
const { inst, log } = pose(doc, req, scriptHost);
|
|
91
|
+
const scale = req.scale ?? 1;
|
|
92
|
+
const canvas = createCanvas(Math.max(1, Math.round(doc.width * scale)), Math.max(1, Math.round(doc.height * scale)));
|
|
93
|
+
const ctx = canvas.getContext("2d");
|
|
94
|
+
const r = new CanvasRenderer(ctx, (id) => images.get(id));
|
|
95
|
+
r.render(inst.skeleton, {
|
|
96
|
+
view: { x: 0, y: 0, zoom: scale },
|
|
97
|
+
bones: req.showBones ?? true,
|
|
98
|
+
boundingBoxes: req.showBoundingBoxes ?? true,
|
|
99
|
+
wireframe: req.wireframe ?? false,
|
|
100
|
+
background: req.background === undefined ? (doc.background ?? "#f4f4f6") : req.background,
|
|
101
|
+
});
|
|
102
|
+
return { png: canvas.toBuffer("image/png"), log };
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Largest GIF returned inline by render_gif. A tool result travels as one JSON-RPC message (the image as base64, a
|
|
106
|
+
* third bigger), and MCP clients drop the connection on very large messages: a 14 MB GIF (19 MB of base64) did. Bigger
|
|
107
|
+
* GIFs are still saved to disk; the result then carries a contact sheet of a few frames instead.
|
|
108
|
+
*/
|
|
109
|
+
export const MAX_INLINE_GIF = 4 * 1024 * 1024;
|
|
110
|
+
/** A grid of evenly spaced frames, for a preview of a long render. */
|
|
111
|
+
export function contactSheet(frameW, frameH, count, cols = 3, rows = 2) {
|
|
112
|
+
const n = cols * rows;
|
|
113
|
+
const tw = Math.min(frameW, 480);
|
|
114
|
+
const th = Math.round((frameH * tw) / frameW);
|
|
115
|
+
const sheet = createCanvas(cols * tw, rows * th);
|
|
116
|
+
const sctx = sheet.getContext("2d");
|
|
117
|
+
const picks = Array.from({ length: n }, (_, i) => Math.round((i * (count - 1)) / Math.max(1, n - 1)));
|
|
118
|
+
return {
|
|
119
|
+
add(canvas, index) {
|
|
120
|
+
const k = picks.indexOf(index);
|
|
121
|
+
if (k >= 0)
|
|
122
|
+
sctx.drawImage(canvas, (k % cols) * tw, Math.floor(k / cols) * th, tw, th);
|
|
123
|
+
},
|
|
124
|
+
png: () => sheet.toBuffer("image/png"),
|
|
125
|
+
picks,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export async function renderGif(session, req, scriptHost,
|
|
129
|
+
/** Called with each rendered frame (e.g. to build a preview); the canvas is reused, so copy what you need. */
|
|
130
|
+
onFrame) {
|
|
131
|
+
const doc = session.doc;
|
|
132
|
+
const images = await loadImages(session, doc);
|
|
133
|
+
const scale = req.scale ?? 0.5;
|
|
134
|
+
const w = Math.max(1, Math.round(doc.width * scale));
|
|
135
|
+
const h = Math.max(1, Math.round(doc.height * scale));
|
|
136
|
+
const canvas = createCanvas(w, h);
|
|
137
|
+
const ctx = canvas.getContext("2d");
|
|
138
|
+
const renderer = new CanvasRenderer(ctx, (id) => images.get(id));
|
|
139
|
+
const inst = new OpenBoneInstance(doc, { scriptHost });
|
|
140
|
+
useSkin(inst, req.skin);
|
|
141
|
+
const sm = req.stateMachine !== undefined || req.steps ? inst.useStateMachine(req.stateMachine || undefined) : null;
|
|
142
|
+
if (!sm) {
|
|
143
|
+
if (!req.animation)
|
|
144
|
+
throw new Error("Give animation or stateMachine");
|
|
145
|
+
inst.animationState.setAnimation(0, req.animation, true, 0);
|
|
146
|
+
}
|
|
147
|
+
const gif = GIFEncoder();
|
|
148
|
+
const frames = Math.max(1, Math.round(req.duration * req.fps));
|
|
149
|
+
const dt = 1 / req.fps;
|
|
150
|
+
// Steps (state machine) are scheduled by cumulative time.
|
|
151
|
+
const schedule = [];
|
|
152
|
+
let acc = 0;
|
|
153
|
+
for (const s of req.steps ?? []) {
|
|
154
|
+
schedule.push({ at: acc, step: s });
|
|
155
|
+
acc += s.advance ?? 0;
|
|
156
|
+
}
|
|
157
|
+
let t = 0;
|
|
158
|
+
for (let f = 0; f < frames; f++) {
|
|
159
|
+
if (sm)
|
|
160
|
+
while (schedule.length && schedule[0].at <= t + 1e-9) {
|
|
161
|
+
const { step } = schedule.shift();
|
|
162
|
+
for (const [k, v] of Object.entries(step.setInputs ?? {}))
|
|
163
|
+
sm.setInput(k, v);
|
|
164
|
+
for (const n of step.fire ?? [])
|
|
165
|
+
sm.fire(n);
|
|
166
|
+
if (step.pointer) {
|
|
167
|
+
const [x, y] = inst.artboardToSkeleton(step.pointer.x, step.pointer.y);
|
|
168
|
+
sm.pointer(step.pointer.type, x, y);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
inst.advance(f === 0 ? 0 : dt);
|
|
172
|
+
t += f === 0 ? 0 : dt;
|
|
173
|
+
renderer.render(inst.skeleton, {
|
|
174
|
+
view: { x: 0, y: 0, zoom: scale },
|
|
175
|
+
bones: req.showBones ?? false,
|
|
176
|
+
background: doc.background ?? "#ffffff",
|
|
177
|
+
});
|
|
178
|
+
onFrame?.(canvas, f, frames);
|
|
179
|
+
const { data } = ctx.getImageData(0, 0, w, h);
|
|
180
|
+
const palette = quantize(data, 256);
|
|
181
|
+
gif.writeFrame(applyPalette(data, palette), w, h, { palette, delay: Math.round(1000 / req.fps) });
|
|
182
|
+
}
|
|
183
|
+
gif.finish();
|
|
184
|
+
return Buffer.from(gif.bytes());
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Lay out every image of `doc` on atlas pages and compose the page PNGs (as data: URIs), with the same code the
|
|
188
|
+
* editor uses (`buildAtlas`). Images that already live only in an atlas are cut back out of their page. The result
|
|
189
|
+
* is ready for the `setAtlas` op.
|
|
190
|
+
*/
|
|
191
|
+
export async function packAtlas(session, doc, opts = {}) {
|
|
192
|
+
return (await buildAtlas(doc, opts, atlasEnv(session))).atlas;
|
|
193
|
+
}
|
|
194
|
+
/** `packAtlas` with the scale used and any warnings (e.g. JPEG pages losing transparency). */
|
|
195
|
+
export async function packAtlasFull(session, doc, opts = {}) {
|
|
196
|
+
return buildAtlas(doc, opts, atlasEnv(session));
|
|
197
|
+
}
|
|
198
|
+
/** One atlas per scale variant (multi-scale packs), images loaded once. */
|
|
199
|
+
export async function packAtlasScales(session, doc, opts, variants) {
|
|
200
|
+
return buildAtlasScales(doc, opts, variants, atlasEnv(session));
|
|
201
|
+
}
|
|
202
|
+
/** Canvases, image loading and PNG / JPEG encoding for the atlas packer, in Node (@napi-rs/canvas). */
|
|
203
|
+
export function atlasEnv(session) {
|
|
204
|
+
return {
|
|
205
|
+
createCanvas: (w, h) => createCanvas(w, h),
|
|
206
|
+
load: async (src, name) => {
|
|
207
|
+
try {
|
|
208
|
+
return (await decodeImage(await session.readImageSrc(src)));
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
throw new Error(`Could not load image "${name}" (${src.slice(0, 60)})`);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
encodePng: (data, width, height) => encodePng({ width, height, data }),
|
|
215
|
+
encodeJpeg: async (data, width, height, quality) => {
|
|
216
|
+
const c = createCanvas(width, height);
|
|
217
|
+
const g = c.getContext("2d");
|
|
218
|
+
const id = g.createImageData(width, height);
|
|
219
|
+
id.data.set(data);
|
|
220
|
+
g.putImageData(id, 0, 0);
|
|
221
|
+
return new Uint8Array(c.toBuffer("image/jpeg", quality));
|
|
222
|
+
},
|
|
223
|
+
};
|
|
224
|
+
}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { ScriptHost } from "@openbone/runtime";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import { ProjectSession } from "./session.js";
|
|
5
|
+
export declare const snake: (s: string) => string;
|
|
6
|
+
export declare const stepSchema: z.ZodObject<{
|
|
7
|
+
advance: z.ZodOptional<z.ZodNumber>;
|
|
8
|
+
setInputs: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodBoolean, z.ZodNumber, z.ZodString]>>>;
|
|
9
|
+
fire: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
10
|
+
pointer: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
type: z.ZodEnum<{
|
|
12
|
+
down: "down";
|
|
13
|
+
up: "up";
|
|
14
|
+
move: "move";
|
|
15
|
+
}>;
|
|
16
|
+
x: z.ZodNumber;
|
|
17
|
+
y: z.ZodNumber;
|
|
18
|
+
}, z.core.$strip>>;
|
|
19
|
+
}, z.core.$strip>;
|
|
20
|
+
export interface ServerOptions {
|
|
21
|
+
session?: ProjectSession;
|
|
22
|
+
scriptHost?: ScriptHost;
|
|
23
|
+
}
|
|
24
|
+
export declare function createServer(opts?: ServerOptions): {
|
|
25
|
+
server: McpServer;
|
|
26
|
+
session: ProjectSession;
|
|
27
|
+
};
|
|
28
|
+
/** Seconds of PCM audio in a WAV file (0 for other formats or unreadable headers). */
|