Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spliceNode function to reparent an entire scene or subtree #1536

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more about my recommendation from #1530 (comment) here, and unfortunately it's not as simple as I'd been hoping.

  1. As mentioned, we will get validation warnings if we put a parent above a skinned mesh. Technically it's valid, these are just 'hints' or 'warnings' at the validator level, because the transform won't do anything to the skinned mesh directly, transforms affect only the joints.
  2. However, it's possible that the skinned mesh's joints are descendants of the node. In that case, skipping the skinned mesh will cause the transform to not affect the joints as expected, and thus not to affect the skinned mesh.

Probably (2) is the more critical error, as it will change the rendered result.

We could search the child's subtree for any joints, and skip the skinned mesh only if no joints are found. But this might be a more complex and less predictable operation than users are expecting, calling a method like 'spliceNode'. If we skip that special case, then the method body becomes something more like:

const node = Document.fromGraph(parent.getGraph()).createNode();
for (const child of parent.listChildren()) {
  node.addChild(child);
}
parent.addChild(newNode);

newNode.addChild(child);
}
parent.addChild(newNode);
return newNode;
}