A comprehensive TypeScript library for parsing and working with 3D Manufacturing Format (3MF) files.
Key features:
- Open and inspect OPC/ZIP-based
.3mfarchives - Parse
[Content_Types].xmland_rels/.relsrelationships - Extract and validate the
<model>element - Read
<resources>: base materials and object definitions - Handle
<mesh>: vertices, triangles, manifold and orientation checks - Compose objects via
<components>and build instructions - Parse
<build>items and link them to resources - In-memory API with
ThreeMFDocumentfor JSON serialization/round-trip - Rich error reporting (
ValidationError,ParseError) and spec warnings (WarningLogger) - CI-ready with
bun testand GitHub Actions
bun installbun installbun test --watchbun run buildbun run build --watchAfter building, publish the package to npm:
bun run build
npm publishImplementations status of 3MF spec (see 3MF specification):
- Core 3MF Mesh Specification (Core spec)
- Production Extension
- Materials and Properties Extension
- Slice Extension
- Beam Lattice Extension
- Boolean Operations Extension
- Displacement Extension
- Secure Content Extension
- Volumetric Extension
import {
openArchive,
getPrimaryModelPath,
getModel,
parseResourcesFromXml,
parseBuildFromXml,
ThreeMFDocument
} from 'three-mf';
async function example(filePath: string) {
// 1. Open 3MF archive
const zip = await openArchive(filePath);
const modelPath = await getPrimaryModelPath(zip);
// 2. Parse the <model> element
const model = await getModel(zip, modelPath);
const xmlText = await zip.file(modelPath)!.async('text');
// 3. Extract resources and build items
const resources = parseResourcesFromXml(xmlText);
const buildItems = parseBuildFromXml(xmlText, resources);
// 4. Work with in-memory document
const document = new ThreeMFDocument(model, resources, buildItems);
console.log(JSON.stringify(document.toJSON(), null, 2));
}- openArchive(pathOrBuffer: string | Buffer | Uint8Array): Promise
- getPrimaryModelPath(zip: JSZip): Promise
- getContentTypeMap(zip: JSZip): Promise
- getContentType(map: ContentTypeMap, partPath: string): string | null
- getRelationships(zip: JSZip): Promise
- getStartPartPath(relationships: RelationshipMap): string
- getRelationshipsByType(relationships: RelationshipMap, type: string): Relationship[]
- parseModel(content: string): Model
- getModel(zip: JSZip, modelPath: string): Promise
- parseResourcesFromXml(content: string): Resources
- parseResources(modelXml: any): Resources
- parseBuildFromXml(content: string, resources: Resources): BuildItem[]
- ThreeMFDocument(model: Model, resources: Resources, build: BuildItem[])
- toJSON(): DocumentJSON
- fromJSON(json: DocumentJSON): ThreeMFDocument
- parseMesh(element: any): Mesh
- validateMesh(mesh: Mesh, type: string): Mesh
- flattenComponentHierarchy(id: number, objects: Map<number, ObjectResource>): Mesh
- validateAllComponentReferences(objects: Map<number, ObjectResource>): void
- meshToXml(mesh: Mesh)
- objectWithMesh(id: number, mesh: Mesh, attrs?): ObjectElement
- buildItemXml(objectId: number, attrs?): BuildItemElement
- texture2dToXml(tex: Texture2D): any — Build
<texture2d>elements for the Materials & Properties Extension. - texture2dGroupToXml(grp: Texture2DGroup): any — Build
<texture2dgroup>elements. - colorGroupToXml(grp: ColorGroup): any — Build
<colorgroup>elements. - compositeMaterialsToXml(grp: CompositeMaterials): any — Build
<compositematerials>elements. - multiPropertiesToXml(mp: MultiProperties): any — Build
<multiproperties>elements. - pbSpecularDisplayPropertiesToXml(dp: PBSpecularDisplayProperties): any — Build
<pbspeculardisplayproperties>elements. - pbMetallicDisplayPropertiesToXml(dp: PBMetallicDisplayProperties): any — Build
<pbmetallicdisplayproperties>elements. - pbSpecularTextureDisplayPropertiesToXml(dp: PBSpecularTextureDisplayProperties): any — Build
<pbspeculartexturedisplayproperties>elements. - pbMetallicTextureDisplayPropertiesToXml(dp: PBMetallicTextureDisplayProperties): any — Build
<pbmetallictexturedisplayproperties>elements. - translucentDisplayPropertiesToXml(dp: TranslucentDisplayProperties): any — Build
<translucentdisplayproperties>elements.
- create3MFArchive(xmlObj: ThreeMFXml, modelFilePath?: string): JSZip
- parseProductionExtensions(xmlObj: any): void
- serializeProductionExtensions(xmlObj: any): void
- generatePartRels(xmlObj: any): any[]
- generateUUID(): string
This library provides high-level helpers to build 3MF XML structures and package them without manual XML or ZIP boilerplate.
import type { ThreeMFXml } from './src/builder';
import { objectWithMesh, buildItemXml } from './src/builder';
import { create3MFArchive } from './src/packager';
import type { Mesh } from './src/mesh';
// Given a Mesh object (from parseMesh or custom geometry)
const mesh: Mesh = /* ... */;
// 1. Build object and build-item XML elements
const objElement = objectWithMesh(1, mesh);
const itemElement = buildItemXml(1 /* object ID */, { '@_partnumber': 'baseplate' });
// 2. Assemble the top-level ThreeMFXml structure
const xmlObj: ThreeMFXml = {
model: {
'@_unit': 'millimeter',
'@_xmlns': 'http://schemas.microsoft.com/3dmanufacturing/core/2015/02',
resources: { object: objElement },
build: { item: itemElement }
}
};
// 3. Create the .3mf package and write it out
const zip = create3MFArchive(xmlObj);
const buffer = await zip.generateAsync({ type: 'nodebuffer' });
await Deno.writeFile('output.3mf', buffer);For runnable examples and usage details, see examples/README.md.
We welcome contributions! Please follow these steps to contribute to three-mf:
- Fork the repository and clone your fork locally.
- Create a feature branch:
git checkout -b feature/your-feature. - Install dependencies:
bun install. - Implement your changes.
- Add or update tests under the
test/directory to cover your changes. - Ensure all tests pass:
bun test. - Commit your changes with a descriptive message:
git commit -m 'feat: describe your change'. - Push to your branch:
git push origin feature/your-feature. - Open a pull request against the
mainbranch with a clear description of your changes.
For bug reports or feature requests, please open an issue.
MIT