rig-parts 2.74.0 → 2.76.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.ts +43 -13
- package/docs/AUTHORING.md +151 -16
- package/docs/STABILITY.md +92 -6
- package/package.json +2 -2
- package/src/check.ts +61 -35
- package/src/compose-seams.ts +476 -0
- package/src/layers.ts +73 -7
- package/src/scene.ts +191 -68
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import corePackage from '@rig-c/core/package.json' with { type: 'json' };
|
|
5
|
+
import { decodePng, encodePng } from '@rig-c/core/plate';
|
|
6
|
+
import { assertPng } from '@rig-c/core/png';
|
|
7
|
+
import { type Frame, loadCandidate, type Plate, type Poser, renderFrame, sampleAnimation, throughPoser, type Viewport } from '@rig-c/core/render-shared';
|
|
8
|
+
import { SeamError, seamsOf, type SeamFrame, type SeamHit, type SeamPair } from '@rig-c/core/seam';
|
|
9
|
+
import { type CheckReport, MOTION_SEAM_LINE, motionSeamClass, type MotionSeamClass, type MotionSeamPair, openingOverFloor } from './check.ts';
|
|
10
|
+
import { PartsError } from './errors.ts';
|
|
11
|
+
import { rigJsonText } from './rig.ts';
|
|
12
|
+
|
|
13
|
+
export const COMPOSE_SEAMS_DIR = 'motion-seams';
|
|
14
|
+
export const COMPOSE_SEAMS_REQUEST = 'rig-parts-motion-seams-request/1';
|
|
15
|
+
export const COMPOSE_SEAMS_REPORT = 'rig-parts-motion-seams/1';
|
|
16
|
+
const INVENTORY_SPEC = 'rig-parts-motion-seams-frames/1';
|
|
17
|
+
export const COMPOSE_SEAMS_CORE = corePackage.version;
|
|
18
|
+
const COMPOSE_SEAMS_NOT_JUDGED = 'COMPOSE_MOTION_SEAMS_NOT_JUDGED';
|
|
19
|
+
export const COMPOSE_SEAMS_OFF = `${COMPOSE_SEAMS_NOT_JUDGED} — bound render frames were not judged; use --motion-seams <request.json>`;
|
|
20
|
+
|
|
21
|
+
export interface ComposeSeamRequest {
|
|
22
|
+
spec: typeof COMPOSE_SEAMS_REQUEST;
|
|
23
|
+
animation: string;
|
|
24
|
+
fps: number;
|
|
25
|
+
preroll?: number;
|
|
26
|
+
viewport?: [number, number, number, number];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
interface FrameRow {
|
|
30
|
+
index: number;
|
|
31
|
+
time: number;
|
|
32
|
+
viewport: Viewport;
|
|
33
|
+
path: string;
|
|
34
|
+
sha256: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
interface FrameInventory {
|
|
38
|
+
spec: typeof INVENTORY_SPEC;
|
|
39
|
+
animation: string;
|
|
40
|
+
fps: number;
|
|
41
|
+
duration: number;
|
|
42
|
+
frame_count: number;
|
|
43
|
+
frames: FrameRow[];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
type MeasuredHit = Pick<SeamHit, 'px' | 'cell' | 'box'>;
|
|
47
|
+
type FrameFinding = 'NONE' | 'OPENING_WARNING' | 'BODY_ROOT_OPENING' | 'REQUIRED_BACKGROUND';
|
|
48
|
+
const SKIP_REASONS = ['SLOT_MISSING', 'SLOT_NOT_DRAWN', 'NO_SETUP_BAND', 'CORE_SEAM_UNREADABLE'] as const;
|
|
49
|
+
type FrameSkipReason = (typeof SKIP_REASONS)[number];
|
|
50
|
+
type FrameReading = { index: number; time: number } & (
|
|
51
|
+
| { status: 'SKIP'; reason: FrameSkipReason; detail?: string }
|
|
52
|
+
| { status: 'MEASURED'; gap: MeasuredHit | null; ghost: MeasuredHit | null;
|
|
53
|
+
gap_open: MeasuredHit | null; gap_covered: MeasuredHit | null;
|
|
54
|
+
root?: MeasuredHit & { under: 'a' | 'b'; under_ghost_px: number };
|
|
55
|
+
finding: FrameFinding }
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
interface PairReading extends SeamPair {
|
|
59
|
+
class: MotionSeamClass;
|
|
60
|
+
declared: string[];
|
|
61
|
+
frames: FrameReading[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface BundleVerdict {
|
|
65
|
+
status: 'PASS' | 'FAIL' | 'SKIP';
|
|
66
|
+
reason: 'COMPLETE' | 'OPENING' | 'INCOMPLETE' | 'NO_DECLARED_PAIRS';
|
|
67
|
+
measured: number;
|
|
68
|
+
skipped: number;
|
|
69
|
+
warning_frames: number;
|
|
70
|
+
failing_frames: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type ComposeSeamRefusalCode =
|
|
74
|
+
| 'COMPOSE_MOTION_SEAMS_ANIMATION'
|
|
75
|
+
| 'COMPOSE_MOTION_SEAMS_BINDING'
|
|
76
|
+
| 'COMPOSE_MOTION_SEAMS_BUILD'
|
|
77
|
+
| 'COMPOSE_MOTION_SEAMS_FPS'
|
|
78
|
+
| 'COMPOSE_MOTION_SEAMS_INVENTORY'
|
|
79
|
+
| 'COMPOSE_MOTION_SEAMS_REQUEST'
|
|
80
|
+
| 'COMPOSE_MOTION_SEAMS_SAMPLE';
|
|
81
|
+
|
|
82
|
+
function refusal(code: ComposeSeamRefusalCode, object: string, detail: string): never {
|
|
83
|
+
throw new PartsError([{ code, object, detail }]);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function objectOf(value: unknown): Record<string, unknown> {
|
|
87
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function bytesAt(path: string, code: ComposeSeamRefusalCode): Buffer {
|
|
91
|
+
try {
|
|
92
|
+
return readFileSync(path);
|
|
93
|
+
} catch (err) {
|
|
94
|
+
return refusal(code, path, `requires readable bytes: ${(err as Error).message}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function jsonOf(bytes: Uint8Array, path: string, code: ComposeSeamRefusalCode): Record<string, unknown> {
|
|
99
|
+
try {
|
|
100
|
+
return objectOf(JSON.parse(Buffer.from(bytes).toString('utf8')));
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return refusal(code, path, `requires JSON: ${(err as Error).message}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function hash(bytes: string | Uint8Array): string {
|
|
107
|
+
return createHash('sha256').update(bytes).digest('hex');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function jsonDigest(value: unknown): string {
|
|
111
|
+
return hash(rigJsonText(value));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function clearComposeSeams(out: string): void {
|
|
115
|
+
rmSync(join(out, COMPOSE_SEAMS_DIR), { recursive: true, force: true });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function readComposeSeamRequest(path: string): ComposeSeamRequest {
|
|
119
|
+
const raw = jsonOf(bytesAt(path, 'COMPOSE_MOTION_SEAMS_REQUEST'), path, 'COMPOSE_MOTION_SEAMS_REQUEST');
|
|
120
|
+
const { spec, animation, fps, preroll, viewport } = raw;
|
|
121
|
+
const extra = Object.keys(raw).filter(key => !['spec', 'animation', 'fps', 'preroll', 'viewport'].includes(key));
|
|
122
|
+
if (extra.length > 0) refusal('COMPOSE_MOTION_SEAMS_REQUEST', path, `has unknown fields ${extra.join(', ')}`);
|
|
123
|
+
if (spec !== COMPOSE_SEAMS_REQUEST || typeof animation !== 'string' || animation.trim() === '') {
|
|
124
|
+
refusal('COMPOSE_MOTION_SEAMS_REQUEST', path, `requires spec "${COMPOSE_SEAMS_REQUEST}" and a literal composed animation name`);
|
|
125
|
+
}
|
|
126
|
+
if (typeof fps !== 'number' || !Number.isFinite(fps) || fps < 1) {
|
|
127
|
+
refusal('COMPOSE_MOTION_SEAMS_FPS', path, `fps is ${JSON.stringify(fps)}; a finite number at least 1 is required`);
|
|
128
|
+
}
|
|
129
|
+
if (preroll !== undefined && (typeof preroll !== 'number' || !Number.isSafeInteger(preroll) || preroll < 1)) {
|
|
130
|
+
refusal('COMPOSE_MOTION_SEAMS_REQUEST', path, `preroll is ${JSON.stringify(preroll)}; omit it or declare a positive whole number of loops`);
|
|
131
|
+
}
|
|
132
|
+
let view: ComposeSeamRequest['viewport'];
|
|
133
|
+
if (viewport !== undefined) {
|
|
134
|
+
if (!Array.isArray(viewport) || viewport.length !== 4 || !viewport.every(v => typeof v === 'number' && Number.isFinite(v)) ||
|
|
135
|
+
!Number.isSafeInteger(viewport[2]) || !Number.isSafeInteger(viewport[3]) || viewport[2] < 1 || viewport[3] < 1) {
|
|
136
|
+
refusal('COMPOSE_MOTION_SEAMS_REQUEST', path, 'viewport requires [x, y, width, height] in world units with positive whole pixel dimensions');
|
|
137
|
+
}
|
|
138
|
+
view = [viewport[0], viewport[1], viewport[2], viewport[3]];
|
|
139
|
+
}
|
|
140
|
+
return { spec, animation, fps, ...(preroll === undefined ? {} : { preroll }), ...(view === undefined ? {} : { viewport: view }) };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function openBuild(out: string) {
|
|
144
|
+
const build = join(out, 'check', 'build');
|
|
145
|
+
const model = join(build, 'skeleton.model.json');
|
|
146
|
+
const skeleton = join(build, 'skeleton.json');
|
|
147
|
+
const atlas = join(build, 'skeleton.atlas');
|
|
148
|
+
const checkPath = join(out, 'check', 'check.json');
|
|
149
|
+
const checkBytes = bytesAt(checkPath, 'COMPOSE_MOTION_SEAMS_BUILD');
|
|
150
|
+
const check = jsonOf(checkBytes, checkPath, 'COMPOSE_MOTION_SEAMS_BUILD');
|
|
151
|
+
const model_sha256 = hash(bytesAt(model, 'COMPOSE_MOTION_SEAMS_BUILD'));
|
|
152
|
+
if (check.gate_spine_html_green !== true || objectOf(check.build).model_sha256 !== model_sha256) {
|
|
153
|
+
refusal('COMPOSE_MOTION_SEAMS_BUILD', model, `SHA-256 ${model_sha256} must match the model bound to a green gate in ${checkPath}`);
|
|
154
|
+
}
|
|
155
|
+
const skeletonBytes = bytesAt(skeleton, 'COMPOSE_MOTION_SEAMS_BUILD');
|
|
156
|
+
const atlasBytes = bytesAt(atlas, 'COMPOSE_MOTION_SEAMS_BUILD');
|
|
157
|
+
const { choice, pageNames } = (() => {
|
|
158
|
+
try {
|
|
159
|
+
const candidate = loadCandidate({
|
|
160
|
+
skeletonText: skeletonBytes.toString('utf8'), atlasText: atlasBytes.toString('utf8'), atlasDir: build, label: 'compose motion seams',
|
|
161
|
+
}, { skeleton, atlas }, 'core');
|
|
162
|
+
return { choice: candidate.choice, pageNames: [...candidate.pages.keys()].sort() };
|
|
163
|
+
} catch (err) {
|
|
164
|
+
return refusal('COMPOSE_MOTION_SEAMS_BUILD', build, `requires one Core-readable packed candidate: ${(err as Error).message}`);
|
|
165
|
+
}
|
|
166
|
+
})();
|
|
167
|
+
if (choice.core === null) refusal('COMPOSE_MOTION_SEAMS_BUILD', build, choice.why);
|
|
168
|
+
// Core's candidate loader exposes decoded pages only. Use the exact
|
|
169
|
+
// buffers hashed below as the source of the render and seam textures.
|
|
170
|
+
const pageFiles = pageNames.map(path => ({ path, bytes: bytesAt(join(build, path), 'COMPOSE_MOTION_SEAMS_BUILD') }));
|
|
171
|
+
const sources = {
|
|
172
|
+
model_sha256,
|
|
173
|
+
skeleton_sha256: hash(skeletonBytes),
|
|
174
|
+
atlas_sha256: hash(atlasBytes),
|
|
175
|
+
pages: pageFiles.map(({ path, bytes }) => ({ path, sha256: hash(bytes) })),
|
|
176
|
+
check_sha256: hash(checkBytes),
|
|
177
|
+
scene_sha256: hash(bytesAt(join(out, 'scene.json'), 'COMPOSE_MOTION_SEAMS_BUILD')),
|
|
178
|
+
};
|
|
179
|
+
return { choice, pageFiles, sources, declaration: objectOf(check[MOTION_SEAM_LINE]) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function sampleCount(duration: number, request: ComposeSeamRequest): number {
|
|
183
|
+
const steps = duration * request.fps;
|
|
184
|
+
const count = Math.round(steps) + 1;
|
|
185
|
+
const warmSteps = steps * (request.preroll ?? 0);
|
|
186
|
+
if (!Number.isFinite(duration) || duration < 0 || count + Math.ceil(warmSteps) >= 0xffffffff ||
|
|
187
|
+
(request.preroll !== undefined && count < 2)) {
|
|
188
|
+
refusal('COMPOSE_MOTION_SEAMS_FPS', request.animation, `${duration}s at ${request.fps} fps with preroll ${request.preroll ?? 0} cannot fit a Core sample schedule`);
|
|
189
|
+
}
|
|
190
|
+
return count;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function framePath(index: number): string {
|
|
194
|
+
return `frames/f${String(index).padStart(4, '0')}.png`;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function validateInventory(dir: string, inventory: Record<string, unknown>, request: ComposeSeamRequest, duration: number): void {
|
|
198
|
+
if (inventory.fps !== request.fps) refusal('COMPOSE_MOTION_SEAMS_FPS', 'inventory.json', `has ${String(inventory.fps)} fps; consumer requires ${request.fps} fps`);
|
|
199
|
+
const count = sampleCount(duration, request);
|
|
200
|
+
const frames: unknown[] = Array.isArray(inventory.frames) ? inventory.frames : [];
|
|
201
|
+
if (inventory.spec !== INVENTORY_SPEC || inventory.animation !== request.animation || inventory.duration !== duration ||
|
|
202
|
+
inventory.frame_count !== count || frames.length !== count) {
|
|
203
|
+
refusal('COMPOSE_MOTION_SEAMS_INVENTORY', 'inventory.json', `requires "${request.animation}" with ${count} frames at ${request.fps} fps (packed duration ${duration}s); found ${frames.length} rows`);
|
|
204
|
+
}
|
|
205
|
+
let files: string[];
|
|
206
|
+
try {
|
|
207
|
+
files = readdirSync(join(dir, 'frames')).sort();
|
|
208
|
+
} catch (err) {
|
|
209
|
+
return refusal('COMPOSE_MOTION_SEAMS_INVENTORY', 'frames/', `requires the frame directory: ${(err as Error).message}`);
|
|
210
|
+
}
|
|
211
|
+
const expected = frames.map((_, i) => framePath(i).slice('frames/'.length)).sort();
|
|
212
|
+
if (files.length !== expected.length || files.some((name, i) => name !== expected[i])) {
|
|
213
|
+
refusal('COMPOSE_MOTION_SEAMS_INVENTORY', 'frames/', `found ${files.length} entries; the exact ${count} named frame PNGs are required`);
|
|
214
|
+
}
|
|
215
|
+
frames.forEach((value, index) => {
|
|
216
|
+
const row = objectOf(value);
|
|
217
|
+
const path = framePath(index);
|
|
218
|
+
const time = index * (1 / request.fps);
|
|
219
|
+
if (row.path !== path || row.index !== index || row.time !== time) {
|
|
220
|
+
refusal('COMPOSE_MOTION_SEAMS_INVENTORY', path, `requires frame ${index} at ${time}s; found ${String(row.path)} frame ${String(row.index)} at ${String(row.time)}s`);
|
|
221
|
+
}
|
|
222
|
+
const actual = hash(bytesAt(join(dir, path), 'COMPOSE_MOTION_SEAMS_INVENTORY'));
|
|
223
|
+
if (row.sha256 !== actual) refusal('COMPOSE_MOTION_SEAMS_INVENTORY', path, `bytes have SHA-256 ${actual}; inventory requires ${String(row.sha256)}`);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function hitOf(hit: SeamHit | undefined): MeasuredHit | null {
|
|
228
|
+
return hit === undefined ? null : { px: hit.px, cell: hit.cell, box: hit.box };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function frameFinding(
|
|
232
|
+
kind: MotionSeamClass, required: boolean, gap: MeasuredHit | null, ghost: MeasuredHit | null,
|
|
233
|
+
gapOpen: MeasuredHit | null, root: MeasuredHit | undefined,
|
|
234
|
+
): FrameFinding {
|
|
235
|
+
if (required && gapOpen !== null) return 'REQUIRED_BACKGROUND';
|
|
236
|
+
if (kind === 'body-root') return root !== undefined && openingOverFloor(root) ? 'BODY_ROOT_OPENING' : 'NONE';
|
|
237
|
+
return [gap, ghost].some(hit => hit !== null && openingOverFloor(hit)) ? 'OPENING_WARNING' : 'NONE';
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function readFrame(pair: MotionSeamPair, kind: MotionSeamClass, read: ReturnType<typeof seamsOf>[number], frame: SeamFrame, required: boolean): FrameReading {
|
|
241
|
+
const id = { index: frame.index, time: frame.time };
|
|
242
|
+
if (read.banded === 0) return { ...id, status: 'SKIP', reason: read.drawn === 0 ? 'SLOT_NOT_DRAWN' : 'NO_SETUP_BAND' };
|
|
243
|
+
const gap = hitOf(read.gap.worst ?? undefined);
|
|
244
|
+
const ghost = hitOf(read.ghost.worst ?? undefined);
|
|
245
|
+
const gap_open = hitOf(read.gapOpen.worst ?? undefined);
|
|
246
|
+
const gap_covered = hitOf(read.gapCovered.worst ?? undefined);
|
|
247
|
+
let root: (MeasuredHit & { under: 'a' | 'b'; under_ghost_px: number }) | undefined;
|
|
248
|
+
if (pair.root !== undefined) {
|
|
249
|
+
const a = frame.attachments.findIndex(attachment => attachment.slot === pair.a);
|
|
250
|
+
const b = frame.attachments.findIndex(attachment => attachment.slot === pair.b);
|
|
251
|
+
const under = a < b ? 'a' : 'b';
|
|
252
|
+
const underGhost = under === 'a' ? read.ghostA.worst : read.ghostB.worst;
|
|
253
|
+
const under_ghost_px = underGhost?.px ?? 0;
|
|
254
|
+
// Keep the existing visible-gap + lower ghost count against the union of
|
|
255
|
+
// the contributing raw boxes. Visibility changes counts, not the floor.
|
|
256
|
+
const bounds = [gap_open === null ? null : gap, underGhost === null ? null : ghost].filter(hit => hit !== null);
|
|
257
|
+
if (bounds.length > 0) root = {
|
|
258
|
+
px: (gap_open?.px ?? 0) + under_ghost_px, cell: bounds[0].cell,
|
|
259
|
+
box: [Math.min(...bounds.map(h => h.box[0])), Math.min(...bounds.map(h => h.box[1])),
|
|
260
|
+
Math.max(...bounds.map(h => h.box[2])), Math.max(...bounds.map(h => h.box[3]))],
|
|
261
|
+
under, under_ghost_px,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
const finding = frameFinding(kind, required, gap, ghost, gap_open, root);
|
|
265
|
+
return { ...id, status: 'MEASURED', gap, ghost, gap_open, gap_covered, ...(root === undefined ? {} : { root }), finding };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function readPairs(
|
|
269
|
+
frames: (Frame & SeamFrame)[], pairs: readonly MotionSeamPair[], required: boolean, poser: Poser, pages: ReadonlyMap<string, Plate>,
|
|
270
|
+
): PairReading[] {
|
|
271
|
+
const rest = poser.rest(undefined, frames.map(frame => frame.attachments));
|
|
272
|
+
const slots = new Set(poser.slots.map(slot => slot.name));
|
|
273
|
+
return pairs.map(pair => {
|
|
274
|
+
const names: SeamPair = { a: pair.a, b: pair.b };
|
|
275
|
+
const kind = motionSeamClass(pair);
|
|
276
|
+
const missing = [names.a, names.b].filter(slot => !slots.has(slot));
|
|
277
|
+
return {
|
|
278
|
+
...names, class: kind, declared: pair.declared,
|
|
279
|
+
frames: frames.map((frame): FrameReading => {
|
|
280
|
+
const { index, time } = frame;
|
|
281
|
+
if (missing.length > 0) return { index, time, status: 'SKIP', reason: 'SLOT_MISSING', detail: missing.join(', ') };
|
|
282
|
+
try {
|
|
283
|
+
const [read] = seamsOf([names], rest, [frame], pages);
|
|
284
|
+
return readFrame(pair, kind, read, frame, required);
|
|
285
|
+
} catch (err) {
|
|
286
|
+
if (!(err instanceof SeamError)) throw err;
|
|
287
|
+
return { index, time, status: 'SKIP', reason: 'CORE_SEAM_UNREADABLE', detail: err.message };
|
|
288
|
+
}
|
|
289
|
+
}),
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function summarize(pairs: readonly { frames: readonly FrameReading[] }[], required: boolean, unmeasuredRoots: number): BundleVerdict {
|
|
295
|
+
let measured = 0, skipped = 0, warning_frames = 0, failing_frames = 0;
|
|
296
|
+
for (const { frames } of pairs) for (const row of frames) {
|
|
297
|
+
if (row.status === 'SKIP') skipped++;
|
|
298
|
+
else {
|
|
299
|
+
measured++;
|
|
300
|
+
if (row.finding === 'OPENING_WARNING') warning_frames++;
|
|
301
|
+
if (row.finding === 'REQUIRED_BACKGROUND' || row.finding === 'BODY_ROOT_OPENING') failing_frames++;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
const reason = failing_frames > 0 ? 'OPENING' : pairs.length === 0 ? 'NO_DECLARED_PAIRS'
|
|
305
|
+
: skipped > 0 || unmeasuredRoots > 0 ? 'INCOMPLETE' : 'COMPLETE';
|
|
306
|
+
const status = reason === 'OPENING' || (reason !== 'COMPLETE' && required) ? 'FAIL' : reason === 'COMPLETE' ? 'PASS' : 'SKIP';
|
|
307
|
+
return { status, reason, measured, skipped, warning_frames, failing_frames };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function readBoundHit(value: unknown, where: string): MeasuredHit | null {
|
|
311
|
+
if (value === null) return null;
|
|
312
|
+
const { px, cell, box } = objectOf(value);
|
|
313
|
+
if (typeof px !== 'number' || !Number.isSafeInteger(px) || px < 1 ||
|
|
314
|
+
typeof cell !== 'number' || !Number.isFinite(cell) || cell <= 0 ||
|
|
315
|
+
!Array.isArray(box) || box.length !== 4 || !box.every(v => typeof v === 'number' && Number.isFinite(v)) ||
|
|
316
|
+
box[0] >= box[2] || box[1] >= box[3]) {
|
|
317
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', where, 'requires a positive cell count and size with a finite world box, or null for zero cells');
|
|
318
|
+
}
|
|
319
|
+
return { px, cell, box: [box[0], box[1], box[2], box[3]] };
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
function readBoundFrame(value: unknown, index: number, fps: number, kind: MotionSeamClass, required: boolean, where: string): FrameReading {
|
|
323
|
+
const row = objectOf(value);
|
|
324
|
+
const time = index * (1 / fps);
|
|
325
|
+
if (row.index !== index || row.time !== time) refusal('COMPOSE_MOTION_SEAMS_BINDING', where, `requires inventoried frame ${index} at ${time}s`);
|
|
326
|
+
if (row.status === 'SKIP') {
|
|
327
|
+
const reason = SKIP_REASONS.find(code => row.reason === code);
|
|
328
|
+
if (reason === undefined || row.finding !== undefined || (row.detail !== undefined && typeof row.detail !== 'string')) {
|
|
329
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', where, `skipped frames require a named reason from [${SKIP_REASONS.join(', ')}] and no finding`);
|
|
330
|
+
}
|
|
331
|
+
return { index, time, status: 'SKIP', reason, ...(row.detail === undefined ? {} : { detail: row.detail }) };
|
|
332
|
+
}
|
|
333
|
+
if (row.status !== 'MEASURED' || row.reason !== undefined) refusal('COMPOSE_MOTION_SEAMS_BINDING', where, 'requires a measured row or a named skip');
|
|
334
|
+
const gap = readBoundHit(row.gap, `${where}.gap`);
|
|
335
|
+
const ghost = readBoundHit(row.ghost, `${where}.ghost`);
|
|
336
|
+
const gap_open = readBoundHit(row.gap_open, `${where}.gap_open`);
|
|
337
|
+
const gap_covered = readBoundHit(row.gap_covered, `${where}.gap_covered`);
|
|
338
|
+
if ((gap_open?.px ?? 0) + (gap_covered?.px ?? 0) !== (gap?.px ?? 0)) {
|
|
339
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', where, 'gap_open and gap_covered counts must sum to the gap count');
|
|
340
|
+
}
|
|
341
|
+
let root: Extract<FrameReading, { status: 'MEASURED' }>['root'];
|
|
342
|
+
if (row.root !== undefined) {
|
|
343
|
+
const hit = readBoundHit(row.root, `${where}.root`);
|
|
344
|
+
const { under, under_ghost_px } = objectOf(row.root);
|
|
345
|
+
if (kind !== 'body-root' || hit === null || (under !== 'a' && under !== 'b') || typeof under_ghost_px !== 'number' ||
|
|
346
|
+
!Number.isSafeInteger(under_ghost_px) || under_ghost_px < 0 || under_ghost_px > (ghost?.px ?? 0) ||
|
|
347
|
+
hit.px !== (gap_open?.px ?? 0) + under_ghost_px) {
|
|
348
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', where, 'body-root counts must match the visible gap and lower-slot ghost counts');
|
|
349
|
+
}
|
|
350
|
+
root = { ...hit, under, under_ghost_px };
|
|
351
|
+
} else if (kind === 'body-root' && gap_open !== null) {
|
|
352
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', where, 'a visible body-root gap requires its root reading');
|
|
353
|
+
}
|
|
354
|
+
const finding = frameFinding(kind, required, gap, ghost, gap_open, root);
|
|
355
|
+
if (row.finding !== finding) refusal('COMPOSE_MOTION_SEAMS_BINDING', where, `finding ${String(row.finding)} disagrees with the measured cells; expected ${finding}`);
|
|
356
|
+
return { index, time, status: 'MEASURED', gap, ghost, gap_open, gap_covered, ...(root === undefined ? {} : { root }), finding };
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function writeComposeSeams(
|
|
360
|
+
out: string, request: ComposeSeamRequest, report: CheckReport,
|
|
361
|
+
viewOf: (time: number, duration: number) => Viewport,
|
|
362
|
+
) {
|
|
363
|
+
const dir = join(out, COMPOSE_SEAMS_DIR);
|
|
364
|
+
try {
|
|
365
|
+
const { choice, pageFiles, sources } = openBuild(out);
|
|
366
|
+
if (sources.model_sha256 !== report.figures.build.model_sha256) {
|
|
367
|
+
refusal('COMPOSE_MOTION_SEAMS_BUILD', 'check/build', 'the published packed model must match this compose invocation');
|
|
368
|
+
}
|
|
369
|
+
const { pairs: declared, roots, required: requiredBlock } = report.motionSeam;
|
|
370
|
+
const required = requiredBlock !== undefined;
|
|
371
|
+
const unmeasured_roots = roots?.unmeasured ?? [];
|
|
372
|
+
const pages = new Map(pageFiles.map(({ path, bytes }) => {
|
|
373
|
+
try {
|
|
374
|
+
assertPng(bytes, path);
|
|
375
|
+
return [path, decodePng(bytes)] as const;
|
|
376
|
+
} catch (err) {
|
|
377
|
+
return refusal('COMPOSE_MOTION_SEAMS_BUILD', path, `requires a readable packed PNG: ${(err as Error).message}`);
|
|
378
|
+
}
|
|
379
|
+
}));
|
|
380
|
+
mkdirSync(join(dir, 'frames'), { recursive: true });
|
|
381
|
+
const { value } = throughPoser(choice, poser => {
|
|
382
|
+
const take = poser.animations.find(take => take.name === request.animation);
|
|
383
|
+
if (take === undefined) refusal('COMPOSE_MOTION_SEAMS_ANIMATION', request.animation, `the composed build holds [${poser.animations.map(a => a.name).join(', ')}]`);
|
|
384
|
+
const duration = take.duration;
|
|
385
|
+
const count = sampleCount(duration, request);
|
|
386
|
+
const frames = sampleAnimation(poser, request.animation, request.fps, { geometry: true, preroll: request.preroll });
|
|
387
|
+
if (frames.length !== count) refusal('COMPOSE_MOTION_SEAMS_SAMPLE', request.animation, `Core returned ${frames.length} frames; expected ${count}`);
|
|
388
|
+
if (!frames.every((frame): frame is Frame & SeamFrame => frame.attachments !== undefined)) {
|
|
389
|
+
refusal('COMPOSE_MOTION_SEAMS_SAMPLE', request.animation, 'Core geometry is required on every sampled frame');
|
|
390
|
+
}
|
|
391
|
+
const rows = frames.map((frame): FrameRow => {
|
|
392
|
+
const path = framePath(frame.index);
|
|
393
|
+
const viewport = viewOf(frame.time, duration);
|
|
394
|
+
const png = renderFrame(frame, pages, viewport, [0, 0, 0, 0]);
|
|
395
|
+
const data = encodePng(png.width, png.height, png.data);
|
|
396
|
+
writeFileSync(join(dir, path), data);
|
|
397
|
+
return { index: frame.index, time: frame.time, viewport, path, sha256: hash(data) };
|
|
398
|
+
});
|
|
399
|
+
const inventory: FrameInventory = { spec: INVENTORY_SPEC, animation: request.animation, fps: request.fps,
|
|
400
|
+
duration, frame_count: frames.length, frames: rows };
|
|
401
|
+
const pairs = readPairs(frames, declared, required, poser, pages);
|
|
402
|
+
return { inventory, pairs };
|
|
403
|
+
});
|
|
404
|
+
const { inventory, pairs } = value;
|
|
405
|
+
const inventoryText = rigJsonText(inventory);
|
|
406
|
+
writeFileSync(join(dir, 'inventory.json'), inventoryText);
|
|
407
|
+
const inventoryFile = join(dir, 'inventory.json');
|
|
408
|
+
const inventoryBytes = bytesAt(inventoryFile, 'COMPOSE_MOTION_SEAMS_INVENTORY');
|
|
409
|
+
validateInventory(dir, jsonOf(inventoryBytes, inventoryFile, 'COMPOSE_MOTION_SEAMS_INVENTORY'), request, inventory.duration);
|
|
410
|
+
const body = {
|
|
411
|
+
spec: COMPOSE_SEAMS_REPORT, core: COMPOSE_SEAMS_CORE, animation: request.animation, fps: request.fps,
|
|
412
|
+
frame_count: inventory.frame_count, request_sha256: jsonDigest(request), sources, inventory_sha256: hash(inventoryBytes),
|
|
413
|
+
required, unmeasured_roots, pairs, verdict: summarize(pairs, required, unmeasured_roots.length),
|
|
414
|
+
};
|
|
415
|
+
writeFileSync(join(dir, 'report.json'), rigJsonText({ ...body, binding_sha256: jsonDigest(body) }));
|
|
416
|
+
return body;
|
|
417
|
+
} catch (err) {
|
|
418
|
+
clearComposeSeams(out);
|
|
419
|
+
if (err instanceof PartsError) throw err;
|
|
420
|
+
return refusal('COMPOSE_MOTION_SEAMS_SAMPLE', request.animation, (err as Error).message);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/** Re-read the packed build and all named PNGs before returning the attested verdict. */
|
|
425
|
+
export function verifyComposeSeams(out: string, request: ComposeSeamRequest) {
|
|
426
|
+
const dir = join(out, COMPOSE_SEAMS_DIR);
|
|
427
|
+
const path = join(dir, 'report.json');
|
|
428
|
+
const { binding_sha256, ...body } = jsonOf(bytesAt(path, 'COMPOSE_MOTION_SEAMS_BINDING'), path, 'COMPOSE_MOTION_SEAMS_BINDING');
|
|
429
|
+
if (body.fps !== request.fps) refusal('COMPOSE_MOTION_SEAMS_FPS', path, `has ${String(body.fps)} fps; consumer requires ${request.fps} fps`);
|
|
430
|
+
if (body.spec !== COMPOSE_SEAMS_REPORT || body.core !== COMPOSE_SEAMS_CORE || body.animation !== request.animation ||
|
|
431
|
+
body.request_sha256 !== jsonDigest(request) || binding_sha256 !== jsonDigest(body)) {
|
|
432
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', path, 'the report, consumer request and binding digest must agree');
|
|
433
|
+
}
|
|
434
|
+
const file = join(dir, 'inventory.json');
|
|
435
|
+
const inventoryBytes = bytesAt(file, 'COMPOSE_MOTION_SEAMS_INVENTORY');
|
|
436
|
+
const inventory = jsonOf(inventoryBytes, file, 'COMPOSE_MOTION_SEAMS_INVENTORY');
|
|
437
|
+
if (inventory.fps !== request.fps) refusal('COMPOSE_MOTION_SEAMS_FPS', file, `has ${String(inventory.fps)} fps; consumer requires ${request.fps} fps`);
|
|
438
|
+
if (body.inventory_sha256 !== hash(inventoryBytes) || body.frame_count !== inventory.frame_count) {
|
|
439
|
+
refusal('COMPOSE_MOTION_SEAMS_INVENTORY', file, `inventory bytes and frame count must match ${path}`);
|
|
440
|
+
}
|
|
441
|
+
const { choice, sources, declaration } = openBuild(out);
|
|
442
|
+
if (jsonDigest(body.sources) !== jsonDigest(sources)) refusal('COMPOSE_MOTION_SEAMS_BUILD', path, 'the packed build, scene and check bytes must match the report');
|
|
443
|
+
const { value: duration } = throughPoser(choice, poser => poser.animations.find(take => take.name === request.animation)?.duration);
|
|
444
|
+
if (duration === undefined) refusal('COMPOSE_MOTION_SEAMS_ANIMATION', request.animation, 'the packed build must hold the bound take');
|
|
445
|
+
validateInventory(dir, inventory, request, duration);
|
|
446
|
+
const pairs: unknown[] = Array.isArray(body.pairs) ? body.pairs : [];
|
|
447
|
+
const declared: unknown[] = Array.isArray(declaration.pairs) ? declaration.pairs : [];
|
|
448
|
+
const required = declaration.required !== undefined;
|
|
449
|
+
if (!Array.isArray(body.pairs) || pairs.length !== declared.length || body.required !== required) {
|
|
450
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', path, 'the verdict must cover the declared pairs and required mode in the composed check');
|
|
451
|
+
}
|
|
452
|
+
const unmeasuredRoots = objectOf(declaration.roots).unmeasured ?? [];
|
|
453
|
+
if (!Array.isArray(unmeasuredRoots) || jsonDigest(body.unmeasured_roots) !== jsonDigest(unmeasuredRoots)) {
|
|
454
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', path, 'the unmeasured roots must match the composed check');
|
|
455
|
+
}
|
|
456
|
+
const readings = pairs.map((value, i) => {
|
|
457
|
+
const pair = objectOf(value);
|
|
458
|
+
const source = objectOf(declared[i]);
|
|
459
|
+
const origins = source.declared;
|
|
460
|
+
if (typeof source.a !== 'string' || typeof source.b !== 'string' || !Array.isArray(origins) || !origins.every(d => typeof d === 'string') ||
|
|
461
|
+
pair.a !== source.a || pair.b !== source.b || jsonDigest(pair.declared) !== jsonDigest(origins)) {
|
|
462
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', path, `pairs[${i}] must match the literal names and declarations in the composed check`);
|
|
463
|
+
}
|
|
464
|
+
const kind = motionSeamClass(source as unknown as MotionSeamPair);
|
|
465
|
+
if (pair.class !== kind) refusal('COMPOSE_MOTION_SEAMS_BINDING', path, `pairs[${i}] class ${String(pair.class)} disagrees with the composed check class ${kind}`);
|
|
466
|
+
const frames: unknown[] = Array.isArray(pair.frames) ? pair.frames : [];
|
|
467
|
+
if (frames.length !== inventory.frame_count) refusal('COMPOSE_MOTION_SEAMS_BINDING', path, `pairs[${i}] must cover every inventoried frame in order`);
|
|
468
|
+
return { frames: frames.map((frame, index) =>
|
|
469
|
+
readBoundFrame(frame, index, request.fps, kind, required, `${path} pairs[${i}].frames[${index}]`)) };
|
|
470
|
+
});
|
|
471
|
+
const verdict = summarize(readings, required, unmeasuredRoots.length);
|
|
472
|
+
if (jsonDigest(body.verdict) !== jsonDigest(verdict)) {
|
|
473
|
+
refusal('COMPOSE_MOTION_SEAMS_BINDING', path, `the aggregate verdict disagrees with validated rows: expected ${JSON.stringify(verdict)}`);
|
|
474
|
+
}
|
|
475
|
+
return { status: verdict.status, reason: verdict.reason, frameCount: inventory.frame_count, bindingSha256: binding_sha256 };
|
|
476
|
+
}
|
package/src/layers.ts
CHANGED
|
@@ -195,10 +195,49 @@ function describe(v: unknown): string {
|
|
|
195
195
|
// the wrapper form
|
|
196
196
|
// ---------------------------------------------------------------------------
|
|
197
197
|
|
|
198
|
-
const MANIFEST_KEYS = ['prefix', 'timestamp', 'layers', 'width', 'height'] as const;
|
|
198
|
+
const MANIFEST_KEYS = ['prefix', 'timestamp', 'layers', 'width', 'height', 'source'] as const;
|
|
199
199
|
const MANIFEST_REQUIRED = ['layers', 'width', 'height'] as const;
|
|
200
200
|
const LAYER_KEYS = ['name', 'filename', 'left', 'top', 'right', 'bottom', 'depth_median', 'depth_filename'] as const;
|
|
201
201
|
const LAYER_REQUIRED = ['name', 'filename', 'left', 'top', 'right', 'bottom', 'depth_median'] as const;
|
|
202
|
+
const EXPORT_SOURCE_FORMATS = ['psd', 'wrapper-directory', 'layers.json'] as const;
|
|
203
|
+
const EXPORT_COMMAND = 'rig-parts layers --export';
|
|
204
|
+
|
|
205
|
+
interface LayerExportSource {
|
|
206
|
+
path: string;
|
|
207
|
+
sha256: string;
|
|
208
|
+
format: typeof EXPORT_SOURCE_FORMATS[number];
|
|
209
|
+
parts_version: string;
|
|
210
|
+
command: typeof EXPORT_COMMAND;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function checkExportSource(value: unknown, manifestPath: string, problems: Problem[]): void {
|
|
214
|
+
const refuse = (field: string, detail: string): void => {
|
|
215
|
+
problems.push({ code: 'LAYERS_EXPORT_SOURCE', object: `${manifestPath} field "${field}"`, detail });
|
|
216
|
+
};
|
|
217
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
218
|
+
refuse('source', `is ${describe(value)}; an object is required`);
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const source = value as Record<string, unknown>;
|
|
222
|
+
const fields = {
|
|
223
|
+
path: 'a non-empty string',
|
|
224
|
+
sha256: '64 lowercase hexadecimal digits',
|
|
225
|
+
format: `one of ${EXPORT_SOURCE_FORMATS.join(', ')}`,
|
|
226
|
+
parts_version: 'a non-empty string',
|
|
227
|
+
command: JSON.stringify(EXPORT_COMMAND),
|
|
228
|
+
} satisfies Record<keyof LayerExportSource, string>;
|
|
229
|
+
for (const key of Object.keys(source)) {
|
|
230
|
+
if (!Object.hasOwn(fields, key)) refuse(`source.${key}`, `is unknown; known keys are ${Object.keys(fields).join(', ')}`);
|
|
231
|
+
}
|
|
232
|
+
const invalid = (key: keyof LayerExportSource): void => {
|
|
233
|
+
refuse(`source.${key}`, `${Object.hasOwn(source, key) ? `is ${describe(source[key])}` : 'is absent'}; required: ${fields[key]}`);
|
|
234
|
+
};
|
|
235
|
+
if (typeof source.path !== 'string' || source.path.length === 0) invalid('path');
|
|
236
|
+
if (typeof source.sha256 !== 'string' || !/^[a-f0-9]{64}$/.test(source.sha256)) invalid('sha256');
|
|
237
|
+
if (typeof source.format !== 'string' || !(EXPORT_SOURCE_FORMATS as readonly string[]).includes(source.format)) invalid('format');
|
|
238
|
+
if (typeof source.parts_version !== 'string' || source.parts_version.length === 0) invalid('parts_version');
|
|
239
|
+
if (source.command !== EXPORT_COMMAND) invalid('command');
|
|
240
|
+
}
|
|
202
241
|
|
|
203
242
|
export interface WrapperManifestReading {
|
|
204
243
|
path: string;
|
|
@@ -292,7 +331,9 @@ export function readWrapperLayers(
|
|
|
292
331
|
if ('layers' in m && !Array.isArray(m.layers)) {
|
|
293
332
|
refuse('LAYERS_FIELD_PRESENT', `${manifestPath} field "layers"`, `is ${describe(m.layers)}; an array is required`);
|
|
294
333
|
}
|
|
295
|
-
|
|
334
|
+
const malformedManifest = problems.length > 0;
|
|
335
|
+
if ('source' in m) checkExportSource(m.source, manifestPath, problems);
|
|
336
|
+
if (malformedManifest) refuseIfAny(problems);
|
|
296
337
|
const W = width as number;
|
|
297
338
|
const H = height as number;
|
|
298
339
|
const entries = m.layers as unknown[];
|
|
@@ -489,6 +530,10 @@ function stubCanvas(): void {
|
|
|
489
530
|
const PSD_RGB = 3;
|
|
490
531
|
|
|
491
532
|
export function readPsdLayers(path: string): LayerSet {
|
|
533
|
+
return readPsdInput(path);
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function readPsdInput(path: string, recordSource?: (bytes: Buffer) => void): LayerSet {
|
|
492
537
|
const problems: Problem[] = [];
|
|
493
538
|
const refuse = (code: string, object: string, detail: string): void => {
|
|
494
539
|
problems.push({ code, object, detail });
|
|
@@ -500,7 +545,9 @@ export function readPsdLayers(path: string): LayerSet {
|
|
|
500
545
|
stubCanvas();
|
|
501
546
|
let psd;
|
|
502
547
|
try {
|
|
503
|
-
|
|
548
|
+
const bytes = readFileSync(path);
|
|
549
|
+
recordSource?.(bytes);
|
|
550
|
+
psd = readPsd(bytes, {
|
|
504
551
|
useImageData: true,
|
|
505
552
|
skipCompositeImageData: true,
|
|
506
553
|
skipThumbnail: true,
|
|
@@ -605,7 +652,11 @@ export function readPsdLayers(path: string): LayerSet {
|
|
|
605
652
|
|
|
606
653
|
/** Read whichever form `path` is: a `.psd` file, or a wrapper directory / `layers.json`. */
|
|
607
654
|
export function readLayers(path: string, binding?: DepthBinding, readings?: WrapperReadings): LayerSet {
|
|
608
|
-
|
|
655
|
+
return readLayerInput(path, binding, readings);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function readLayerInput(path: string, binding?: DepthBinding, readings?: WrapperReadings, recordPsd?: (bytes: Buffer) => void): LayerSet {
|
|
659
|
+
if (/\.psd$/i.test(path)) { const set = readPsdInput(path, recordPsd); return binding === undefined ? set : bindDepthRun(set, binding.run, binding.config, binding.base); }
|
|
609
660
|
if (existsSync(path) && (statSync(path).isDirectory() || basename(path) === 'layers.json')) return readWrapperLayers(path, binding, undefined, readings);
|
|
610
661
|
refuseIfAny([
|
|
611
662
|
{
|
|
@@ -623,6 +674,20 @@ export function readLayers(path: string, binding?: DepthBinding, readings?: Wrap
|
|
|
623
674
|
// the export and the template (issue #359)
|
|
624
675
|
// ---------------------------------------------------------------------------
|
|
625
676
|
|
|
677
|
+
/** Capture the exact PSD or manifest bytes consumed by the export's read. */
|
|
678
|
+
export function readLayersForExport(path: string, partsVersion: string): { set: LayerSet; source: LayerExportSource } {
|
|
679
|
+
const readings: WrapperReadings = { pngs: [] };
|
|
680
|
+
let psdSha256: string | undefined;
|
|
681
|
+
const set = readLayerInput(path, undefined, readings, (bytes) => {
|
|
682
|
+
psdSha256 = createHash('sha256').update(bytes).digest('hex');
|
|
683
|
+
});
|
|
684
|
+
const manifest = readings.manifest;
|
|
685
|
+
const sha256 = psdSha256 ?? manifest?.sha256;
|
|
686
|
+
if (sha256 === undefined) throw new Error('layers export read did not capture its source bytes');
|
|
687
|
+
const format = set.form === 'psd' ? 'psd' : manifest?.givenAsDirectory ? 'wrapper-directory' : 'layers.json';
|
|
688
|
+
return { set, source: { path, sha256, format, parts_version: partsVersion, command: EXPORT_COMMAND } };
|
|
689
|
+
}
|
|
690
|
+
|
|
626
691
|
/**
|
|
627
692
|
* The file a layer is exported to: its index in the manifest, zero-padded to
|
|
628
693
|
* the width of the last index (at least two digits), then its name with every
|
|
@@ -636,7 +701,7 @@ export function exportFileName(index: number, count: number, name: string): stri
|
|
|
636
701
|
}
|
|
637
702
|
|
|
638
703
|
/** The wrapper manifest `exportLayers` writes, as text: fixed key order, two-space indent, a trailing newline. */
|
|
639
|
-
export function exportManifestText(set: LayerSet, binding?: DepthBinding): string {
|
|
704
|
+
export function exportManifestText(set: LayerSet, binding?: DepthBinding, source?: LayerExportSource): string {
|
|
640
705
|
set = bindDepthRun(set, binding?.run ?? 'full', binding?.config, binding?.base ?? '');
|
|
641
706
|
const all: Array<{ name: string; left: number; top: number; right: number; bottom: number; depth: number | null; depthSheet?: SourceDepthSheet }> = [
|
|
642
707
|
...set.layers.map((l) => ({ name: l.name, left: l.left, top: l.top, right: l.right, bottom: l.bottom, depth: l.depth, ...(l.depthSheet === undefined ? {} : { depthSheet: l.depthSheet }) })),
|
|
@@ -646,6 +711,7 @@ export function exportManifestText(set: LayerSet, binding?: DepthBinding): strin
|
|
|
646
711
|
width: set.canvas.w,
|
|
647
712
|
height: set.canvas.h,
|
|
648
713
|
layers: all.map((l, i) => ({ name: l.name, filename: exportFileName(i, all.length, l.name), left: l.left, top: l.top, right: l.right, bottom: l.bottom, depth_median: l.depth, ...(l.depthSheet === undefined ? {} : { depth_filename: exportFileName(i, all.length, `${l.name}_depth`) }) })),
|
|
714
|
+
...(source === undefined ? {} : { source: { path: source.path, sha256: source.sha256, format: source.format, parts_version: source.parts_version, command: source.command } }),
|
|
649
715
|
};
|
|
650
716
|
return `${JSON.stringify(manifest, null, 2)}\n`;
|
|
651
717
|
}
|
|
@@ -666,7 +732,7 @@ export function exportManifestText(set: LayerSet, binding?: DepthBinding): strin
|
|
|
666
732
|
* `parts/<name>.png` the reader also looks for — could be read in place of
|
|
667
733
|
* the one written now. Writes nothing until that has passed.
|
|
668
734
|
*/
|
|
669
|
-
export function exportLayers(set: LayerSet, dir: string, binding?: DepthBinding): { manifest: string; files: string[] } {
|
|
735
|
+
export function exportLayers(set: LayerSet, dir: string, binding?: DepthBinding, source?: LayerExportSource): { manifest: string; files: string[] } {
|
|
670
736
|
if (existsSync(dir) && (!statSync(dir).isDirectory() || readdirSync(dir).length > 0)) {
|
|
671
737
|
refuseIfAny([
|
|
672
738
|
{
|
|
@@ -677,7 +743,7 @@ export function exportLayers(set: LayerSet, dir: string, binding?: DepthBinding)
|
|
|
677
743
|
]);
|
|
678
744
|
}
|
|
679
745
|
set = bindDepthRun(set, binding?.run ?? 'full', binding?.config, binding?.base ?? '');
|
|
680
|
-
const text = exportManifestText(set, binding);
|
|
746
|
+
const text = exportManifestText(set, binding, source);
|
|
681
747
|
const entries = (JSON.parse(text) as { layers: Array<{ filename: string }> }).layers;
|
|
682
748
|
const pixels = [...set.layers.map((l) => l.pixels), ...set.hidden.map((h) => h.pixels)];
|
|
683
749
|
mkdirSync(dir, { recursive: true });
|