feat: export Director cameras for Blender

This commit is contained in:
disco committed 2026-10-28 20:13:26 -06:00
1 parent f2c6ff082d
commit 9f32bf3d92
12 files changed
+1536 -1

No files matched your search

+276
View File
@@ -0,0 +1,276 @@
/** Camera-only interchange. No viewer, providers, environment files or terrain access. */
import * as C from 'cesium';
import { validateSceneDocument } from '../director/document.js';
import {
resolveCameraPose,
resolveCameraMove,
sampleCameraMove,
} from '../director/camera.js';
import { cameraAtProgress } from '../director/timeline.js';
export const CAMERA_MANIFEST_SCHEMA = 'gev.camera-manifest.v1';
export const MAX_CAMERA_FRAMES = 100000;
function number(value, label, min, max) {
if (
typeof value !== 'number' ||
!Number.isFinite(value) ||
value < min ||
value > max
) {
throw new RangeError(
`${label} must be a finite number in [${min}, ${max}]`,
);
}
return value;
}
function position(pose, heightKey = 'alt') {
if (!pose) throw new TypeError('A complete camera position is required');
number(pose.lat, 'lat', -90, 90);
number(pose.lon, 'lon', -180, 180);
number(pose[heightKey], heightKey, -12000, 1e9);
}
/** Convert Cesium's WGS84/heading-pitch-roll pose to a fixed Blender ENU frame.
* Angles are degrees; alt and origin.height are WGS84 ellipsoid meters.
* The returned columns are Blender's local X (right), Y (up), Z (back).
*/
export function cameraPoseToEnu(pose, origin) {
position(pose);
position(origin, 'height');
// Short-arc sampling can pass 360 degrees even when both saved endpoints do not.
for (const key of ['heading', 'roll']) number(pose[key], key, -720, 720);
number(pose.pitch, 'pitch', -90, 90);
const xyz = C.Cartesian3.fromDegrees(
pose.lon,
pose.lat,
pose.alt,
C.Ellipsoid.WGS84,
);
const anchor = C.Cartesian3.fromDegrees(
origin.lon,
origin.lat,
origin.height,
C.Ellipsoid.WGS84,
);
if (C.Cartesian3.distance(xyz, anchor) > 100000)
throw new RangeError('Camera exceeds the 100 km local-origin limit');
const inverse = C.Matrix4.inverseTransformation(
C.Transforms.eastNorthUpToFixedFrame(anchor),
new C.Matrix4(),
);
const local = C.Matrix4.multiplyByPoint(inverse, xyz, new C.Cartesian3());
// Same basis as Cesium Camera.setView3D, including its heading - PI/2 offset.
// This is the coordinate math retained from the staged ENU camera prototype.
const rotation = C.Matrix3.fromQuaternion(
C.Quaternion.fromHeadingPitchRoll(
new C.HeadingPitchRoll(
C.Math.toRadians(pose.heading) - Math.PI / 2,
C.Math.toRadians(pose.pitch),
C.Math.toRadians(pose.roll),
),
),
);
const cameraFrame = C.Transforms.eastNorthUpToFixedFrame(xyz);
const transform = (vector) =>
C.Matrix4.multiplyByPointAsVector(
inverse,
C.Matrix4.multiplyByPointAsVector(
cameraFrame,
vector,
new C.Cartesian3(),
),
new C.Cartesian3(),
);
const forward = transform(
C.Matrix3.getColumn(rotation, 0, new C.Cartesian3()),
);
const up = transform(C.Matrix3.getColumn(rotation, 2, new C.Cartesian3()));
const right = C.Cartesian3.cross(forward, up, new C.Cartesian3());
const array = (v) => [v.x, v.y, v.z];
return {
position: array(local),
basisColumns: [
array(right),
array(up),
array(C.Cartesian3.negate(forward, new C.Cartesian3())),
],
};
}
function identity(value, label) {
if (typeof value !== 'string' || !value.trim())
throw new TypeError(`${label} requires a saved ID`);
return value;
}
/** Export one existing scene using Director seek interpolation and authored timing.
* Projection is explicit because scene documents do not store viewport lenses.
* Frame 1 samples time zero; the final sample precedes the exclusive end time.
*/
export function exportCameraManifest(
project,
{
sceneId,
origin,
fps,
width,
height,
horizontalFovDegrees,
sensorWidthMm = 36,
} = {},
) {
validateSceneDocument(project);
position(origin, 'height');
for (const key of ['numerator', 'denominator']) {
number(fps?.[key], `fps.${key}`, 1, 1000000);
if (!Number.isInteger(fps[key]))
throw new RangeError('fps must be a positive integer ratio');
}
const rate = number(fps.numerator / fps.denominator, 'frame rate', 1, 240);
for (const [key, value] of Object.entries({ width, height })) {
number(value, key, 4, 16384);
if (!Number.isInteger(value))
throw new RangeError(`${key} must be an integer`);
}
number(horizontalFovDegrees, 'horizontal FOV degrees', 1, 179);
number(sensorWidthMm, 'sensor width mm', 1, 100);
const candidates = project.scenes.filter(
(scene) => sceneId === undefined || scene.id === sceneId,
);
if (candidates.length !== 1)
throw new Error('Select exactly one scene with sceneId');
const scene = candidates[0];
identity(scene.id, 'Scene');
if (!scene.shots.length) throw new Error('Scene has no camera shots');
const frameAt = (seconds) => {
const frame = seconds * rate;
return (
(Math.abs(frame - Math.round(frame)) < 1e-9
? Math.round(frame)
: Math.ceil(frame)) + 1
);
};
let elapsed = 0;
let rounding = 0;
const shots = scene.shots.map((shot) => {
identity(shot.id, 'Shot');
const camera = resolveCameraPose(scene, shot.camera);
position(camera);
const move = resolveCameraMove(scene, shot);
if (move) position(move.from);
cameraPoseToEnu(camera, origin);
if (move) cameraPoseToEnu(move.from, origin);
const durationSec = shot.durationSec || 4;
const holdSec = shot.holdSec || 0;
const startTimeSec = elapsed;
// Compensated summation prevents long decimal timelines gaining a frame.
const increment = durationSec + holdSec - rounding;
const next = elapsed + increment;
rounding = next - elapsed - increment;
elapsed = next;
const frameStart = frameAt(startTimeSec);
const frameEndExclusive = frameAt(elapsed);
if (frameStart >= frameEndExclusive)
throw new Error(`Shot ${shot.id} has no sampled frame at this fps`);
return {
id: shot.id,
title: shot.title || '',
...(shot.sourcePackId
? {
sourcePackId: shot.sourcePackId,
sourcePackVersion: shot.sourcePackVersion ?? null,
}
: {}),
startTimeSec,
endTimeSec: elapsed,
durationSec,
holdSec,
frameStart,
frameEndExclusive,
camera,
move,
};
});
const frameCount = frameAt(elapsed) - 1;
if (frameCount > MAX_CAMERA_FRAMES)
throw new RangeError(`Export exceeds ${MAX_CAMERA_FRAMES} camera frames`);
const frames = [];
let shotIndex = 0;
for (let frame = 1; frame <= frameCount; frame += 1) {
while (
frame >= shots[shotIndex].frameEndExclusive &&
shotIndex < shots.length - 1
)
shotIndex += 1;
const shot = shots[shotIndex];
const timeSec = ((frame - 1) * fps.denominator) / fps.numerator;
const progress = Math.max(
0,
Math.min(
1,
(timeSec - shot.startTimeSec) / Math.max(0.001, shot.durationSec),
),
);
const pose = shot.move
? sampleCameraMove(shot.move, progress)
: cameraAtProgress(
shots[shotIndex - 1]?.camera || shot.camera,
shot.camera,
progress,
);
frames.push({
frame,
timeSec,
shotId: shot.id,
...cameraPoseToEnu(pose, origin),
});
}
const horizontalFovRadians = C.Math.toRadians(horizontalFovDegrees);
const lensMm = number(
sensorWidthMm / (2 * Math.tan(horizontalFovRadians / 2)),
'Blender lens mm',
1,
10000,
);
return {
schema: CAMERA_MANIFEST_SCHEMA,
source: {
sceneId: scene.id,
sceneTitle: scene.title || '',
documentVersion: project.version ?? 1,
sampling: 'director-seek-authored-timing',
attributions: (scene.dataPacks || []).map(({ attribution }) =>
structuredClone(attribution),
),
},
coordinates: {
units: 'meters',
axes: 'east-north-up',
heightDatum: 'WGS84-ellipsoid',
origin: { ...origin },
cameraAxes: 'X-right,Y-up,-Z-forward',
},
camera: {
projection: 'perspective',
sensorFit: 'HORIZONTAL',
sensorWidthMm,
lensMm,
horizontalFovRadians,
verticalFovRadians:
2 * Math.atan((Math.tan(horizontalFovRadians / 2) * height) / width),
},
timeline: {
fps: { ...fps },
width,
height,
pixelAspect: [1, 1],
frameStart: 1,
frameEnd: frameCount,
durationSec: elapsed,
},
shots,
frames,
};
}
+367
View File
@@ -0,0 +1,367 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import {
mkdtempSync,
writeFileSync,
readFileSync,
rmSync,
existsSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { spawnSync } from 'node:child_process';
import { sceneSeekState } from '../director/timeline.js';
const adapter = await import('./blenderCamera.js');
test('a portable camera manifest adapter exists', () => {
assert.equal(typeof adapter.exportCameraManifest, 'function');
});
const origin = { lat: 0, lon: 0, height: 0 };
const pose = (changes = {}) => ({
lat: 0,
lon: 0,
alt: 0,
heading: 0,
pitch: 0,
roll: 0,
...changes,
});
const options = (changes = {}) => ({
origin,
fps: { numerator: 30, denominator: 1 },
width: 1920,
height: 1080,
horizontalFovDegrees: 90,
...changes,
});
const project = (
shots = [{ id: 'north', durationSec: 1, holdSec: 0, camera: pose() }],
) => ({
version: 6,
scenes: [{ id: 'scene', title: 'Synthetic cameras', shots }],
});
const near = (a, b, eps = 1e-8) =>
a.forEach((v, i) => assert.ok(Math.abs(v - b[i]) < eps, `${a} != ${b}`));
const camera = (changes = {}, opts = {}) =>
adapter.exportCameraManifest(
project([{ id: 'camera', durationSec: 1, camera: pose(changes) }]),
options(opts),
).frames[0];
test('cardinal cameras use Blender X right, Y up, negative Z forward', () => {
for (const [heading, right, back] of [
[0, [1, 0, 0], [0, -1, 0]],
[90, [0, -1, 0], [-1, 0, 0]],
[180, [-1, 0, 0], [0, 1, 0]],
[270, [0, 1, 0], [1, 0, 0]],
]) {
const result = camera({ heading });
near(result.basisColumns[0], right);
near(result.basisColumns[1], [0, 0, 1]);
near(result.basisColumns[2], back);
}
near(camera({ pitch: -90 }).basisColumns[2], [0, 0, 1]);
near(camera({ roll: 90 }).basisColumns[1], [1, 0, 0]);
});
test('ENU translation uses WGS84 meters and ellipsoid heights', () => {
near(camera({ alt: 100 }).position, [0, 0, 100]);
const east = camera({ lon: 0.001 }).position;
assert.ok(Math.abs(east[0] - 111.3194907876) < 1e-6);
assert.ok(Math.abs(east[2] + 0.0009714458) < 1e-6);
});
test('changed tangent frames and nonzero roll match an independent Cesium Camera', () => {
const anchor = { lat: 39.7, lon: -105, height: 1600 };
const p = pose({
lat: 39.73,
lon: -104.97,
alt: 2400,
heading: 47,
pitch: -29,
roll: 37,
});
const reference = new Cesium.Camera({
drawingBufferWidth: 1920,
drawingBufferHeight: 1080,
mapProjection: new Cesium.GeographicProjection(),
mode: Cesium.SceneMode.SCENE3D,
});
reference.setView({
destination: Cesium.Cartesian3.fromDegrees(p.lon, p.lat, p.alt),
orientation: {
heading: Cesium.Math.toRadians(p.heading),
pitch: Cesium.Math.toRadians(p.pitch),
roll: Cesium.Math.toRadians(p.roll),
},
});
const inverse = Cesium.Matrix4.inverseTransformation(
Cesium.Transforms.eastNorthUpToFixedFrame(
Cesium.Cartesian3.fromDegrees(anchor.lon, anchor.lat, anchor.height),
),
new Cesium.Matrix4(),
);
const arr = (v) => [v.x, v.y, v.z];
const local = (v) =>
arr(
Cesium.Matrix4.multiplyByPointAsVector(
inverse,
v,
new Cesium.Cartesian3(),
),
);
const actual = camera(p, { origin: anchor });
near(actual.basisColumns[0], local(reference.rightWC));
near(actual.basisColumns[1], local(reference.upWC));
near(
actual.basisColumns[2],
local(
Cesium.Cartesian3.negate(reference.directionWC, new Cesium.Cartesian3()),
),
);
});
test('explicit horizontal lens works for landscape and portrait output', () => {
for (const [width, height] of [
[1920, 1080],
[1080, 1920],
]) {
const m = adapter.exportCameraManifest(
project(),
options({ width, height }),
);
assert.ok(Math.abs(m.camera.lensMm - 18) < 1e-10);
assert.equal(m.camera.sensorFit, 'HORIZONTAL');
assert.ok(
Math.abs(m.camera.verticalFovRadians - 2 * Math.atan(height / width)) <
1e-10,
);
}
});
test('anchors, cubic moves, cuts, holds and rational frame timing survive JSON round trip', () => {
const p = project([
{
id: 'move',
durationSec: 0.4,
holdSec: 0.2,
camera: { anchorId: 'end', heading: 10, pitch: 0, roll: 20 },
move: {
from: {
...pose({ lon: 179.999, heading: 350 }),
altitudeReference: 'ellipsoid',
},
easing: 'cubic-in-out',
},
},
{
id: 'cut',
durationSec: 0.2,
holdSec: 0,
camera: pose({ lon: -179.999, heading: 90 }),
},
]);
p.scenes[0].anchors = [
{
id: 'end',
lat: 0,
lon: -179.999,
alt: 20,
altitudeReference: 'ellipsoid',
},
];
const m = adapter.exportCameraManifest(
p,
options({
origin: { lat: 0, lon: 180, height: 0 },
fps: { numerator: 30000, denominator: 1001 },
}),
);
assert.equal(m.frames.length, 24);
assert.equal(m.source.sceneId, 'scene');
assert.deepEqual(
m.shots.map((s) => s.id),
['move', 'cut'],
);
assert.equal(m.shots[0].endTimeSec, 0.6000000000000001);
assert.equal(m.frames[18].shotId, 'cut');
assert.equal(m.frames[18].timeSec, (18 * 1001) / 30000);
near(m.frames[12].position, m.frames[17].position);
assert.deepEqual(JSON.parse(JSON.stringify(m)), m);
assert.equal(p.scenes[0].shots[0].camera.anchorId, 'end');
});
test('invalid or ambiguous input fails before export', () => {
for (const change of [
{ fps: { numerator: 0, denominator: 1 } },
{ horizontalFovDegrees: undefined },
{ width: 0 },
{ origin: { lat: 0, lon: NaN, height: 0 } },
{ horizontalFovDegrees: 180 },
]) {
assert.throws(() =>
adapter.exportCameraManifest(project(), options(change)),
);
}
assert.throws(() => camera({ lon: 20 }), /100 km/);
const multiple = project();
multiple.scenes.push({ ...multiple.scenes[0], id: 'other' });
assert.throws(
() => adapter.exportCameraManifest(multiple, options()),
/scene/i,
);
assert.throws(
() =>
adapter.exportCameraManifest(
project([{ id: 'missing', camera: { heading: 0 } }]),
options(),
),
/lat|position/i,
);
assert.throws(
() =>
adapter.exportCameraManifest(
project([{ id: 'long', camera: pose(), durationSec: 86400 }]),
options(),
),
/frame/i,
);
});
test('projection settings cannot be silently clamped by Blender', () => {
assert.throws(
() => adapter.exportCameraManifest(project(), options({ width: 1 })),
/width/,
);
assert.throws(
() =>
adapter.exportCameraManifest(
project(),
options({ horizontalFovDegrees: 179, sensorWidthMm: 1 }),
),
/lens/i,
);
});
test('every exported sample agrees with Director seeking at fractional shot boundaries', () => {
const p = project([
{
id: 'first',
camera: pose({ heading: 350, roll: -350 }),
durationSec: 0.21,
holdSec: 0.11,
},
{
id: 'second',
camera: pose({ lon: 0.001, heading: 10, roll: 350 }),
durationSec: 0.22,
holdSec: 0.13,
},
]);
const m = adapter.exportCameraManifest(p, options());
assert.equal(m.frames.length, 21);
for (const sample of m.frames) {
const state = sceneSeekState(
p.scenes[0],
sample.timeSec / m.timeline.durationSec,
(_scene, shot) => shot.durationSec + shot.holdSec,
(_scene, shot) => shot.holdSec,
);
const expected = adapter.cameraPoseToEnu(state.camera, origin);
assert.equal(sample.shotId, state.shot.id);
near(sample.position, expected.position);
sample.basisColumns.forEach((axis, i) =>
near(axis, expected.basisColumns[i]),
);
}
});
test('CLI works in paths with spaces and refuses overwrites or invalid projection', () => {
const directory = mkdtempSync(join(tmpdir(), 'gev camera '));
try {
const input = join(directory, 'saved scene.json');
const output = join(directory, 'camera manifest.json');
writeFileSync(input, JSON.stringify(project()));
const script = fileURLToPath(
new URL('../../tools/export-blender-camera.mjs', import.meta.url),
);
const args = [
script,
'--input',
input,
'--output',
output,
'--origin',
'0,0,0',
'--fps',
'30000/1001',
'--width',
'1920',
'--height',
'1080',
'--horizontal-fov-deg',
'60',
];
const first = spawnSync(process.execPath, args, { encoding: 'utf8' });
assert.equal(first.status, 0, first.stderr);
assert.match(first.stdout, /CAMERA_EXPORT_OK/);
const contents = readFileSync(output, 'utf8');
assert.equal(JSON.parse(contents).timeline.fps.denominator, 1001);
const repeat = spawnSync(process.execPath, args, { encoding: 'utf8' });
assert.equal(repeat.status, 1);
assert.equal(readFileSync(output, 'utf8'), contents);
const invalidOutput = join(directory, 'invalid.json');
const invalid = [...args];
invalid[invalid.indexOf(output)] = invalidOutput;
invalid[invalid.length - 1] = '180';
assert.equal(
spawnSync(process.execPath, invalid, { encoding: 'utf8' }).status,
1,
);
assert.equal(existsSync(invalidOutput), false);
} finally {
rmSync(directory, { recursive: true, force: true });
}
});
test('ordinary submillisecond shots retain the Director seek duration floor', () => {
const p = project([
{ id: 'start', camera: pose(), durationSec: 0.033, holdSec: 0 },
{
id: 'short',
camera: pose({ heading: 90 }),
durationSec: 0.0005,
holdSec: 0.1,
},
]);
const m = adapter.exportCameraManifest(p, options());
const sample = m.frames[1];
const state = sceneSeekState(
p.scenes[0],
sample.timeSec / m.timeline.durationSec,
(_scene, shot) => shot.durationSec + shot.holdSec,
(_scene, shot) => shot.holdSec,
);
const expected = adapter.cameraPoseToEnu(state.camera, origin);
sample.basisColumns.forEach((axis, i) =>
near(axis, expected.basisColumns[i]),
);
});
test('long decimal timelines do not gain a rounding-only frame', () => {
const p = project(
Array.from({ length: 10000 }, (_, i) => ({
id: `shot-${i}`,
durationSec: 0.1,
holdSec: 0,
camera: pose(),
})),
);
const m = adapter.exportCameraManifest(p, options());
assert.equal(m.timeline.durationSec, 1000);
assert.equal(m.frames.length, 30000);
assert.equal(m.shots[9999].frameEndExclusive, 30001);
});