From 8731da9afad67392073cbad07d41ca918f3e6bd7 Mon Sep 17 00:00:00 2001 From: John Doe Date: Wed, 25 Mar 2026 23:43:25 -0400 Subject: [PATCH] fix: childern <> parent link --- packages/core/src/utils/clone-scene-graph.ts | 24 +++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/clone-scene-graph.ts b/packages/core/src/utils/clone-scene-graph.ts index c8b11c8d..ff2a2f61 100644 --- a/packages/core/src/utils/clone-scene-graph.ts +++ b/packages/core/src/utils/clone-scene-graph.ts @@ -16,6 +16,25 @@ function extractIdPrefix(id: string): string { return underscoreIndex === -1 ? 'node' : id.slice(0, underscoreIndex) } +/** + * Resolves a child reference to a node ID. + * Supports both string IDs and embedded child node objects with an `id` field. + */ +function resolveChildRefId(child: unknown): string | undefined { + if (typeof child === 'string') { + return child + } + + if (child && typeof child === 'object' && 'id' in child) { + const id = (child as { id?: unknown }).id + if (typeof id === 'string') { + return id + } + } + + return undefined +} + /** * Deep clones a scene graph with all node IDs regenerated while preserving * parent-child relationships and other internal references. @@ -52,7 +71,10 @@ export function cloneSceneGraph(sceneGraph: SceneGraph): SceneGraph { // Remap children array (walls, levels, buildings, sites, items can have children) if ('children' in clonedNode && Array.isArray(clonedNode.children)) { ;(clonedNode as Record).children = clonedNode.children - .map((childId) => idMap.get(childId as string)) + .map((childRef) => { + const childId = resolveChildRefId(childRef) + return childId ? idMap.get(childId) : undefined + }) .filter((id): id is string => id !== undefined) as string[] }