Working with the Scene Graph
Working with the Scene Graph
Every 3D scene in Aspose.3D for Java is organized as a tree of Node objects. Scene provides a single root — getRootNode() — and every piece of geometry, material, and transform lives under that root as a child or descendant node.
Creating the Scene and Accessing the Root Node
Scene initializes automatically with a root node named "RootNode":
import com.aspose.threed.Scene;
import com.aspose.threed.Node;
Scene scene = new Scene();
Node root = scene.getRootNode(); // always "RootNode"Adding Child Nodes
Call createChildNode() on any node to add a child. The method has three commonly used overloads:
import com.aspose.threed.*;
Scene scene = new Scene();
Node root = scene.getRootNode();
// 1. Named node with no entity — useful as a pivot or group container
Node pivot = root.createChildNode("pivot");
// 2. Named node with an entity
Mesh mesh = new Mesh("box");
mesh.getControlPoints().add(new Vector4(0, 0, 0));
mesh.getControlPoints().add(new Vector4(1, 0, 0));
mesh.getControlPoints().add(new Vector4(1, 1, 0));
mesh.getControlPoints().add(new Vector4(0, 1, 0));
mesh.createPolygon(0, 1, 2, 3);
Node meshNode = root.createChildNode("box", mesh);
// 3. Named node with entity and material
PbrMaterial mat = new PbrMaterial("red");
mat.setAlbedo(new Vector4(0.8f, 0.2f, 0.2f, 1.0f));
Node decorated = pivot.createChildNode("red_box", mesh, mat);To attach a node that was constructed separately, use addChildNode():
Node detached = new Node("standalone");
root.addChildNode(detached);Querying Child Nodes
Find a direct child by name or by index, or iterate all direct children:
// By name — returns null if no direct child has that name
Node found = root.getChild("box");
if (found != null) {
System.out.println("Found: " + found.getName());
}
// By index
Node first = root.getChild(0);
// Iterate all direct children
for (Node child : root.getChildNodes()) {
System.out.println(child.getName());
}
getChild(String name)searches only direct children, not the full subtree. Useaccept()to search the entire tree.
Traversing the Full Tree
node.accept(NodeVisitor) walks a node and all its descendants in depth-first order. The visitor returns true to continue or false to stop early:
import com.aspose.threed.NodeVisitor;
// Print every node name in the scene
scene.getRootNode().accept(n -> {
System.out.println(n.getName());
return true; // false would stop traversal
});
// Stop after finding the first node that has an entity
final Node[] found = {null};
scene.getRootNode().accept(n -> {
if (n.getEntity() != null) {
found[0] = n;
return false; // stop walking
}
return true;
});NodeVisitor is a single-method interface, so it accepts a lambda in Java 8+.
Controlling Visibility and Export Exclusion
Nodes can be hidden or excluded from export without being removed from the hierarchy:
Node ground = root.createChildNode("ground_plane", mesh);
ground.setVisible(false); // hidden in viewport / renderer
Node helperNode = root.createChildNode("debug_arrow", mesh);
helperNode.setExcluded(true); // omitted from all export operationssetVisible(false) is a display hint. setExcluded(true) prevents the node from appearing in exported files regardless of format.
Attaching Multiple Entities to One Node
A node has a primary entity (getEntity() / setEntity()), but can hold additional entities through addEntity(). This is useful when different mesh pieces share a single transform:
Mesh body = new Mesh("body");
Mesh wheel = new Mesh("wheel");
Node carNode = root.createChildNode("car");
carNode.addEntity(body);
carNode.addEntity(wheel);
// Retrieve all entities on this node
for (Entity ent : carNode.getEntities()) {
System.out.println(ent.getName());
}Merging Nodes
merge() moves all children, entities, and materials from a source node into the target node. The source node is left empty:
Node lod0 = root.createChildNode("lod0");
lod0.createChildNode("mesh_high", mesh);
Node lod1 = root.createChildNode("lod1");
lod1.createChildNode("mesh_low", mesh);
// Consolidate lod0 children into lod1
lod1.merge(lod0);
// lod1 now has both mesh_high and mesh_low; lod0 is emptyNext Steps
- Applying Transforms — position, rotate, and scale any node using its
Transform - Creating and Working with Meshes — build polygon geometry and attach it to nodes
API Quick Reference
| Member | Description |
|---|---|
scene.getRootNode() | Root of the scene tree; always present after new Scene() |
node.createChildNode(name) | Create a named child node with no entity |
node.createChildNode(name, entity) | Create a named child node with an entity |
node.createChildNode(name, entity, material) | Create a named child node with entity and material |
node.addChildNode(node) | Attach a separately constructed Node |
node.getChild(name) | Find a direct child by name; returns null if not found |
node.getChild(index) | Get the direct child at a given index |
node.getChildNodes() | List<Node> of all direct children |
node.accept(visitor) | Walk this node and all descendants depth-first |
node.addEntity(entity) | Attach an additional entity to the node |
node.getEntities() | List<Entity> of all entities on this node |
node.setVisible(bool) | Show or hide the node |
node.setExcluded(bool) | Include or exclude the node from export |
node.merge(other) | Move all children and entities from other into this node |