Gltf Usability: traverse hierarchy by node name
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 22h
- Merged PRs (30d)
- 161
Description
## What problem does this solve or what need does it fill?
After loading a Gltf file, i need to traverse the nodes in a hierarchical fashion.
Use Cases:
- find a specific node (for example, by following a path of known gltf node names)
- add components to each child of a specific node (for example, a node identified by its name)
(my use case: I have multiple empties in the Gltf file that define camera position and rotation)
- only add specific nodes from the gltf to the bevy scene, based on name
Problem is: Right now, it seems to be flattened, and the hierarchy is not easily accessible anymore.
If I have overlooked something, please don't hesitate to tell me.
## What solution would you like?
Two changes are necessary for this.
1. Allow acces to the the set of root nodes in the top level Gltf or Scene struct.
2. Allow access to the name of children of a given Gltf node.
Solution A (preferred, because it closely models the gltf specification):
```rust
pub struct GltfNode {
// add this property:
pub name: Option,
pub children: Vec,
pub mesh: Option>,
pub transform: bevy_transform::prelude::Transform,
pub extras: Option,
}
pub struct Gltf {
pub nodes: Vec>,
pub named_nodes: HashMap>,
// add this (or maybe add it to the scene)
pub root_nodes: Vec>,
// ...
}
```
Solution B (matches the current design, but seems cumbersome):
Extend `Gltf::named_nodes` and friends to include a hierarchy instead of only a flattened hashmap.
```rust
pub struct Gltf {
pub nodes: Vec>,
pub named_nodes: HashMap>,
// add this
pub name_hierarchy: NameHierarchy,
// ...
}
pub struct NameHierarchy {
pub name: String,
pub node: Handle,
pub children: Vec
}
```
More solutions possible.
## What alternative(s) have you considered?
There is a very cumbersome workaround. Right now, to find a child with a name, a linear search though the children of a node, combined with a linear search through `named_nodes` hashmap would be required.
Finding the root nodes is also cumbersome, by starting with a hashset and removing all nodes that are children of other nodes.
Edit:
__The Gltf Loader attaches `Name` components, which is a sufficient workaround for me :)__
## Additional context
Technically, all gltf entities can have names, not only nodes. For example, the scenes too.
https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#indices-and-names
Contributor guide
Assessment
This issue has not been assessed yet.