How to Load 3D Files with Aspose.3D FOSS
Problem
Loading 3D assets in Python requires parsing geometry and materials from standard file formats. Aspose.3D supports loading OBJ, STL, glTF, FBX, 3MF, and other formats through a single Scene.from_file() call, which populates the scene hierarchy ready for inspection or conversion.
Prerequisites
- Python 3.7 or later installed
- Install the package via pip:
pip install aspose-3d-foss - A supported 3D input file (OBJ, STL, GLB, 3MF, FBX, etc.)
How Loading Works
Scene.from_file() is a class method that accepts a file path and an optional format-specific load options object. It returns a populated Scene instance containing the full node hierarchy, entities, and associated assets from the file.
The scene’s node hierarchy is accessed through the root_node property (not a method call). Child nodes are accessed through the child_nodes property on any node.
Loading a File
The simplest form loads a file with automatic format detection:
from aspose.threed import Scene
scene = Scene.from_file("model.obj")To pass load options (useful for controlling coordinate system handling, scale, or material loading behavior):
from aspose.threed import Scene
from aspose.threed.formats import ObjLoadOptions
options = ObjLoadOptions()
options.enable_materials = True
options.flip_coordinate_system = False
scene = Scene.from_file("model.obj", options)Traversing the Scene Graph
Once loaded, traverse the scene by iterating over root_node.child_nodes. Use child_nodes (not children); child_nodes is the correct property name on Node.
from aspose.threed import Scene
from aspose.threed.entities import Mesh
scene = Scene.from_file("model.obj")
# root_node is a property, not a method call
root = scene.root_node
print(f"Top-level node count: {len(root.child_nodes)}")
# child_nodes is a property, not children
for node in root.child_nodes:
entity = node.entity
if entity is not None:
print(f"Node '{node.name}' contains {type(entity).__name__}")
if isinstance(entity, Mesh):
print(f" Vertices: {len(entity.control_points)}")
print(f" Polygons: {entity.polygon_count}")
else:
print(f"Node '{node.name}' has no entity")Code Example
Complete example: load a model, count mesh nodes, and print a summary.
from aspose.threed import Scene
from aspose.threed.entities import Mesh
scene = Scene.from_file("model.stl")
mesh_count = 0
for node in scene.root_node.child_nodes:
if isinstance(node.entity, Mesh):
mesh_count += 1
print(f"Scene loaded. Mesh nodes: {mesh_count}")Supported Formats
Aspose.3D supports loading the following common 3D file formats.
| Format | Extension | Notes |
|---|---|---|
| Wavefront OBJ | .obj | Geometry and optional MTL materials |
| STL | .stl | ASCII and binary STL |
| glTF 2.0 | .gltf, .glb | JSON and binary variants |
| Microsoft 3MF | .3mf | 3D printing format |
| FBX | .fbx | Basic geometry loading; limited support |