knotflow Lessons PDF ↓ Deutsch Handbook Modeller ↗ Source

Lesson 4.6 · IV Tools

3MF and pictures

What it's about: STL knows only triangles. A slicer gets neither colours nor separate parts from it, and every copy of a shape is stored in full in the file. 3MF, the 3D Manufacturing Format, can do all of this. knotflow writes it: kf render model.kf -o model.3mf and "save 3MF" in the browser, byte-identical between the two, as with STL. For the eye, kf also writes pictures (PNG and SVG); section 5 shows how its small renderer works.

What you should know first: a slicer that reads 3MF (the common ones do). For sections 3 and 4, Lesson 3.1 (frames and instances) helps, for section 1 Lesson 1.3.

Model STL 3MF
Cup 7.3 MB 1.3 MB
Vase 3.7 MB 0.77 MB
Sowing (600 spheres as instances, 2 single) 37.5 MB 0.98 MB
Chain (46 links as instances) 58.9 MB 0.26 MB
Romanesco 236 MB 62 MB

1. What an STL file lacks

An STL file (Lesson 1.3) is a list of triangles, each with its three corners and a normal. Nothing more:

2. What is inside a 3MF file

A 3MF file is a ZIP archive with three files. Two of them only say what is where ([Content_Types].xml and _rels/.rels). The actual model is in 3D/3dmodel.model, as XML:

<model unit="millimeter" …>
 <resources>
  <basematerials id="1">
   <base name="urchin 1" displaycolor="#996B9E"/>
   <base name="urchin 2" displaycolor="#D9AD52"/>
  </basematerials>
  <object id="2" type="model" name="urchin 1" pid="1" pindex="0"><mesh>
   <vertices> <vertex x="…" y="…" z="…"/> … </vertices>
   <triangles> <triangle v1="0" v2="1" v3="2"/> … </triangles>
  </mesh></object>
  <object id="3" … name="urchin 2 – copies 1 (shape)" …> … the spike … </object>
  <object id="4" type="model" name="urchin 2 – copies 1"><components>
   <component objectid="3" transform="-0.3522844 -1.036571 0 0.7363941 … -20.977262 7.129238 9.2325735"/>
   …
  </components></object>
 </resources>
 <build> <item objectid="2"/> <item objectid="4"/> </build>
</model>

3. The matrix of a copy

3MF writes a point as a row (x, y, z, 1) and multiplies the matrix from the right. So the twelve numbers are, row by row: the image of the x axis, that of the y axis, that of the z axis, and the translation. In knotflow a placement is a frame (Lesson 3.1), which is exactly these four vectors. The matrix is just a listing of them.

For the chain, the link is in the file once, and each of the 46 copies adds a line of twelve numbers. That is where 0.26 MB instead of 58 MB comes from.

4. Mirrored copies: what the reader makes of them

A mirror reverses the order of the corners. "Anticlockwise seen from outside" becomes "clockwise", and the triangle faces inwards. For STL, knotflow therefore flips the triangles of a mirrored copy.

For 3MF you might expect the reader to take care of this itself when the matrix mirrors. The cross-check with a slicer (Bambu Studio, --info) shows otherwise: with a mirroring component the whole has volume 0, one copy is inside out. Flipping the triangles yourself and pointing to this flipped shape also gives volume 0. How readers treat a mirroring component is evidently not reliable. So mirrored copies are written out computed, as in the STL, as an object of their own. Then the cross-check gives 313.65 mm³ each for the original and its mirror image. Mirroring is rare, and the copies without a mirror remain references.

A format is not just its specification, but also what the programs make of it. Without the test with a real reader, such a bug would only show up when printing.

5. Pictures: a small renderer

kf render x.kf -o x.png --view az,el --width px writes a picture, with a small renderer of its own (crates/cli/src/raster.rs) that writes PNG directly. Unlike the SVG renderer from Lesson 1.3, it doesn't draw from back to front but works like a graphics card:

  1. Perspective: every point is divided by its distance – things far away get smaller.
  2. Rasterising: for every triangle, the pixels it covers are determined.
  3. Depth buffer: the distance is remembered per pixel; a pixel is only painted over if the new triangle is closer. The distance is interpolated via 1/z, because 1/z – not z – varies evenly across the screen.
  4. Smooth normals: each corner gets the average of the neighbouring faces, but only of those that bend away by less than 35°. That way a sphere looks round and the edge of a cylinder stays sharp.
  5. Anti-aliasing: the image is computed at twice the size and then every 2×2 pixels are averaged.

6. Limits

7. A deeper look: the ZIP written by hand

The ZIP archive needs no library. It consists of three parts:

The checksum is CRC-32: the bytes are read as one long polynomial over bits and divided by a fixed polynomial; the remainder is the checksum. With a table for all 256 byte values, that is a dozen lines of code. A test checks it against the standard example: "123456789" gives CBF43926.

Compression uses Deflate from the library miniz_oxide, pure Rust. It was already in the project anyway, through the PNG export. The level is a compromise:

Level Romanesco Time
1 (fastest) 75 MB 4.3 s
3 (chosen) 62 MB 5.1 s
6 (default) 57 MB 8.8 s

The model never exists as one text in the process. At 20 million triangles that would be 1.6 GB, and the browser has 4 GB in all, with the triangles in them. Instead the text goes through Deflate and the CRC in pieces of 64 KB while it is being written, and only what is packed stays in memory. The bytes do not change: Deflate waits until its lookahead of 258 bytes is full and decides only then, so it never sees where a piece ends. A test cuts the same text at awkward places and compares.

And because browser and command line are meant to deliver the same bytes, every file in the archive carries the same fixed date, 1 January 1980, the earliest that ZIP knows. tools/same-stl.mjs compares STL and 3MF from both for all examples: identical.

Try it yourself

cargo run --release -p kf -- render examples/chain.kf -o out/chain.3mf
cargo run --release -p kf -- render examples/urchin.kf -o out/urchin.3mf
unzip -l out/chain.3mf
  1. Open out/urchin.3mf in your slicer. How many objects do you see, and which colours?
  2. Unpack out/chain.3mf (unzip) and look into 3D/3dmodel.model: how many <vertex> elements are there, and how many would the chain have without components?
  3. The CRC-32 of "123456789" is CBF43926. Check it with a one-line Python command (zlib.crc32).
  4. Something to think about: why is the vase's 3MF (no instances) smaller than its STL, even though XML needs many more characters per number than the 4 bytes of a floating-point number in the STL?