threemf2
Library for reading and writing 3MF (3D Manufacturing Format) packages with both eager and lazy loading support.
This repository uses a Cargo workspace with threemf2 (main crate), threemf2-benches (benchmarks), and threemf2-tests (integration tests with large data). The threemf2 crate provides a compact core model representation and I/O helpers for reading/writing 3MF packages with multiple loading strategies optimized for different use cases.
Supported 3MF Extensions and Maximum Supported Versions
| 3MF Specifications | Type | Read1 | Write2 | Current supported version |
|---|---|---|---|---|
| 3MF Core Spec | Core | MUST | MUST | 1.3.0 |
| Triangle Set | Extension | MUST | OPTIONAL | Core Spec 1.3.0 |
| Production | Extension | MUST | OPTIONAL | 1.1.2 |
| Beam Lattice | Extension | MUST | OPTIONAL | 1.2.0 |
Note: This library is still in active development, expect frequent API changes!!
Note1: Reading these data are currently MUST, which means if the data exists in the 3MF Model the library will automatically read it
Note2: Write these data are currently optional however they are available by default in the mapping of the Rust types and are not conditionally compiled.
Overview
threemf2 provides:
- Core Data Structures: Complete 3MF model representation (
Model,Object,Mesh, etc.) - Multiple Loading Strategies:
ThreemfPackage- Eager loading for complete data accessThreemfPackageLazyReader- Lazy loading for memory-constrained environments
- Flexible I/O: Support for reading/writing 3MF packages with different performance characteristics
- Easy reading of data through query APIs.
- Easy creation of 3MF Model through builder APIs. See
builder_example.rsfor the most basic starter guide.
- Extension Support: All 3MF extensions (Production, Beam Lattice, etc.) are always available
- Custom Parts: Support for known parts (thumbnails) and unknown parts (custom XML data)
Performance Options
Choose the right loading strategy for your use case:
- Memory-Optimized: Lower memory usage, good for large files. This is the default.
- Speed-Optimized: Faster parsing, higher memory usage
- Lazy Loading: Defers loading until accessed, best for inspection-only use cases
Key types and files:
- Core model types in threemf2/src/core/ —
model,object,resources,mesh,transform, etc. io::ThreemfPackage— eager loading entry pointio::ThreemfPackageLazyReader— lazy loading entry point
Cargo Features
This crate uses optional Cargo features to control functionality. Enable only what you need.
Core Serialization Features
write— Enable writing 3MF data (addsToXmlderive to all 3MF types usinginstant_xml)memory-optimized-read— Enable memory-efficient reading (addsFromXmlderive to all 3MF types usinginstant_xml)speed-optimized-read— Enable fast reading (addsserde::Deserializederive to all 3MF types usingserde_roxmltree)
Package I/O Features
io-write— Package writing with ZIP creation (requireswrite)io-memory-optimized-read— Package reading with memory optimization (requiresmemory-optimized-read)io-speed-optimized-read— Package reading with speed optimization (requiresspeed-optimized-read)io-lazy-read— Lazy loading functionality (requiresio-memory-optimized-read)
Default Features
io-write, io-memory-optimized-read, io-lazy-read, write, memory-optimized-read
Feature Combinations
# Basic reading
= "0.1"
# Full I/O with lazy loading (default)
= { = "0.1", = ["io-lazy-read"] }
# Memory-constrained environments
= { = "0.1", = ["io-lazy-read"], = false }
# High-performance reading
= { = "0.1", = ["io-speed-optimized-read"] }
Examples
The threemf2/examples/ directory contains runnable examples for different use cases:
write.rs- Create and write 3MF packagesbuilder_example.rs- Using ModelBuilder for ergonomic model constructionunpack.rs- Lazy loading withThreemfPackageLazyReaderio_memory_optimized_read.rs- Memory-efficient readingio_speed_optimized_read.rs- High-performance readingstring_extraction.rs- Access raw XML contentbeamlattice_write.rs- Working with beam lattice extensions
Run examples with:
Benchmarks
The benchmarks use Criterion.rs as the benchmark harness on stable Rust channel.
Benchmarks are located in the threemf2-benches crate. To run them, use cargo bench --package threemf2-benches. The files used for the benchmarks are on Git LFS, so ensure git lfs is enabled.
-
reader - Benchmarks the different serialization options instant-xml and serde-roxmltree on an uncompressed 3MF model file.
-
threemf_reader - Benchmarks the different reader methods on ThreemfPackage.
-
threemf_write - Benchmarks the writer method on a full 3MF package.
API Overview
Core Data Structures
Model- Root 3MF model with resources and build configurationObject- 3D objects (meshes or component assemblies)Mesh- Triangle mesh geometry with vertices and trianglesComponent- Object references with transforms
Package I/O
ThreemfPackage- Eager loading - loads all data upfrontThreemfPackageLazyReader- Lazy loading - loads metadata first, data on-demand
Reading and Writing easily through io crate
querymodule enables easy retrieval of data from the 3MF Package or 3MF model.builderenables easy creation of 3MF Model.
Building & Testing
Requirements
- Rust 1.89.0 or later (2024 edition)
- Cargo package manager
Build Commands
# Build with all features
# Check formatting and linting
Testing
# Run all tests in the workspace (includes threemf2, threemf2-tests)
# Run tests for specific crates
# Install cargo-all-features and run tests for multiple feature combinations at once
# Run benchmarks
License
This project and its source code are released under MIT or Apache 2.0 licenses.
Contributing
Contributions are welcome.
- Open an issue to discuss major changes or report bugs.
- Fork the repo and create a feature branch.
- Add tests that exercise new behavior (tests may be feature-gated).
- Run tests locally with all possible features, preferably use
cargo all-features test: - Add or update an example
- Add or update the documentation.
- Submit a pull request with a clear description and link to any related issue.
AI-Assisted Contributions
We welcome contributions created with the assistance of AI tools. However, all contributors must:
- Clearly disclose AI assistance in your pull request description and commit messages
- Provide due diligence by:
- Testing the code thoroughly (run all tests with
cargo-all-features, see above) - Reviewing the generated code for correctness and adherence to project conventions
- Understanding what the code does and why it works
- Ensuring the contribution follows the project's style and patterns
- Testing the code thoroughly (run all tests with
- Take responsibility for the final code quality and functionality
AI tools can be excellent for productivity, but human oversight and understanding remain essential for maintaining code quality.
By contributing you agree to license your contributions under MIT or Apache 2.0 licenses.