Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Tag inspected parts with the source node they came from (Part of #761)

Nguyen Xuan An448a2557d286
diff --git a/changes/761.md b/changes/761.md
index 1ac2b401..ef1bec2b 100644
--- a/changes/761.md
+++ b/changes/761.md
@@ -1 +1,2 @@
- scene_inspect and scene_capabilities take the inputs Haiku sent in the bench: short mesh edit branch names (meshEdit.inset), dotted fields (environment.spotLights), near topic names (uvs, scene.terrain), source nodes without a mesh of their own, includeBounds with kind hierarchy; an unwalkable path start names the nearest walkable point (#761)
+- scene_inspect with a node ID that is a group lists the parts below it and tags each with `fromNode`; a node with no part anywhere below it keeps the error (#761)
diff --git a/src/agent-asset.js b/src/agent-asset.js
index 7b8e6a3f..57596c3c 100644
--- a/src/agent-asset.js
+++ b/src/agent-asset.js
@@ -386,7 +386,7 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {
}
const partIds = sourceNode.meshIds ?? [];
if (partIds.length === 0) {
- throw invalidField('geometry', id, 'a source node with a mesh of its own, or a mesh ID such as "mesh-0"; call scene_inspect with kind hierarchy to see which nodes hold parts');
+ throw invalidField('geometry', id, 'a source node with a mesh of its own or below it, or a mesh ID such as "mesh-0"; call scene_inspect with kind hierarchy to see which nodes hold parts');
}
if (partIds.length > 1) {
const listed = partIds.slice(0, 8).join(', ');
@@ -396,11 +396,20 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {
}
/** The parts scene_inspect lists for one ID: a mesh ID is that part; a source node is every part of it and of the nodes below it. */
- function inspectedRecordsFor(id) {
+ function inspectedRecordsFor(id, originNodes = new Map()) {
const sourceNode = sourceNodes.get(id);
if (!sourceNode || records.some(value => value.id === id)) {
return [partRecordFor(id)];
}
+ const found = collectNodeRecords(id);
+ for (const record of found) {
+ originNodes.set(record.id, id);
+ }
+ return found;
+ }
+
+ /** Every part of a source node and of the nodes below it; a node with no part anywhere below it throws the partRecordFor error. */
+ function collectNodeRecords(id) {
const partIds = [];
const pending = [id];
while (pending.length > 0) {
@@ -1057,7 +1066,8 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {
if (options.ids !== undefined && (!Array.isArray(options.ids) || options.ids.length > records.length)) {
throw invalidField('ids', Array.isArray(options.ids) ? `${options.ids.length} IDs` : options.ids, `an array of at most ${records.length} mesh IDs, such as ["mesh-0"]`);
}
- const selected = options.ids === undefined ? records : options.ids.flatMap(id => inspectedRecordsFor(id));
+ const originNodes = new Map();
+ const selected = options.ids === undefined ? records : options.ids.flatMap(id => inspectedRecordsFor(id, originNodes));
const visible = selected.slice(offset, offset + limit);
if (options.includeBounds && visible.reduce((total, record) => total + geometryOf(record).positions.length / 3, 0) > MAX_INSPECTION_VERTICES) {
throw invalidField('ids', `${visible.length} meshes`, 'fewer meshes, under two million authored base vertices in all, such as ["mesh-0"]');
@@ -1070,6 +1080,7 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {
triangles: geometryOf(record).indices.length / 3,
skinned: Boolean(record.source.skin), morphTargets: geometryOf(record).morphTargets?.length ?? 0,
...(editedParts.has(record.id) ? { edited: true } : {}),
+ ...(originNodes.has(record.id) ? { fromNode: originNodes.get(record.id) } : {}),
};
if (options.includeBounds) {
node.bounds = baseBounds([geometryOf(record)], [matrices[index]]);
diff --git a/tests/agent-asset-mesh.test.js b/tests/agent-asset-mesh.test.js
index 402a8271..ee0110f4 100644
--- a/tests/agent-asset-mesh.test.js
+++ b/tests/agent-asset-mesh.test.js
@@ -267,8 +267,11 @@ test('inspect accepts a source-node ID and lists its parts and those of the node
assert.ok(single && several && empty, 'the robot has leaf, group and childless mesh-less nodes');
const byNode = scene.inspect({ ids: [single.id] }).nodes;
assert.deepEqual(byNode.map(node => node.id), single.meshIds);
+ assert.ok(byNode.every(node => node.fromNode === single.id), 'each part says which node it came from');
const bySeveral = scene.inspect({ ids: [several.id] }).nodes.map(node => node.id);
assert.ok(several.meshIds.every(id => bySeveral.includes(id)));
- assert.throws(() => scene.inspect({ ids: [empty.id] }), /^RangeError: geometry: got .*; expected a source node with a mesh of its own, or a mesh ID such as "mesh-0"/);
+ assert.ok(scene.inspect({ ids: [several.id] }).nodes.every(node => node.fromNode === several.id));
+ assert.equal(scene.inspect({ ids: [single.meshIds[0]] }).nodes[0].fromNode, undefined);
+ assert.throws(() => scene.inspect({ ids: [empty.id] }), /^RangeError: geometry: got .*; expected a source node with a mesh of its own or below it, or a mesh ID such as "mesh-0"/);
assert.throws(() => scene.inspect({ ids: ['nothing-here'] }), /^RangeError: geometry: got "nothing-here"; expected an imported mesh ID such as "mesh-0"/);
});