Kukan Kogei -空間工芸-

Notes about my 3d printing artcrafts

Exporting 3d surfaces into STL files in Python

Overview

When writing some python code in Jupyter and displaying 3d surfaces using matplotlib, I sometimes want to export the surfaces into STL (stereolithography) files to use the 3d models in other software(Blender, Meshlab, etc.). Although I found some python's pip packages and some are attractive, I need more lightweight, simple and easy to generate. 

f:id:asahidari:20200507230606p:plain

STL export tools in Python

To export surfaces into STL files, you can use numpy-stl package installed with pip, and you can find some similar pip packages in github. However, when you create some surfaces using matplotlib, you have to install these pip packages in advance, and some may requires to use and learn new APIs. 

Other Tools to generate mathematical surfaces

Drawing 3d surfaces with complex math functions (like ellitic integrals), you may want to use professional math tools like Mathematica, MATLAB, Geogebra, etc. And to export 3d graphs into 3d format files, available choises seems to be limited.

 

After searching alternatives, I found surf2stl.m, a small script in MATLAB (or Octave), which can export 3d surfaces into STL (stereolithography) format file. The script is relatively easy to understand, and it seems possible to implement this in python code. So I tried that. 

 

Writing lightweight script to  export surfaces to STL files

After reading the MATLAB's script, I wrote python version of surf2stl, surf2stl.py.

github.com

 

This script have 2 fucntions, write and tri_write.  Both requires only filename, X, Y and Z   (X,,Y,Z : array-like). And tri_write requires one more parameter, a Delaunay triangle object The parameter decides mesh triangulation if you use other parameters (like u, v).

 

Basically, these two function export triangulated meshes. This script have no options to quadify them so far. However, you may easily do that applying quadifying tools in other 3d apps. (For example, in Blender, Applying Menu->Faces-> Tris to Quads .)

Example

When you write this code,

import numpy as np

# create x,y,z data for 3d surface plot
x = np.linspace(-6, 6, 30)
y = np.linspace(-6, 6, 30)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X ** 2 + Y ** 2))

import matplotlib.pyplot as plt

# draw surface plot
ax = plt.axes(projection='3d')
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
                cmap='viridis', edgecolor='none')
ax.set_title('surface');

you get the graph.

 

f:id:asahidari:20200507232947p:plain

And when you write the folloing,

import surf2stl

# export surface to a stl format file
surf2stl.write('3d-sinusoidal.stl', X, Y, Z)

you get this 3d model in STL format (This image show the result opened in Meshlab). 

 

f:id:asahidari:20200507232649p:plain

References

* surf2stl.m

https://jp.mathworks.com/matlabcentral/fileexchange/4512-surf2stl

 

 

Environments

* Python 3

* numpy

* scipy

* matplotlib (for drawing 3d graph in python)

* jupyter notebook (if you can use)