Skip to main content

openusd_rs/usd/
object.rs

1use crate::{
2	sdf, tf, usd,
3	vt::{self, ValueType},
4};
5
6/// Base class for Usd scenegraph objects, providing common API.
7///
8/// The commonality between the three types of scenegraph objects in Usd
9/// ([`usd::Prim`], [`usd::Attribute`], [`usd::Relationship`]) is that they
10/// can all have metadata. Other objects in the API simply are kinds of metadata.
11///
12/// **Inheritance hierarchy:**
13/// - [`usd::Object`]
14///   - [`usd::Prim`]
15///   - [`usd::Property`]
16///     - [`usd::Attribute`]
17///     - [`usd::Relationship`]
18pub struct Object<'a> {
19	stage: &'a usd::Stage,
20	path: sdf::Path,
21}
22
23impl<'a> Object<'a> {
24	pub(crate) fn new(stage: &'a usd::Stage, path: sdf::Path) -> Self {
25		Object { stage, path }
26	}
27
28	pub fn stage(&self) -> &usd::Stage {
29		self.stage
30	}
31
32	pub fn path(&self) -> &sdf::Path {
33		&self.path
34	}
35
36	pub fn metadata<T: ValueType>(&self, key: &tf::Token) -> Option<T> {
37		self.stage()
38			.data()
39			.get(self.path(), key)
40			.map(|v| v.get::<T>())
41			.flatten()
42	}
43
44	/// Return this object's documentation (metadata).
45	///
46	/// This returns the empty string if no documentation has been set.
47	pub fn documentation(&self) -> String {
48		self.metadata(&sdf::FIELD_KEYS.documentation)
49			.unwrap_or_default()
50	}
51
52	/// Return this object's composed customData dictionary.
53	pub fn custom_data(&self) -> vt::Dictionary {
54		self.metadata(&sdf::FIELD_KEYS.custom_data)
55			.unwrap_or_default()
56	}
57}