How to Convert 3D Models in TypeScript

How to Convert 3D Models in TypeScript

Aspose.3D FOSS for TypeScript converts between 3D formats by loading into a neutral Scene representation and saving to the target format. This guide shows the most common conversions.

Step-by-Step Guide

Step 1: Install @aspose/3d

npm install @aspose/3d

Step 2: Load the Source File

Create a Scene and call scene.open(). Use the format-specific *LoadOptions class for best results.

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

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

Step 3: Save to the Target Format

Call scene.save() with the output path. The output format is detected from the file extension.

// OBJ → glTF binary (GLB)
scene.save('output.glb');

// OBJ → STL
scene.save('output.stl');

// OBJ → FBX
scene.save('output.fbx');

Step 4: Common Conversion Examples

OBJ to glTF / GLB

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

const scene = new Scene();
scene.open('model.obj', new ObjLoadOptions());
scene.save('model.glb');
console.log('Converted OBJ → GLB');

FBX to glTF

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

const scene = new Scene();
scene.open('scene.fbx');
scene.save('scene.gltf');
console.log('Converted FBX → glTF');

glTF to STL

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

const scene = new Scene();
scene.open('model.glb');
scene.save('model.stl');
console.log('Converted GLB → STL');

COLLADA to 3MF

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

const scene = new Scene();
scene.open('model.dae');
scene.save('model.3mf');
console.log('Converted COLLADA → 3MF');

Step 5: Batch Convert Multiple Files

import * as fs from 'fs';
import * as path from 'path';
import { Scene } from '@aspose/3d';
import { ObjLoadOptions } from '@aspose/3d/formats/obj';

const inputDir = './input';
const outputDir = './output';

fs.mkdirSync(outputDir, { recursive: true });

const objFiles = fs.readdirSync(inputDir).filter(f => f.endsWith('.obj'));

for (const file of objFiles) {
    const inputPath = path.join(inputDir, file);
    const outputFile = file.replace('.obj', '.glb');
    const outputPath = path.join(outputDir, outputFile);

    const scene = new Scene();
    scene.open(inputPath, new ObjLoadOptions());
    scene.save(outputPath);

    console.log(`Converted ${file}${outputFile}`);
}

Supported Conversion Matrix

From \ ToglTF/GLBOBJSTLFBX3MFCOLLADA
OBJ
glTF/GLB
FBX
STL
3MF
COLLADA

Common Issues and Fixes

Materials are lost after conversion

OBJ materials (usemtl, .mtl file) are loaded when ObjLoadOptions.enableMaterials = true. When saving to glTF, PBR material properties are mapped automatically. Complex materials (procedural, multi-layer) may convert with reduced fidelity.

Mesh appears scaled incorrectly

Different formats use different default units (millimeters for STL, meters for glTF). Use ObjLoadOptions.scale when loading or StlSaveOptions.scaleFactor when saving to normalize units.

Coordinate system mismatch (model flipped or rotated)

Some formats use right-hand Y-up, others Z-up. Use ObjLoadOptions.flipCoordinateSystem = true or apply a rotation to the root node after loading.


Frequently Asked Questions

Does conversion preserve animations?

Animation data is preserved when converting between formats that support it (FBX → glTF, COLLADA → FBX, etc.). STL and OBJ do not carry animation data.

Is texture data preserved?

Textures referenced by OBJ materials or embedded in glTF are carried through to the scene graph. When saving to GLB (binaryMode = true), the binary buffer is embedded in a single file. For OBJ output, textures are saved as separate files alongside the .obj.

Can I convert many files in parallel?

scene.open() and scene.save() are synchronous. Use Node.js worker_threads for parallel processing.


See Also