STL files are used to represent simple 3D models, defined using triangular 3D faces.
Initially it was introduced as native format for 3D Systems Stereolithography CAD system, but due to its extreme simplicity, it was adopted by a wide range of 3D modelling, CAD, rapid prototyping and 3D printing applications as the simplest 3D model exchange format.
STL is extremely bare-bones format: there are no complex headers, no texture / color support, no units specifications, no distinct vertex arrays. Whole model is specified as a collection of triangular faces.
There are two versions of the format (text and binary), this spec describes binary version.
This page hosts a formal specification of .stl file format of 3D Systems Stereolithography using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.
All parsing code for Python generated by Kaitai Struct depends on the Python runtime library. You have to install it before you can parse data.
The Python runtime library can be installed from PyPI:
python3 -m pip install kaitaistruct
Parse a local file and get structure in memory:
data = Stl.from_file("path/to/local/file.stl")
Or parse structure from a bytes:
from kaitaistruct import KaitaiStream, BytesIO
raw = b"\x00\x01\x02..."
data = Stl(KaitaiStream(BytesIO(raw)))
After that, one can get various attributes from the structure by invoking getter methods like:
data.header # => get header
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
import kaitaistruct
from kaitaistruct import KaitaiStruct, KaitaiStream, BytesIO
if getattr(kaitaistruct, 'API_VERSION', (0, 9)) < (0, 9):
raise Exception("Incompatible Kaitai Struct Python API: 0.9 or later is required, but you have %s" % (kaitaistruct.__version__))
class Stl(KaitaiStruct):
"""STL files are used to represent simple 3D models, defined using
triangular 3D faces.
Initially it was introduced as native format for 3D Systems
Stereolithography CAD system, but due to its extreme simplicity, it
was adopted by a wide range of 3D modelling, CAD, rapid prototyping
and 3D printing applications as the simplest 3D model exchange
format.
STL is extremely bare-bones format: there are no complex headers, no
texture / color support, no units specifications, no distinct vertex
arrays. Whole model is specified as a collection of triangular
faces.
There are two versions of the format (text and binary), this spec
describes binary version.
"""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.header = self._io.read_bytes(80)
self.num_triangles = self._io.read_u4le()
self.triangles = []
for i in range(self.num_triangles):
self.triangles.append(Stl.Triangle(self._io, self, self._root))
class Triangle(KaitaiStruct):
"""Each STL triangle is defined by its 3 points in 3D space and a
normal vector, which is generally used to determine where is
"inside" and "outside" of the model.
"""
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.normal = Stl.Vec3d(self._io, self, self._root)
self.vertices = []
for i in range(3):
self.vertices.append(Stl.Vec3d(self._io, self, self._root))
self.abr = self._io.read_u2le()
class Vec3d(KaitaiStruct):
def __init__(self, _io, _parent=None, _root=None):
self._io = _io
self._parent = _parent
self._root = _root if _root else self
self._read()
def _read(self):
self.x = self._io.read_f4le()
self.y = self._io.read_f4le()
self.z = self._io.read_f4le()