Introduction
Aspose.3D FOSS for Java supports four 3D file formats: OBJ, STL, glTF (including GLB binary variant), and FBX (import only). This post is a practical, format-by-format guide showing how to load and save each one, what options are available, and how to convert between them.
All examples use the com.aspose.threed.* package:
import com.aspose.threed.*;
Maven dependency:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-3d-foss</artifactId>
<version>26.5.0</version>
</dependency>Format Landscape
Before diving in, here is a quick overview of the four supported formats and their typical use cases.
| Format | Extension | Import | Export | Material Support | Scene Hierarchy |
|---|---|---|---|---|---|
| OBJ | .obj | Yes | Yes | Basic (MTL files) | Flat |
| STL | .stl | Yes | Yes | None | Flat |
| glTF | .gltf / .glb | Yes | Yes | PBR | Full |
| FBX | .fbx | Yes | No | Rich | Full |
OBJ, STL, and glTF support bidirectional conversion. GLB (binary glTF) is a variant of the glTF format produced by passing FileContentType.BINARY to the GltfSaveOptions constructor. FBX files are loaded using Scene.fromFile() or FbxImporter.
OBJ: Wavefront Object
OBJ is one of the most widely supported 3D mesh formats. It stores geometry as plain text, with an optional .mtl companion file for materials.
Loading OBJ Files
The simplest approach is to pass the file path directly:
Scene scene = Scene.fromFile("model.obj");
For more control, use ObjLoadOptions:
ObjLoadOptions opts = new ObjLoadOptions();
Scene scene = Scene.fromFile("model.obj", opts);
When the OBJ file references an MTL file (via a mtllib directive), the library will attempt to load material definitions from it automatically. The MTL file should be in the same directory as the OBJ file or at the path specified in the directive.
Saving as OBJ
To convert any loaded scene to OBJ format, use the save() method with an .obj file path:
Scene scene = Scene.fromFile("input.fbx");
scene.save("output.obj");
OBJ Use Cases
- Exchanging mesh data between different 3D tools.
- Importing models from asset libraries that distribute in OBJ format.
- Quick inspection of geometry without complex scene hierarchies.
STL: Stereolithography
STL is the standard format for 3D printing. It represents raw triangulated geometry without materials, colors, or scene hierarchy. STL files come in two variants: ASCII (human-readable) and binary (compact).
Loading STL Files
Load an STL file using Scene.fromFile(), with or without explicit StlLoadOptions:
// Simple load
Scene scene = Scene.fromFile("part.stl");
// With explicit options
StlLoadOptions opts = new StlLoadOptions();
Scene scene = Scene.fromFile("part.stl", opts);
Saving as STL
Save a loaded scene to STL using a direct save() call or with explicit StlSaveOptions for binary-format control:
Scene scene = Scene.fromFile("model.obj");
scene.save("output.stl");
For granular control over the output file, use StlSaveOptions to configure the binary format and other export parameters:
StlSaveOptions opts = new StlSaveOptions();
scene.save("output.stl", opts);
STL Considerations
- STL stores only triangle geometry. Materials, textures, and scene hierarchy are outside the format spec and are omitted on export.
- STL export does not triangulate automatically. Binary STL writes only the first 3 vertices of each polygon face; quad and n-gon faces are silently truncated. ASCII STL writes all vertex indices per face, producing malformed output for non-triangle faces. Always use pre-triangulated meshes (all-triangle faces) when exporting to STL.
- Binary STL is significantly smaller than ASCII STL for large models.
- STL is the format of choice when the target is a 3D printer or slicer software.
glTF: GL Transmission Format
glTF is a modern format designed for efficient transmission and loading of 3D content, particularly on the web and in real-time applications. It supports PBR materials, scene hierarchies, and animations.
Loading glTF Files
Load glTF files using Scene.fromFile(), optionally passing GltfLoadOptions for additional control over the import:
Scene scene = Scene.fromFile("scene.gltf");
// With options
GltfLoadOptions opts = new GltfLoadOptions();
Scene scene = Scene.fromFile("scene.gltf", opts);
Saving as glTF
The basic save uses the file extension to determine the format:
scene.save("output.gltf");
For detailed control, use GltfSaveOptions:
GltfSaveOptions opts = new GltfSaveOptions();
opts.setPrettyPrint(true);
scene.save("output.gltf", opts);
GltfSaveOptions Details
| Method | Purpose |
|---|---|
setPrettyPrint(boolean) | Format the output JSON with indentation for readability. Set to false for smaller file size in production. |
glTF Use Cases
- Web-based 3D viewers (three.js, Babylon.js).
- Real-time applications and game engines.
- Preserving PBR material definitions across tools.
FBX: Filmbox
FBX is a proprietary format by Autodesk that is widely used in game development and digital content creation. Aspose.3D FOSS for Java includes FbxImporter and FbxLoadOptions for reading FBX files.
Binary FBX only. The FBX importer reads binary FBX files only. Passing an ASCII FBX file throws
ImportException. Node hierarchy and material extraction from complex FBX scenes may be partial. Animations and rigging data require further verification.
Loading FBX Files
Load binary FBX files using Scene.fromFile(), optionally passing FbxLoadOptions for additional control:
Scene scene = Scene.fromFile("character.fbx");
// With options
FbxLoadOptions opts = new FbxLoadOptions();
Scene scene = Scene.fromFile("character.fbx", opts);
Exporting a Loaded FBX Scene
FBX is import-only. To export FBX content, load it with Scene.fromFile() and save to a supported output format such as OBJ, STL, or glTF:
Scene scene = Scene.fromFile("character.fbx");
// Save as GLB
GltfSaveOptions opts = new GltfSaveOptions(FileContentType.BINARY);
scene.save("character.glb", opts);
FBX Use Cases
- Importing static mesh assets from Autodesk tools (Maya, 3ds Max) and converting to other formats.
- Loading binary FBX geometry and re-exporting to glTF/GLB, OBJ, or STL.
Batch Conversion
A common workflow is converting an entire directory of files from one format to another. Here is a pattern for batch conversion:
import com.aspose.threed.*;
import java.io.File;
public class BatchConvert {
public static void main(String[] args) throws Exception {
File inputDir = new File("models/obj");
File outputDir = new File("models/gltf");
outputDir.mkdirs();
GltfSaveOptions saveOpts = new GltfSaveOptions();
saveOpts.setPrettyPrint(true);
File[] objFiles = inputDir.listFiles(
(dir, name) -> name.toLowerCase().endsWith(".obj")
);
if (objFiles == null) return;
for (File objFile : objFiles) {
String baseName = objFile.getName()
.replaceFirst("\\.obj$", "");
Scene scene = Scene.fromFile(objFile.getAbsolutePath());
String outPath = new File(outputDir, baseName + ".gltf")
.getAbsolutePath();
scene.save(outPath, saveOpts);
System.out.println("Converted: " + objFile.getName()
+ " -> " + baseName + ".gltf");
}
}
}
This loads every .obj file in a directory, converts each to glTF with pretty-printed output, and saves the results. You can adapt this pattern for any source and target format combination.
Cross-Format Conversion Reference
The following table shows what to expect when converting between formats.
| From | To | Geometry | Materials | Hierarchy | Notes |
|---|---|---|---|---|---|
| OBJ | STL | Preserved | Lost | N/A | STL has no material support |
| OBJ | glTF/GLB | Preserved | Converted to PBR | Flat | MTL materials mapped where possible |
| STL | OBJ | Preserved | None | N/A | No materials in source |
| STL | glTF/GLB | Preserved | Default | Flat | Default material applied |
| glTF | OBJ | Preserved | Simplified | Flattened | PBR to basic material |
| glTF | STL | Preserved | Lost | Flattened | Geometry only |
| FBX | OBJ | Preserved | Simplified | Flattened | Material simplification |
| FBX | STL | Preserved | Lost | Flattened | Geometry only |
| FBX | glTF/GLB | Preserved | Converted to PBR | Preserved | Good fidelity |
General Conversion Guidelines
- Geometry is generally preserved across format pairs, with one important exception: when exporting to STL, meshes must consist entirely of triangle faces. Binary STL silently truncates quad and n-gon faces to their first 3 vertices; ASCII STL produces malformed output for non-triangle faces.
- Materials survive best between glTF and FBX-imported scenes that include material data. Converting to STL always drops materials. Converting to OBJ simplifies materials to the basic MTL model.
- Scene hierarchy is preserved between glTF-format files. OBJ and STL produce flat mesh structures without hierarchy. FBX hierarchy and material extraction are partially supported; complex scenes may require additional handling.
Putting It All Together
Here is a complete example that loads an OBJ file, inspects its nodes, and exports to both glTF and GLB:
import com.aspose.threed.*;
public class FormatWorkflow {
public static void main(String[] args) throws Exception {
// Load
Scene scene = Scene.fromFile("input.obj");
// Inspect
System.out.println("Nodes in scene:");
for (Node child : scene.getRootNode().getChildNodes()) {
System.out.println(" " + child.getName());
Transform t = child.getTransform();
System.out.println(" Translation: "
+ t.getTranslation());
}
// Export to glTF with options
GltfSaveOptions gltfOpts = new GltfSaveOptions();
gltfOpts.setPrettyPrint(true);
scene.save("output.gltf", gltfOpts);
// Export to GLB (binary glTF)
GltfSaveOptions glbOpts = new GltfSaveOptions(FileContentType.BINARY);
scene.save("output.glb", glbOpts);
System.out.println("Export complete.");
}
}
Summary
Aspose.3D FOSS for Java gives you a consistent API across multiple formats. The key points:
- OBJ – simple mesh interchange with basic materials.
- STL – geometry-only format for 3D printing pipelines.
- glTF / GLB – modern PBR-capable format for web and real-time use. Pass
FileContentType.BINARYto theGltfSaveOptionsconstructor for GLB output. - FBX – import only; rich format for loading assets from game engines and DCC tools.
Use format-specific load and save option classes (ObjLoadOptions, StlLoadOptions, StlSaveOptions, GltfLoadOptions, GltfSaveOptions, FbxLoadOptions) when you need fine-grained control over the import or export process.
For more details, visit the Aspose.3D documentation or browse the source on GitHub.
Quick Start
Add the Maven dependency and load your first 3D file:
<dependency>
<groupId>org.aspose</groupId>
<artifactId>aspose-3d-foss</artifactId>
<version>26.5.0</version>
</dependency>import com.aspose.threed.*;
import com.aspose.threed.export.SaveFormat;
try {
Scene scene = Scene.fromFile("model.obj");
scene.save("output.gltf");
}
Getting Started
Install Aspose.3D FOSS for Java via Maven and start working with 3D files in a single import statement:
import com.aspose.threed.*;
No Microsoft or Autodesk software is required. All reading and writing happens in the local Java process with no external dependencies.