3D engine that AI agents drive
scene_inspect returns a node report for a joint or empty with no mesh below it (Part of #761)
Nguyen Xuan An80fba366a6bf
diff --git a/changes/761.md b/changes/761.mdindex ef1bec2b..ce2199f9 100644--- a/changes/761.md+++ b/changes/761.md@@ -1,2 +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)+- scene_inspect with a node ID lists the parts below it and tags each with `fromNode`; a node with no part below it, such as a skeleton joint, returns its transform (parent, joint flag, local translation, rotation and scale, rest world position) instead of an error (#761)diff --git a/src/agent-asset.js b/src/agent-asset.jsindex 57596c3c..67a392fa 100644--- a/src/agent-asset.js+++ b/src/agent-asset.js@@ -374,25 +374,13 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {let editSignature = meshEditSignature(editedParts);let topologyRevision = 0;- /** A part by its ID (mesh-3), or by the source node that holds exactly that one part; several or none is named in the error. */+ /** A part by its ID (mesh-3); anything else that is not a source node is an unknown ID. */function partRecordFor(id) {const record = records.find(value => value.id === id);- if (record) {- return record;+ if (!record) {+ throw invalidField('geometry', id, 'an imported mesh ID such as "mesh-0", or a source node ID; call scene_inspect without ids to list the meshes, or with kind hierarchy for source nodes');}- const sourceNode = sourceNodes.get(id);- if (!sourceNode) {- throw invalidField('geometry', id, 'an imported mesh ID such as "mesh-0"; call scene_inspect without ids to list them, or with kind hierarchy for source nodes');- }- const partIds = sourceNode.meshIds ?? [];- if (partIds.length === 0) {- 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(', ');- throw invalidField('geometry', id, `one part ID, such as "${partIds[0]}"; this source node holds ${partIds.length} parts: ${listed}${partIds.length > 8 ? ', ...' : ''}`);- }- return records.find(value => value.id === partIds[0]);+ return record;}/** 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. */@@ -403,12 +391,31 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {}const found = collectNodeRecords(id);for (const record of found) {- originNodes.set(record.id, id);+ if (!record.isSourceNodeReport) {+ 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. */+ /** A source node with no part anywhere below it (a joint or an empty): its transform at rest, so an agent can aim at it or pose it. */+ function sourceNodeReport(sourceNode) {+ const world = mat4.create();+ const chain = [];+ for (let current = sourceNode; current; current = current.parent ? sourceNodes.get(current.parent) : null) {+ chain.unshift(current);+ }+ for (const link of chain) {+ mat4.multiply(world, world, mat4.fromRotationTranslationScale(mat4.create(), link.rotation, link.translation, link.scale));+ }+ return {+ isSourceNodeReport: true, id: sourceNode.id, label: sourceNode.label, name: sourceNode.name, parent: sourceNode.parent,+ joint: Boolean(sourceNode.joint), translation: [...sourceNode.translation], rotation: [...sourceNode.rotation], scale: [...sourceNode.scale],+ restWorldPosition: [world[12], world[13], world[14]], meshIds: [],+ };+ }++ /** Every part of a source node and of the nodes below it; with no part anywhere below it, the node's own report. */function collectNodeRecords(id) {const partIds = [];const pending = [id];@@ -418,7 +425,7 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {pending.push(...[...sourceNodes.values()].filter(node => node.parent === nodeId).map(node => node.id));}if (partIds.length === 0) {- return [partRecordFor(id)];+ return [sourceNodeReport(sourceNodes.get(id))];}return records.filter(value => partIds.includes(value.id));}@@ -1063,16 +1070,20 @@ export function createAgentAssetScene(asset, inputRecipe, { sources } = {}) {...(options.includeBounds ? { note: 'Hierarchy lists no bounds; inspect without kind, with ids of nodes or meshes, to get them.' } : {}),motion: recipe.motion ? { duration: recipe.motion.duration, loop: recipe.motion.loop, tracks: recipe.motion.tracks.length, time: motionTime } : null };}- 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"]`);+ if (options.ids !== undefined && (!Array.isArray(options.ids) || options.ids.length > Math.max(records.length, sourceNodes.size))) {+ throw invalidField('ids', Array.isArray(options.ids) ? `${options.ids.length} IDs` : options.ids, `an array of at most ${Math.max(records.length, sourceNodes.size)} mesh or node IDs, such as ["mesh-0"]`);}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) {+ if (options.includeBounds && visible.filter(record => !record.isSourceNodeReport).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"]');}const nodes = visible.map(record => {+ if (record.isSourceNodeReport) {+ const { isSourceNodeReport, ...report } = record;+ return report;+ }const index = Number(record.id.slice(5));const node = {id: record.id, label: record.label, geometry: record.geometry.id, material: record.material.id,diff --git a/tests/agent-asset-mesh.test.js b/tests/agent-asset-mesh.test.jsindex ee0110f4..7eb2c138 100644--- a/tests/agent-asset-mesh.test.js+++ b/tests/agent-asset-mesh.test.js@@ -272,6 +272,17 @@ test('inspect accepts a source-node ID and lists its parts and those of the nodeassert.ok(several.meshIds.every(id => bySeveral.includes(id)));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"/);+ const [report] = scene.inspect({ ids: [empty.id, single.id], includeBounds: false }).nodes;+ assert.equal(report.id, empty.id);+ assert.equal(report.parent, empty.parent);+ assert.equal(report.joint, empty.joint);+ assert.deepEqual(report.translation, empty.translation);+ assert.equal(report.restWorldPosition.length, 3);+ assert.ok(report.restWorldPosition.every(Number.isFinite));+ assert.equal(report.isSourceNodeReport, undefined);+ const holdsMeshBelow = node => node.meshIds.length > 0 || nodes.some(other => other.parent === node.id && holdsMeshBelow(other));+ const joint = nodes.find(node => node.joint && !holdsMeshBelow(node));+ assert.ok(joint, 'the rigged robot has a joint with no mesh below it');+ assert.equal(scene.inspect({ ids: [joint.id] }).nodes[0].joint, true);assert.throws(() => scene.inspect({ ids: ['nothing-here'] }), /^RangeError: geometry: got "nothing-here"; expected an imported mesh ID such as "mesh-0"/);});