3D コンテンツを扱う Node.js 開発者は、フォーマット間の変換が頻繁に必要です。デザインツールは OBJ をエクスポートし、Web レンダラは GLB を期待し、3D プリンタは STL を必要とし、製造パイプラインは 3MF を使用します。これらの変換を単一の一貫した API で処理すれば、パイプライン内の外部ツールの数を減らし、変換ロジックをアプリケーションコード内に保つことでテストやバージョン管理が可能になります。.
その @aspose/3d package (v24.12.0, MIT license) は、Node.js で主要な 3D フォーマットすべての読み書きのための TypeScript ファースト API を提供します。このガイドでは、最も一般的な変換ワークフローを順に解説します。.
インストール
npm install @aspose/3d
要件: Node.js 18、20、または 22;TypeScript 5.0 以降。唯一のランタイム依存関係は xmldom.
サポートされているフォーマット
以下の表は、本ガイドで取り上げるフォーマットとその読み書きサポートを示しています。.
| フォーマット | 拡張子 | 読み取り | 書き込み | 注記 |
|---|---|---|---|---|
| Wavefront OBJ | .obj | はい | はい | 読み取り/書き込み .mtl マテリアルファイル |
| glTF 2.0 (JSON) | .gltf | はい | はい | 標準ウェブ配信フォーマット |
| glTF 2.0(バイナリ) | .glb | はい | はい | 自己完結型で、ウェブ向けに推奨されます |
| STL(ASCII/バイナリ) | .stl | はい | はい | 標準的な3D印刷フォーマット |
| 3MF | .3mf | はい | はい | リッチなメタデータを備えた製造フォーマット |
| FBX | .fbx | いいえ* | いいえ* | インポーター/エクスポーターは存在しますが、フォーマットの自動検出は組み込まれておらず、via では使用できません scene.open() |
| COLLADA | .dae | はい | はい | XMLベースの交換フォーマット |
OBJはインポートとエクスポートの両方に対応しています。ロードは scene.open() そして保存は scene.save('output.obj'), または glTF、STL、3MF などの他のサポートされているフォーマットに変換できます。.
OBJ から GLB へ(Web 配信)
OBJ をバイナリ glTF(GLB)に変換するのが最も一般的な Web ワークフローです。GLB はテクスチャ、ジオメトリ、メタデータが単一ファイルにまとめられた自己完結型バイナリバンドルであり、HTTP 配信に効率的で、Three.js、Babylon.js、model‑viewer での直接ロードが可能です。.
import { Scene } from '@aspose/3d';
function convertObjToGlb(inputPath: string, outputPath: string): void {
const scene = new Scene();
scene.open(inputPath);
scene.save(outputPath); // extension '.glb' selects binary GLB format
console.log(`Converted ${inputPath} -> ${outputPath}`);
}
convertObjToGlb('model.obj', 'model.glb');
出力フォーマットはファイル拡張子から推測されます。使用してください .glb バイナリGLB用、または .gltf 別々のJSON + 用 .bin レイアウト。.
OBJ から STL へ(3D 印刷用準備)
STL は FDM と SLA 3D 印刷の共通言語です。PrusaSlicer、Bambu Studio、Chitubox などのスライサーはすべて STL を受け入れます。OBJ から STL への変換は、両フォーマットが三角形メッシュを保持しているため簡単です。.
import { Scene } from '@aspose/3d';
function convertObjToStl(inputPath: string, outputPath: string): void {
const scene = new Scene();
scene.open(inputPath);
scene.save(outputPath); // extension '.stl' selects STL format
console.log(`STL written to ${outputPath}`);
}
convertObjToStl('part.obj', 'part.stl');
STL はカラー、マテリアル、UV データを保持しません。OBJ ファイルでマテリアルグループを使用している場合、その情報はエクスポート時に失われます。カラー情報を保持したい印刷フォーマットには、代わりに 3MF を検討してください(下記参照)。.
STL から glTF へ(スキャナおよび CAD から Web へ)
構造光スキャナやパラメトリック CAD エクスポーターは一般的に STL を出力します。glTF に変換することで、サーバー側でのレンダリング工程なしに、ウェブベースのビューアや AR プラットフォームでジオメトリを利用できるようになります。.
import { Scene } from '@aspose/3d';
function convertStlToGltf(inputPath: string, outputPath: string): void {
const scene = new Scene();
scene.open(inputPath);
// extension '.gltf' saves as JSON + .bin sidecar
scene.save(outputPath);
console.log(`glTF written to ${outputPath}`);
}
convertStlToGltf('scan_output.stl', 'scan_output.gltf');
STL にはマテリアルやテクスチャ情報が含まれないため、生成される glTF ファイルはジオメトリのみとなります。必要に応じて、ロード後にシーンノードにプログラムでマテリアルを付与することが可能です。.
3MF to glTF (Manufacturing to Visualization)
3D Manufacturing Format(3MF)は、カラー、材料、コンポーネントツリー、印刷メタデータをジオメトリと共に保存できるため、付加製造ワークフローでますます利用されています。3MF を glTF に変換することで、シーン構造を保持したまま、Web ツールでの下流の可視化が可能になります。.
import { Scene } from '@aspose/3d';
function convert3mfToGlb(inputPath: string, outputPath: string): void {
const scene = new Scene();
scene.open(inputPath);
scene.save(outputPath); // extension '.glb' selects binary GLB format
console.log(`3MF -> GLB: ${outputPath}`);
}
convert3mfToGlb('assembly.3mf', 'assembly.glb');
3MF files often contain multi-component assemblies. The scene graph produced by scene.open() コンポーネント階層を保持します scene.rootNode.childNodes,、そのため保存前に個々のパーツを検査または操作できます。.
バッチ変換パターン
ファイルのディレクトリを処理する際、各変換を try/catch 単一の破損したファイルがバッチ全体を中止しないようにします。.
import { Scene } from '@aspose/3d';
import { readdirSync } from 'fs';
import { join, basename, extname } from 'path';
interface ConversionResult {
input: string;
output: string;
success: boolean;
error?: string;
}
function batchConvertToGlb(
inputDir: string,
outputDir: string,
extensions: string[] = ['.obj', '.stl', '.3mf', '.dae'] // .fbx excluded: format auto-detection not wired
): ConversionResult[] {
const results: ConversionResult[] = [];
const files = readdirSync(inputDir).filter((f) =>
extensions.includes(extname(f).toLowerCase())
);
for (const file of files) {
const inputPath = join(inputDir, file);
const outputPath = join(outputDir, basename(file, extname(file)) + '.glb');
try {
const scene = new Scene();
scene.open(inputPath);
scene.save(outputPath); // extension '.glb' infers GLB format
results.push({ input: inputPath, output: outputPath, success: true });
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
results.push({ input: inputPath, output: outputPath, success: false, error: message });
console.error(`Failed to convert ${file}: ${message}`);
}
}
const succeeded = results.filter((r) => r.success).length;
console.log(`Batch complete: ${succeeded}/${results.length} files converted.`);
return results;
}
// Usage
batchConvertToGlb('./input', './output');
上記のパターンは、入力ディレクトリからサポートされている各ファイル拡張子を読み取り、GLB に変換し、ループを停止せずに失敗をログに記録します。返される配列は ConversionResult オブジェクトはレポートやリトライロジックに使用できます。.
結論
@aspose/3d Node.js TypeScript アプリケーションにおけるフォーマット変換の全ニーズを、統一された2ステップ API でカバーします: scene.open() ロードするために、, scene.save() 書き込むために。覚えておくべき重要な制約は、FBX インポーターおよびエクスポータークラスは存在するものの、フォーマットの自動検出はまだ実装されていないため、FBX ファイルは via でロードできないことです。 scene.open().
詳細については、 Scene, Node,、および Mesh これらの例で使用されているクラスについては、本ドキュメントのクラスリファレンスページをご覧ください。.