Working with STL files in NumPy

MIKE ARMISTEAD
2 min readAug 26, 2020

--

STL files are used for 3D models and 3D printing. Being able to work with them and automate them can be useful. The way STL files describe the surface geometry of a three-dimensional. The way they do this is with vertices and facets. Facets are small triangles that create the surface of the 3D image. Each facet is connected by 3 vertices (corners). Each vertex has 3 coordination points (x,y,z) so each facet has 9 coordination points.

The libraries that are needed to be able to see STL 3D files are matplotlib, Numpy and STL from mesh. The code I am going to show is in the documentation for numpy-stl but I will break it down because it took me a couple of tries before I could comprehend it.

First thing that needed to happen was to create the axes and figure so we can plot our STL file. We needed to create a 3D plot because we are working with 3D images, like I said I will be breaking this down so I might be pointing out some obvious things. Next load the STL file with mesh, I am working with a plain sphere ball file that comes with MeshMixer. Now you have your_mesh which is not a numpy array yet. Once we use the points method we get a numpy array. The sphere is a shape of (12600, 9), so we need to flatten this out and scale it so we can get the X, Y and Z coordinates. Once that is done it is plotted and we can see our 3D STL model in Matplotlib. The next article we will learn how to create numpy arrays and turn them into STL files.

--

--

MIKE ARMISTEAD
MIKE ARMISTEAD

Written by MIKE ARMISTEAD

Tech recruiter turned Data Scientist

Responses (1)