Class Scene
Package: @aspose/3d (v24.12.0)
Scene is the root container for a 3D scene graph in @aspose/3d. It holds the node hierarchy, metadata, and animation clips, and provides the primary I/O interface for loading and saving 3D files.
export class Scene extends SceneObjectInheritance
A3DObject ← SceneObject ← Scene
Examples
Load an OBJ file and print the number of child nodes in the root of the scene graph.
import { Scene } from '@aspose/3d';
const scene = new Scene();
scene.open('model.obj');
function countNodes(node: any): number {
let total = 1;
for (const child of node.childNodes) {
total += countNodes(child);
}
return total;
}
const nodeCount = countNodes(scene.rootNode);
console.log(`Total nodes in scene: ${nodeCount}`);Properties
| Property | Type | Description |
|---|---|---|
rootNode | Node | The root node of the scene graph. All geometry, lights, cameras, and other objects are attached beneath this node. |
assetInfo | AssetInfo | Metadata about the asset, including creator, creation time, unit information, and coordinate system. |
animationClips | AnimationClip[] | The collection of animation clips defined in the scene. Each clip contains animation curves for nodes and their properties. |
subScenes | Scene[] | Sub-scenes embedded within this scene. Used by formats such as FBX that support scene hierarchies. |
Methods
open(fileOrStream, options?)
Loads a 3D file from a file path or a Buffer into the scene, replacing any existing content.
open(fileOrStream: string | Buffer, options?: LoadOptions): voidParameters
fileOrStream string | Buffer
The path to the source file, or a Buffer containing the raw file data.
options LoadOptions (optional)
Format-specific load options. Pass undefined to use defaults.
Returns
void
Examples
import { Scene } from '@aspose/3d';
const scene = new Scene();
scene.open('input.fbx');
console.log(`Loaded scene with root node: ${scene.rootNode.name}`);openFromBuffer(buffer, options?)
Loads a 3D file from an in-memory Buffer. This is the preferred overload when the file data has already been read into memory.
openFromBuffer(buffer: Buffer, options?: LoadOptions): voidParameters
buffer Buffer
A Node.js Buffer containing the complete file content.
options LoadOptions (optional)
Format-specific load options.
Returns
void
Examples
import { Scene } from '@aspose/3d';
import { readFileSync } from 'fs';
const data = readFileSync('model.glb');
const scene = new Scene();
scene.openFromBuffer(data);
console.log(`Animation clips: ${scene.animationClips.length}`);save(fileOrStream, formatOrOptions?, options?)
Saves the scene to a file. The format is inferred from the file extension, or you can pass a format-specific SaveOptions instance (or a FileFormat singleton) as the second argument.
save(fileOrStream: string, formatOrOptions?: FileFormat | SaveOptions, options?: SaveOptions): voidParameters
fileOrStream string
The destination file path. The extension determines the output format (e.g., .glb → GLB, .gltf → glTF JSON, .stl → STL).
formatOrOptions FileFormat | SaveOptions (optional)
Either a format singleton (e.g., GltfFormat.getInstance()) or a format-specific SaveOptions subclass.
options SaveOptions (optional)
Format-specific save options when a FileFormat is passed as the second argument.
Returns
void
Examples
import { Scene } from '@aspose/3d';
const scene = new Scene();
scene.open('input.obj');
// Format inferred from extension
scene.save('output.glb');
console.log('Scene saved as GLB.');createAnimationClip(name)
Creates a new named animation clip and adds it to the scene’s animationClips collection.
createAnimationClip(name: string): AnimationClipParameters
name string
A descriptive name for the new animation clip.
Returns
AnimationClip
The newly created AnimationClip instance.
Examples
import { Scene } from '@aspose/3d';
const scene = new Scene();
const clip = scene.createAnimationClip('WalkCycle');
console.log(`Created clip: ${clip.name}`);
console.log(`Total clips: ${scene.animationClips.length}`);