pub struct Scene { /* private fields */ }Implementations§
Source§impl Scene
impl Scene
pub fn new() -> Scene
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn node(&self, id: NodeId) -> Result<&Node, SceneError>
pub fn nodes(&self) -> impl Iterator<Item = (NodeId, &Node)>
Sourcepub fn resolve_path(
&self,
parent: Option<NodeId>,
components: &[&str],
) -> Result<NodeId, ScenePathError>
pub fn resolve_path( &self, parent: Option<NodeId>, components: &[&str], ) -> Result<NodeId, ScenePathError>
Resolves literal name components one hierarchy level at a time.
None starts among scene roots; Some(parent) starts among that node’s
direct children. An empty relative path selects its live parent; an empty
absolute path returns ScenePathError::EmptyPath.
Names are case-sensitive UTF-8, with no normalization, splitting, escaping,
or special ./.. semantics. For example, ["a/b", ""] selects the
empty-named child of root a/b. This is the scene hierarchy, not the
authoring system’s expanded identifier namespace.
Duplicate roots/siblings are allowed by mutations but always produce
ScenePathError::Ambiguous at the first ambiguous component. A suffix
never disambiguates a prefix. Missing names return NotFound, including
the component index and its resolved parent. The supplied parent is
validated first, followed by the entire path’s bounds (100,000 components,
4,096 bytes per name), before any names are resolved.
No allocation or recursion. Uses existing child links and Self::nodes
for roots; work is bounded by scene slots/visited sibling groups and input
names. The result is a current NodeId, not a persistent path binding:
removing it and recreating its names can make the same path select a new
identity. Stale parent handles are rejected even for empty paths.
use ferravex_play::scene::{Scene, ScenePathError, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("hand/socket", Transform::default(), Some(actor))?;
assert_eq!(scene.resolve_path(None, &["actor", "hand/socket"])?, socket);
assert_eq!(scene.resolve_path(Some(actor), &["hand/socket"])?, socket);
scene.remove_subtree(actor)?;
assert_eq!(scene.resolve_path(Some(actor), &[]),
Err(ScenePathError::InvalidNode(actor)));Sourcepub fn path_from(
&self,
parent: Option<NodeId>,
id: NodeId,
) -> Result<Vec<&str>, ScenePathError>
pub fn path_from( &self, parent: Option<NodeId>, id: NodeId, ) -> Result<Vec<&str>, ScenePathError>
Inspects id as an unambiguous path from parent, excluding the parent’s
own name. None includes the root’s name; Some(id) returns an empty path.
The parent is checked first, then id. Both must be live, and id must
lie in the parent’s subtree, or ScenePathError::NotDescendant is returned.
Walks existing parent links iteratively, then uses Self::resolve_path
to check the same duplicate-name policy. On success, resolving the returned
components from the same parent returns exactly id. Duplicate ancestors
above or equal to an explicit parent do not affect relative paths.
Allocates only a vector of borrowed names, bounded by scene depth (at most
100,000 entries); names are not copied. The borrow prevents scene edits
while the path is in use. Owned copies of paths can change meaning after
edits; keep the NodeId to follow the same live node across reparenting.
Handles are scene-local and must not be reused after decoding a new scene.
use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("socket", Transform::default(), Some(actor))?;
let path = scene.path_from(None, socket)?;
assert_eq!(path, ["actor", "socket"]);
assert_eq!(scene.resolve_path(None, &path)?, socket);
assert_eq!(scene.path_from(Some(actor), socket)?, ["socket"]);
scene.set_parents(&[(actor, Some(socket)), (socket, None)])?;
assert_eq!(scene.path_from(None, actor)?, ["socket", "actor"]);Sourcepub fn subtree(
&self,
root: NodeId,
) -> Result<impl Iterator<Item = (NodeId, &Node)>, SceneError>
pub fn subtree( &self, root: NodeId, ) -> Result<impl Iterator<Item = (NodeId, &Node)>, SceneError>
Visits a root and its descendants in depth-first, parent-before-child order, respecting each node’s child order. A stale root is rejected before traversal starts. The iterator borrows the scene, preventing edits while it is in use, and uses a heap stack rather than recursion.
Collect IDs first when applying edits to the visited nodes:
use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let actor = scene.create("actor", Transform::default(), None)?;
let socket = scene.create("socket", Transform::default(), Some(actor))?;
let ids: Vec<_> = scene.subtree(actor)?.map(|(id, _)| id).collect();
assert_eq!(ids, [actor, socket]);
for id in ids {
scene.set_local(id, Transform::default())?;
}pub fn create( &mut self, name: impl Into<String>, local: Transform, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>
pub fn set_local( &mut self, id: NodeId, local: Transform, ) -> Result<(), SceneError>
Sourcepub fn set_parent(
&mut self,
id: NodeId,
parent: Option<NodeId>,
) -> Result<(), SceneError>
pub fn set_parent( &mut self, id: NodeId, parent: Option<NodeId>, ) -> Result<(), SceneError>
Reparent while retaining local TRS. Cycles and invalid handles are rejected before any links change. World-space position may change.
Sourcepub fn set_parents(
&mut self,
changes: &[(NodeId, Option<NodeId>)],
) -> Result<(), SceneError>
pub fn set_parents( &mut self, changes: &[(NodeId, Option<NodeId>)], ) -> Result<(), SceneError>
Applies reparenting edits atomically, retaining every local transform. Validates the final hierarchy, so edits can succeed even when applying them individually in the given order would temporarily create a cycle. Any stale handle or final cycle leaves the entire scene unchanged.
Repeated node IDs use the last requested parent; all supplied handles are checked, including overwritten requests. Moved children are appended in order of their first appearance. Unchanged links retain their order. Work and scratch space are linear in scene slots plus requested edits; traversal does not recurse, even for deeply nested hierarchies.
use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let carrier = scene.create("carrier", Transform::default(), None)?;
let payload = scene.create("payload", Transform::default(), Some(carrier))?;
// Make payload the root and attach carrier beneath it in one edit.
scene.set_parents(&[(carrier, Some(payload)), (payload, None)])?;
assert_eq!(scene.node(carrier)?.parent(), Some(payload));
assert_eq!(scene.node(payload)?.parent(), None);pub fn world_matrix(&self, id: NodeId) -> Result<[f32; 16], SceneError>
pub fn remove_subtree(&mut self, root: NodeId) -> Result<usize, SceneError>
Sourcepub fn prefab(&self, root: NodeId) -> Result<Prefab, SceneError>
pub fn prefab(&self, root: NodeId) -> Result<Prefab, SceneError>
Captures local transforms and hierarchy; external parent links are omitted.
Sourcepub fn instantiate(
&mut self,
prefab: &Prefab,
parent: Option<NodeId>,
) -> Result<NodeId, SceneError>
pub fn instantiate( &mut self, prefab: &Prefab, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>
Inserts an independent copy under parent, retaining prefab-local TRS.
All entries, links and capacity are checked before insertion; any returned
error leaves existing nodes, child order and handle allocation unchanged.
use ferravex_play::scene::{Scene, Transform};
let mut scene = Scene::new();
let source = scene.create("enemy", Transform::default(), None)?;
let prefab = scene.prefab(source)?;
let copy = scene.instantiate(&prefab, None)?;
assert_ne!(source, copy);
assert_eq!(scene.node(copy)?.name(), "enemy");pub fn clone_subtree( &mut self, root: NodeId, parent: Option<NodeId>, ) -> Result<NodeId, SceneError>
Sourcepub fn encode(&self) -> Result<Vec<u8>, SceneError>
pub fn encode(&self) -> Result<Vec<u8>, SceneError>
GSC1 format: magic, u16 version, u32 count; each node has u32 parent (MAX=root), u16 UTF-8 name length, name bytes, and 10 little-endian f32 TRS values. Handles are remapped; serialized data never trusts slot generations.