Skip to main content

nsi_trait/
node.rs

1//! Standard ɴsɪ node types and constants.
2
3/// Wildcard node that references all existing nodes at once (`.all`).
4pub const ALL: &str = ".all";
5
6/// The scene's root (`.root`).
7/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-root).
8pub const ROOT: &str = ".root";
9
10/// Global settings node (`.global`).
11/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-global-node).
12pub const GLOBAL: &str = ".global";
13
14/// Expresses relationships of groups of nodes.
15/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-set).
16pub const SET: &str = "set";
17
18/// [OSL](http://opensource.imageworks.com/osl.html) shader or layer in a shader group.
19/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-shader).
20pub const SHADER: &str = "shader";
21
22/// Container for generic attributes (e.g. visibility).
23/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-attributes).
24pub const ATTRIBUTES: &str = "attributes";
25
26/// Transformation to place objects in the scene.
27/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-transform).
28pub const TRANSFORM: &str = "transform";
29
30/// Specifies instances of other nodes.
31/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-instances).
32pub const INSTANCES: &str = "instances";
33
34/// An infinite plane.
35/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-plane).
36pub const PLANE: &str = "plane";
37
38/// Polygonal mesh or subdivision surface.
39/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-mesh).
40pub const MESH: &str = "mesh";
41
42/// Assign attributes to part of a mesh, curves or particles.
43/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-faceset).
44pub const FACE_SET: &str = "faceset";
45
46/// Linear, b-spline and Catmull-Rom curves.
47/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-curves).
48pub const CURVES: &str = "curves";
49
50/// Collection of particles.
51/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-particles).
52pub const PARTICLES: &str = "particles";
53
54/// NURBS surface with optional trim curves.
55///
56/// Intrinsic attributes: `nu`, `nv`, `uorder`, `vorder`, `uknot`, `vknot`, and
57/// either `P` (point) or `Pw` (rational, float[4]). Trim curves are specified
58/// via the `trimcurves.*` attribute family.
59pub const NURBS: &str = "nurbs";
60
61/// Geometry to be loaded or generated in delayed fashion.
62/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-procedural).
63pub const PROCEDURAL: &str = "procedural";
64
65/// A volume loaded from an [OpenVDB](https://www.openvdb.org) file.
66/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-volume).
67pub const VOLUME: &str = "volume";
68
69/// Geometry type to define environment lighting.
70/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-environment).
71pub const ENVIRONMENT: &str = "environment";
72
73/// An orthographic camera.
74/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-orthographiccamera-node).
75pub const ORTHOGRAPHIC_CAMERA: &str = "orthographiccamera";
76
77/// A perspective camera.
78/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-perspectivecamera-node).
79pub const PERSPECTIVE_CAMERA: &str = "perspectivecamera";
80
81/// A fisheye camera.
82/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-fisheyecamera-node).
83pub const FISHEYE_CAMERA: &str = "fisheyecamera";
84
85/// A cylindrical camera.
86/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-cylindricalcamera-node).
87pub const CYLINDRICAL_CAMERA: &str = "cylindricalcamera";
88
89/// A spherical camera.
90/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#the-sphericalcamera-node).
91pub const SPHERICAL_CAMERA: &str = "sphericalcamera";
92
93/// A target where to output rendered pixels.
94/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-outputdriver).
95pub const OUTPUT_DRIVER: &str = "outputdriver";
96
97/// Describes one render layer to be connected to an `outputdriver` node.
98/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-outputlayer).
99pub const OUTPUT_LAYER: &str = "outputlayer";
100
101/// Describes how the view from a camera node will be rasterized into an
102/// `outputlayer` node.
103/// [Documentation](https://nsi.readthedocs.io/en/latest/nodes.html#node-screen).
104pub const SCREEN: &str = "screen";
105
106/// Node types in the ɴsɪ scene graph.
107///
108/// Each variant corresponds to a specific node type that can be created
109/// in the scene graph.
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
111#[non_exhaustive]
112pub enum NodeType {
113    /// Wildcard that references all existing nodes at once.
114    All,
115    /// The scene's root node.
116    Root,
117    /// Global settings node.
118    Global,
119    /// Expresses relationships of groups of nodes.
120    Set,
121    /// OSL shader or layer in a shader group.
122    Shader,
123    /// Container for generic attributes (e.g. visibility).
124    Attributes,
125    /// Transformation to place objects in the scene.
126    Transform,
127    /// Specifies instances of other nodes.
128    Instances,
129    /// An infinite plane.
130    Plane,
131    /// Polygonal mesh or subdivision surface.
132    Mesh,
133    /// Assign attributes to part of a mesh, curves or particles.
134    FaceSet,
135    /// Linear, b-spline and Catmull-Rom curves.
136    Curves,
137    /// Collection of particles.
138    Particles,
139    /// NURBS surface with optional trim curves.
140    Nurbs,
141    /// Geometry to be loaded or generated in delayed fashion.
142    Procedural,
143    /// A volume loaded from an OpenVDB file.
144    Volume,
145    /// Geometry type to define environment lighting.
146    Environment,
147    /// An orthographic camera.
148    OrthographicCamera,
149    /// A perspective camera.
150    PerspectiveCamera,
151    /// A fisheye camera.
152    FisheyeCamera,
153    /// A cylindrical camera.
154    CylindricalCamera,
155    /// A spherical camera.
156    SphericalCamera,
157    /// A target where to output rendered pixels.
158    OutputDriver,
159    /// Describes one render layer to be connected to an output driver.
160    OutputLayer,
161    /// Describes how the view from a camera will be rasterized.
162    Screen,
163}
164
165impl NodeType {
166    /// Returns the string identifier used by the C API.
167    #[inline]
168    pub const fn as_str(&self) -> &'static str {
169        match self {
170            Self::All => ALL,
171            Self::Root => ROOT,
172            Self::Global => GLOBAL,
173            Self::Set => SET,
174            Self::Shader => SHADER,
175            Self::Attributes => ATTRIBUTES,
176            Self::Transform => TRANSFORM,
177            Self::Instances => INSTANCES,
178            Self::Plane => PLANE,
179            Self::Mesh => MESH,
180            Self::FaceSet => FACE_SET,
181            Self::Curves => CURVES,
182            Self::Particles => PARTICLES,
183            Self::Nurbs => NURBS,
184            Self::Procedural => PROCEDURAL,
185            Self::Volume => VOLUME,
186            Self::Environment => ENVIRONMENT,
187            Self::OrthographicCamera => ORTHOGRAPHIC_CAMERA,
188            Self::PerspectiveCamera => PERSPECTIVE_CAMERA,
189            Self::FisheyeCamera => FISHEYE_CAMERA,
190            Self::CylindricalCamera => CYLINDRICAL_CAMERA,
191            Self::SphericalCamera => SPHERICAL_CAMERA,
192            Self::OutputDriver => OUTPUT_DRIVER,
193            Self::OutputLayer => OUTPUT_LAYER,
194            Self::Screen => SCREEN,
195        }
196    }
197
198    /// Parse a node type from its string identifier.
199    pub fn from_name(s: &str) -> Option<Self> {
200        Some(match s {
201            ALL => Self::All,
202            ROOT => Self::Root,
203            GLOBAL => Self::Global,
204            SET => Self::Set,
205            SHADER => Self::Shader,
206            ATTRIBUTES => Self::Attributes,
207            TRANSFORM => Self::Transform,
208            INSTANCES => Self::Instances,
209            PLANE => Self::Plane,
210            MESH => Self::Mesh,
211            FACE_SET => Self::FaceSet,
212            CURVES => Self::Curves,
213            PARTICLES => Self::Particles,
214            NURBS => Self::Nurbs,
215            PROCEDURAL => Self::Procedural,
216            VOLUME => Self::Volume,
217            ENVIRONMENT => Self::Environment,
218            ORTHOGRAPHIC_CAMERA => Self::OrthographicCamera,
219            PERSPECTIVE_CAMERA => Self::PerspectiveCamera,
220            FISHEYE_CAMERA => Self::FisheyeCamera,
221            CYLINDRICAL_CAMERA => Self::CylindricalCamera,
222            SPHERICAL_CAMERA => Self::SphericalCamera,
223            OUTPUT_DRIVER => Self::OutputDriver,
224            OUTPUT_LAYER => Self::OutputLayer,
225            SCREEN => Self::Screen,
226            _ => return None,
227        })
228    }
229}
230
231impl std::fmt::Display for NodeType {
232    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233        f.write_str(self.as_str())
234    }
235}
236
237#[cfg(test)]
238mod tests {
239    use super::*;
240
241    #[test]
242    fn node_type_round_trip() {
243        let types = [
244            NodeType::All,
245            NodeType::Root,
246            NodeType::Global,
247            NodeType::Set,
248            NodeType::Shader,
249            NodeType::Attributes,
250            NodeType::Transform,
251            NodeType::Instances,
252            NodeType::Plane,
253            NodeType::Mesh,
254            NodeType::FaceSet,
255            NodeType::Curves,
256            NodeType::Particles,
257            NodeType::Nurbs,
258            NodeType::Procedural,
259            NodeType::Volume,
260            NodeType::Environment,
261            NodeType::OrthographicCamera,
262            NodeType::PerspectiveCamera,
263            NodeType::FisheyeCamera,
264            NodeType::CylindricalCamera,
265            NodeType::SphericalCamera,
266            NodeType::OutputDriver,
267            NodeType::OutputLayer,
268            NodeType::Screen,
269        ];
270
271        for ty in types {
272            let s = ty.as_str();
273            let parsed = NodeType::from_name(s).expect("should parse");
274            assert_eq!(ty, parsed);
275        }
276    }
277}