Skip to content

Commit

Permalink
Add spliceNode function to reparent an entire scene or subtree
Browse files Browse the repository at this point in the history
  • Loading branch information
rotu committed Oct 18, 2024
1 parent 1693ffd commit fd30214
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/functions/src/splice-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Scene, Node } from '@gltf-transform/core';

/**
* Create a new node as child of the target {@link Scene} or {@link Node}.
* Its existing children will be reparented under the new node.
*
* Example:
*
* ```javascript
* import { transformScene } from '@gltf-transform/functions';
*
* let scene = document.getRoot().getDefaultScene()
* // rotate the entire scene a quarter turn around the y axis
* spliceNode(scene).setRotation([0, Math.SQRT1_2, 0, Math.SQRT1_2]);
* ```
*
* @param parent Node or Scene whose children to reroot
* @returns The new parent node
*/
export function spliceNode(parent: Scene | Node): Node {
const newNode = new Node(parent.getGraph());
for (const child of parent.listChildren()) {
if (child.getSkin()) continue;
newNode.addChild(child);
}
parent.addChild(newNode);
return newNode;
}

0 comments on commit fd30214

Please sign in to comment.