diff --git a/packages/scene-composer/src/components/toolbars/floatingToolbar/items/AddObjectMenu.tsx b/packages/scene-composer/src/components/toolbars/floatingToolbar/items/AddObjectMenu.tsx index 9241a2815..438b7d306 100644 --- a/packages/scene-composer/src/components/toolbars/floatingToolbar/items/AddObjectMenu.tsx +++ b/packages/scene-composer/src/components/toolbars/floatingToolbar/items/AddObjectMenu.tsx @@ -209,13 +209,20 @@ export const AddObjectMenu = () => { parentRef: getRefForParenting(), } as unknown as ISceneNodeInternal; - const parent = getObject3DBySceneNodeRef(node.parentRef); + let physicalParentNode: ISceneNodeInternal | undefined = getSceneNodeByRef(node.parentRef); + while (physicalParentNode && findComponentByType(physicalParentNode, KnownComponentType.SubModelRef)) { + physicalParentNode = getSceneNodeByRef(physicalParentNode.parentRef); + } + + const hierarchicalParentRef = node.parentRef; + const parent = physicalParentNode ? getObject3DBySceneNodeRef(physicalParentNode.ref) : undefined; const newNode = createNodeWithTransform( node, mainCameraObject.position, mainCameraObject.rotation, mainCameraObject.scale, parent, + hierarchicalParentRef, ); appendSceneNode(newNode); diff --git a/packages/scene-composer/src/utils/__snapshots__/nodeUtils.spec.ts.snap b/packages/scene-composer/src/utils/__snapshots__/nodeUtils.spec.ts.snap index 434c38ede..deb733964 100644 --- a/packages/scene-composer/src/utils/__snapshots__/nodeUtils.spec.ts.snap +++ b/packages/scene-composer/src/utils/__snapshots__/nodeUtils.spec.ts.snap @@ -7,6 +7,7 @@ Object { "type": "Camera", }, ], + "parentRef": "parent-ref", "ref": "test-ref", "transform": Object { "position": Array [ @@ -35,6 +36,7 @@ Object { "type": "Camera", }, ], + "parentRef": undefined, "ref": "test-ref", "transform": Object { "position": Array [ diff --git a/packages/scene-composer/src/utils/nodeUtils.ts b/packages/scene-composer/src/utils/nodeUtils.ts index 4db361aaf..7388687a5 100644 --- a/packages/scene-composer/src/utils/nodeUtils.ts +++ b/packages/scene-composer/src/utils/nodeUtils.ts @@ -124,12 +124,14 @@ export const createNodeWithTransform = ( rotation: THREE.Euler, scale: THREE.Vector3, parent?: THREE.Object3D, + targetRef?: string, ): ISceneNodeInternal => { const finalTransform = getFinalTransform({ position, rotation, scale }, parent); const node = (newWidget as AddingWidgetInfo).node ? (newWidget as AddingWidgetInfo).node : newWidget; return { ...node, + parentRef: targetRef || parent?.userData.nodeRef, transform: { position: finalTransform.position.toArray(), rotation: finalTransform.rotation.toVector3().toArray(),