Skip to content

3D engine that AI agents drive

Count removed motion tracks from the set-motion operation in the apply report (#2288)

Nguyen Xuan An40d060e5a15f
diff --git a/.github/workflows/cook-addon.yml b/.github/workflows/cook-addon.yml
index fa99a217..8493d2ae 100644
--- a/.github/workflows/cook-addon.yml
+++ b/.github/workflows/cook-addon.yml
@@ -265,6 +265,7 @@ on:
- 'src/motion-expression.js'
- 'src/motion-properties.js'
- 'src/motion-track-removal.js'
+ - 'src/motion-track-selector.js'
- 'src/motion-validation.js'
- 'src/navmesh.js'
- 'src/near-plane-fit.js'
diff --git a/src/agent-apply-report.js b/src/agent-apply-report.js
index 4bc6dbb1..67b354cf 100644
--- a/src/agent-apply-report.js
+++ b/src/agent-apply-report.js
@@ -2,6 +2,8 @@
// and a compact diff of the node, material and geometry IDs the batch added, removed and changed. Pure recipe data in,
// plain JSON out, so a library user gets the same answer as an MCP client.
+import { motionAfterRemoval } from './motion-track-selector.js';
+
export const APPLY_REPORT_LIMITS = Object.freeze({ maxEntries: 20, maxDiffIds: 8, maxText: 48 });
const COLLECTIONS = Object.freeze({ node: 'nodes', material: 'materials', geometry: 'geometries' });
@@ -66,7 +68,7 @@ function targetOf(operation) {
}
// Applies one operation to the working copy and says what it changed. Fields it cannot judge count as changed.
-function describeOperation(working, operation, maxText, after) {
+function describeOperation(working, operation, maxText) {
const { type } = operation;
const add = /^add-(node|material|geometry)$/.exec(type);
if (add) {
@@ -101,12 +103,10 @@ function describeOperation(working, operation, maxText, after) {
return { changed: shortList(differing, maxText) };
}
if (type === 'set-motion' && Object.hasOwn(operation, 'removeTracks')) {
- // The selectors are resolved by the scene when it applies the batch; the report reads the result from the recipe after it.
- const before = working.motion?.tracks?.length ?? 0;
- const remaining = after.motion;
- if (remaining) working.motion = structuredClone(remaining);
+ const { motion, removed } = motionAfterRemoval(working.motion, operation.removeTracks);
+ if (motion) working.motion = motion;
else delete working.motion;
- return { changed: `removed ${before - (remaining?.tracks?.length ?? 0)} motion tracks` };
+ return { changed: `removed ${removed} motion tracks` };
}
if (Object.hasOwn(OWN_SECTIONS, type)) {
const name = OWN_SECTIONS[type];
@@ -161,7 +161,7 @@ export function describeApplication(before, after, operations, limits = APPLY_RE
const working = structuredClone(before);
for (const collection of Object.values(COLLECTIONS)) working[collection] ??= [];
const entries = operations.map((operation, index) => {
- const outcome = describeOperation(working, operation, limits.maxText, after);
+ const outcome = describeOperation(working, operation, limits.maxText);
const target = targetOf(operation);
return { i: index, op: operation.type, ...(target === undefined ? {} : { target }), ...outcome };
});
diff --git a/src/motion-track-removal.js b/src/motion-track-removal.js
index 4202d1fa..3f5946ac 100644
--- a/src/motion-track-removal.js
+++ b/src/motion-track-removal.js
@@ -1,10 +1,9 @@
// The removeTracks field of a set-motion operation: drop the motion tracks that match selectors, keep everything else.
// A leaf module, so scripts/mcp-server.mjs and the scene backends can share it without pulling in the motion compiler.
import { invalidField } from './field-error.js';
+import { SELECTOR_FIELDS, selectorMatches, motionAfterRemoval } from './motion-track-selector.js';
export const MAXIMUM_TRACK_SELECTORS = 64;
-const SELECTOR_FIELDS = ['kind', 'id', 'property'];
-const NO_MOTION_KEYS = new Set(['duration', 'loop', 'tracks']);
function describeTrack(track) {
return [track.kind, track.id, track.property].filter(part => part !== undefined).join('.');
@@ -30,10 +29,6 @@ function assertSelectors(selectors, path) {
});
}
-function selectorMatches(selector, track) {
- return SELECTOR_FIELDS.every(field => selector[field] === undefined || selector[field] === track[field]);
-}
-
/** The motion a set-motion operation with removeTracks leaves: the tracks its selectors match are gone; null when no motion content is left. */
export function removeMotionTracks(operation, motion, path = 'operation') {
const keys = Object.keys(operation);
@@ -52,8 +47,5 @@ export function motionWithoutTracks(motion, selectors, path = 'operation.removeT
if (unmatched.length > 0) {
throw invalidField(path, unmatched.map(describeSelector).join(', '), `selectors that each match a track the scene has; its tracks are: ${listed}`);
}
- const kept = tracks.filter(track => !selectors.some(selector => selectorMatches(selector, track)));
- const remaining = { ...motion, tracks: kept };
- const hasOtherMotion = Object.keys(remaining).some(key => !NO_MOTION_KEYS.has(key) && remaining[key] !== undefined);
- return kept.length === 0 && !hasOtherMotion ? null : remaining;
+ return motionAfterRemoval(motion, selectors).motion;
}
diff --git a/src/motion-track-selector.js b/src/motion-track-selector.js
new file mode 100644
index 00000000..42fd8306
--- /dev/null
+++ b/src/motion-track-selector.js
@@ -0,0 +1,18 @@
+// What a removeTracks selector matches and what motion a removal leaves. Small and always loaded, so the apply report
+// can count removed tracks itself; the validating code lives in motion-track-removal.js and loads on demand.
+
+export const SELECTOR_FIELDS = ['kind', 'id', 'property'];
+const NO_MOTION_KEYS = new Set(['duration', 'loop', 'tracks']);
+
+export function selectorMatches(selector, track) {
+ return SELECTOR_FIELDS.every(field => selector[field] === undefined || selector[field] === track[field]);
+}
+
+/** The motion without the tracks the selectors match, and how many went; motion is null when no motion content is left. */
+export function motionAfterRemoval(motion, selectors) {
+ const tracks = Array.isArray(motion?.tracks) ? motion.tracks : [];
+ const kept = tracks.filter(track => !selectors.some(selector => selectorMatches(selector, track)));
+ const remaining = { ...motion, tracks: kept };
+ const hasOtherMotion = Object.keys(remaining).some(key => !NO_MOTION_KEYS.has(key) && remaining[key] !== undefined);
+ return { motion: kept.length === 0 && !hasOtherMotion ? null : remaining, removed: tracks.length - kept.length };
+}
diff --git a/tests/agent-apply-report.test.js b/tests/agent-apply-report.test.js
index 932548d3..88096b51 100644
--- a/tests/agent-apply-report.test.js
+++ b/tests/agent-apply-report.test.js
@@ -85,3 +85,14 @@ test('the diff caps a long ID list', () => {
assert.equal(result.diff.nodes.added.length, 9);
assert.equal(result.diff.nodes.added.at(-1), '+4 more');
});
+
+test('removeTracks counts the tracks it removes even when a later set-motion replaces the motion', () => {
+ const track = (kind, property) => ({ kind, property, ...(kind === 'node' ? { id: 'cube' } : {}), keyframes: [{ time: 0, value: 0 }, { time: 1, value: 1 }] });
+ const before = { ...recipe(), motion: { duration: 1, tracks: [track('camera', 'fov'), track('node', 'translation'), track('node', 'rotation')] } };
+ const replacement = { duration: 1, tracks: [track('node', 'scale'), track('camera', 'fov')] };
+ const after = { ...recipe(), motion: replacement };
+ const operations = [{ type: 'set-motion', removeTracks: [{ kind: 'node', id: 'cube' }] }, { type: 'set-motion', motion: replacement }];
+ const { results } = describeApplication(before, after, operations);
+ assert.equal(results[0].changed, 'removed 2 motion tracks');
+ assert.equal(results[1].changed, 'set motion');
+});