How to Save 3D Models in TypeScript

How to Save 3D Models in TypeScript

Aspose.3D FOSS for TypeScript saves scenes to all supported formats with a single scene.save() call. The output format is detected automatically from the file extension. This guide covers saving to each format and using format-specific options.

Step-by-Step Guide

Step 1: Install @aspose/3d

npm install @aspose/3d

Step 2: Load or Construct a Scene

Either load an existing file or build a scene programmatically before saving.

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';

// Load from file
const scene = new Scene();
scene.open('source.obj', new ObjLoadOptions());

// Or create a new empty scene
const emptyScene = new Scene();

Step 3: Save with Auto-Detected Format

scene.save(path) detects the output format from the file extension:

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';

const scene = new Scene();
scene.open('input.obj', new ObjLoadOptions());

// Save as binary glTF
scene.save('output.glb');

// Save as JSON glTF
scene.save('output.gltf');

// Save as STL
scene.save('output.stl');

// Save as OBJ
scene.save('output.obj');

// Save as FBX
scene.save('output.fbx');

// Save as 3MF
scene.save('output.3mf');

// Save as COLLADA
scene.save('output.dae');

Step 4: Use Format-Specific SaveOptions

For fine-grained control, pass a format-specific options object:

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';
import { GltfSaveOptions } from '@aspose/3d/formats/gltf';

const scene = new Scene();
scene.open('model.obj', new ObjLoadOptions());

// Export to GLB with specific options
const saveOptions = new GltfSaveOptions();
saveOptions.binaryMode = true;   // produce GLB (binary glTF)

scene.save('output.glb', saveOptions);

Step 5: Save to a Buffer (In-Memory)

Use scene.saveToBuffer() to get the output as a Buffer without writing to disk:

import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';

const scene = new Scene();
scene.open('model.obj', new ObjLoadOptions());

const buffer = scene.saveToBuffer('output.glb');
console.log(`Buffer size: ${buffer.length} bytes`);

// Send via HTTP, upload to S3, etc.

Step 6: Verify the Output

After saving, verify the file exists and has a non-zero size:

import * as fs from 'fs';

const stats = fs.statSync('output.glb');
console.log(`Saved output.glb: ${stats.size} bytes`);

Format Support Matrix

ExtensionFormatNotes
.glbglTF 2.0 BinaryRecommended for glTF; all assets embedded in single file
.gltfglTF 2.0 JSONSeparate .bin and texture files alongside JSON
.objWavefront OBJWrites .mtl material file alongside .obj when materials present
.stlSTLDefault: binary STL; use StlSaveOptions.ascii = true for text
.fbxAutodesk FBXBinary FBX format
.3mf3D ManufacturingSuitable for 3D printing workflows
.daeCOLLADAXML-based interchange format

Common Issues and Fixes

Error: Unsupported format when saving

Check that the file extension matches a supported format. The library uses the extension to detect format; a file named output.xyz will fail.

.obj file saves but materials are missing

When saving OBJ, the material library (.mtl) is written automatically alongside the .obj. Both files must be in the same directory when re-opening. If you only need geometry, set ObjSaveOptions.enableMaterials = false.

Large .gltf with separate textures

Use .glb instead of .gltf; it bundles binary data into a single self-contained file. Set GltfSaveOptions.binaryMode = true when passing a GltfSaveOptions instance.


Frequently Asked Questions

Can I save to multiple formats in one run?

Yes; call scene.save() multiple times with different paths:

scene.save('output.glb');
scene.save('output.stl');
scene.save('output.obj');

Does saving modify the scene?

No. scene.save() is a read-only operation on the scene graph. You can save the same scene to multiple formats without any side effects.

Can I overwrite the source file?

Yes. Pass the same path to scene.save() that you used in scene.open(). The library writes to a buffer and then writes to disk.


See Also