##### For issues and/or questions, create an issue on Github: WoLpH/numpy-stl issues

As a followup of my earlier article about reading and writing STL files with Numpy, I’ve created a library that can be used easily to read, modify and write STL files in both binary and ascii format.

The library automatically detects whether your file is in ascii or binary STL format and is very fast due to all operations being done by numpy.

First, install using pip or easy_install:

```
pip install numpy-stl
# Or if you don't have pip available
easy_install numpy-stl
```

Note that `numpy`

numpy and python-utils version 1.6 or greater are required. While these should both be installed automatically by pip/easy_install, for numpy it’s generally recommended to download a binary release so it installs a bit faster.

Example usage: https://github.com/WoLpH/numpy-stl

```
import numpy
from stl import mesh
# Using an existing stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')
# Or creating a new mesh (make sure not to overwrite the `mesh` import by
# naming it `mesh`):
VERTICE_COUNT = 100
data = numpy.zeros(VERTICE_COUNT, dtype=mesh.Mesh.dtype)
your_mesh = mesh.Mesh(data, remove_empty_areas=False)
# The mesh normals (calculated automatically)
your_mesh.normals
# The mesh vectors
your_mesh.v0, your_mesh.v1, your_mesh.v2
# Accessing individual points (concatenation of v0, v1 and v2 in triplets)
assert (your_mesh.points[0][0:3] == your_mesh.v0[0]).all()
assert (your_mesh.points[0][3:6] == your_mesh.v1[0]).all()
assert (your_mesh.points[0][6:9] == your_mesh.v2[0]).all()
assert (your_mesh.points[1][0:3] == your_mesh.v0[1]).all()
your_mesh.save('new_stl_file.stl')
```

Documentation can be found here: http://numpy-stl.readthedocs.org/en/latest/

Please let me know if you have any problems using it or just to tell me that you like the project π

For the fastest response, ask questions on Github: WoLpH/numpy-stl issues

I run above program on raspberrypi and getting this error:

Traceback (most recent call last):

File “/home/pi/checkstl.py”, line 1, in

from stl import stl

File “/usr/local/lib/python2.7/dist-packages/stl/__init__.py”, line 2, in

import stl.ascii

ImportError: No module named ascii

For future reference, this error is caused by a different stl package. Since I started this project someone else apparently had a similar idea. Unfortunately the two projects conflict so if you get this error you need to do a “pip uninstall stl” followed by a “pip install numpy-stl” to get everything working again π

Still the error persists. Even after doing the uninstall and reinstall

Do you have a file called “stl.py” by any chance? If so, please rename it since it’s apparently causing colisions.

To test:

Hi, I’m trying to create STL files after from generating 3D graphics with Mayavi and Matplotlib.

I can not find a way to save the 2D arrangements for my variables X, Y and Z in a STL file, these variables are very important because their are used in methods to generate and display 3D surfaces.

Thanks!

Hi Miguel, I’ve since updated the package to include a Matplotlib example. Perhaps that helps? π

Have a look at the quickstart: http://numpy-stl.readthedocs.org/en/latest/usage.html#quickstart

Hi Rick,

This library looks awesome and perfectly suited to my needs. I’d like to create basic solids like cubes, for example and was just wondering whether this was possible with the library and if so, how I would go about doing this.

Many Thanks π

Hi Gregory,

It’s definitely possible although I’m not sure if it’s the easiest library for your purpose. Creating your own objects with the library requires you to manually create an array with points for the vertices. The library is meant to read/modify/write existing stl files and creating new stuff is a tad difficult. There are examples in the test though: https://github.com/WoLpH/numpy-stl/blob/master/tests/test_mesh.py#L99

Ah, Ok. I’m looking at using the library to create cubes at various points and then concatenate them. Would it be better for me to create the cubes manually as STL files and then use the library to concatenate them?

Concatenation is just appending the data arrays so it doesn’t make much difference really. It would come down to something like this:

Hi! Thanks for writing this library! It works beautifully! I have a question about his question–suppose he has 3 cubes –are they describe as points, or are they already tessellated by triangles? Suppose they are interpenetrating? If all he has is the vertices (not triangularized), then I think for the general problem, he may have to use something like alpha shapes to construct the mesh from the collection of points…Some cleanup of points that are inside of the surface may also be necessary–I’m not sure if alpha shapes will remove those automatically…

Hi! I am folowing the example at https://github.com/WoLpH/numpy-stl. I am working with WinPython-64bit-2.7.10.1 under Windows 7. My version of python utils is 1.6.2. When trying to execute the example with one of the stl files provided in the package (for example, bynarri Moon.stl) I get the following errors:

1) AssertionError: File too large, got 1953785888 triangles which exceeds the maximum of 1000000

2) ValueError: Attempted relative import in non-package.

The fist one I am trying to solve increasing the default MAX_COUNT in stl.py.

But I don’t know how to deal with the second one. Could you give me your assistance?

Thanks!

I tried to set MAX_COUNT to 1e10, but now I am facing with another problem:

File “C:\Users\tester\Documents\WinPython-64bit-2.7.10.1\python-2.7.10.amd64\lib\site-packages\stl\stl.py”, line 99, in _load_binary

return numpy.fromfile(fh, dtype=cls.dtype, count=count)

MemoryError

The MemoryError is because you’re trying to load an absurd amount of data so it’s most likely detecting your STL file incorrectly. Perhaps it’s trying to load an ascii file as a binary file?

As for the ValueError, I would need to see the entire stacktrace to know where the problem is. My guess is that your filenames are clashing with the numpy-stl files.

Hello,

Thanks for this very useful library.

I am facing a problem with reading and writing binary STLs through. I get an error at the line:

packed = struct.pack(‘@i’, self.data.size)

and:

count, = struct.unpack(‘@i’, b(fh.read(COUNT_SIZE)))

The error is:

TypeError: Struct() argument 1 must be string, not unicode

I tried adding the following line before importing your STL but it did not help:

# -*- coding: utf-8 -*-

The only thing that seems to help is if I manually changed your code to look like this:

packed = struct.pack(‘@i’.encode(‘utf-8’), self.data.size)

and:

count, = struct.unpack(‘@i’.encode(‘utf-8’), b(fh.read(COUNT_SIZE)))

I really dont want to change your library to fit my needs. Is there a better way to resolve this issue?

I am relatively new to Python programming so any help would be appreciated.

Hi Gopinath,

This problem seems similar to one of the currently known bugs: https://github.com/WoLpH/numpy-stl/issues/10

Can you tell me how you are opening the file and which Python version you are using?

Hi Rick,

Thanks for the feedback. I am using Python 2.7.6. I am running this in a Virtual Env inside PyCharm. But I get the error even when I run it on command line.

So you think one of the libraries is the issue?

Thanks

Gopinath

In this case it might also be related to how the file is opened. It seems the library needs some fixes so it’s a bit more versatile in different environments.

How do you read the file exactly?

Hi Rick,

I let your library read it for me. Here is what my code looks like:

def ReadMesh(inputFilePath):

filePath = inputFilePath

mymesh = stl.StlMesh(filePath)

# Do my stuff here

In that case you are probably using a pretty old version of numpy-stl. Can you try upgrading with:

`pip install -U numpy-stl`

And after that use this code instead of the StlMesh:

I just noticed this tutorial was out of date, I’ve just updated it π

Hi Rick,

I just started using the library so I already had 1.5.0. But I took your suggestion and upgraded to 1.5.1 and the problem still exists :-(.

Thanks

Gopinath

hi ,

STL model to calculate the length and breadth, please give me some ideas

thsοΌ

Start by reading your STL file completely, after that it depends what you’re looking for. The simplest form of a length and breadth simply involve a

`mesh.X.max() - mesh.X.min()`

and`mesh.Y.max() - mesh.Y.min()`

Hi, Im normally a matlab user so I used to create stl files similar to how I would plot a surface.

IE surf(x,y,z)–>stlwrite(‘name’,x,y,z) where z=f(x,y)

Just wondering if theres similar syntax for generating an stl from 3 [NxN] arrays in your library?

Thanks

Hi, the library doesn’t really do plots by itself but when matplotlib is involved it’s fairly easy to plot. Just take a look at a few of the examples: https://github.com/WoLpH/numpy-stl#quickstart

I can plot my function. Im just trying to generate an stl from x,y,z arrays.

In that case you probably just have an array of points instead of triangles, correct? Some triangulation needs to be done before it can be converted to an STL file. There’s currently a user with a similar issue but I haven’t had time to look for a solution: https://github.com/WoLpH/numpy-stl/issues/19

It should be possible to have matplotlib triangulate the data into vertices

The relevant manual is the mplot3d tutorial: http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html#tri-surface-plots

Something like this might work:

`triangulation = plot_trisurf(x, y, z)`

OK, I reworked my function’s geometry and now I’ve got 3 1D triangle vertex arrays x, y, z. (IE vertices = (x[0],y[0],z[0])…(x[N],y[N],z[N])

Im still confused on how to feed this data correctly myMesh=mesh.Mesh()

In that case I think the basic examples in the docs should help: http://numpy-stl.readthedocs.org/en/latest/

Specifically: http://numpy-stl.readthedocs.org/en/latest/usage.html#creating-mesh-objects-from-a-list-of-vertices-and-faces

Hello ! Congratulations for your numpy-stl module.

I’ve got a doubt.

Is it possible extraction only the surface, in order to create an 2D image ?

Hi Marco,

While it is possible to extract a 2D image out of a 3D image it’s not something the library does internally. With the matplotlib examples however, you can generate 2D images out of your STL files quite easily.

Thank you for answering.

I got that the numpy-stl module doesnt support directly.

I just want to use the top surface of the mesh to overlay in other images.

I think that I need to “draw” in 2D the triangles, I’m not figuring out how.

For instance; My problem in particular is applied to a PCB.

I have here the stl file, which corresponds to a PCB mesh.

A PCB has different copper layers. I want to apply the mesh to different copper layers, and later calculate all the seconds moments per triangle (each triangle will represent an element)

https://dl.dropboxusercontent.com/u/710615/stlMidpoint.stl

It all depends what you need exactly. Most 3D projections use a single point as the camera of course which might not be what you need here.

If you simply rotate the object so it aligns with one of the axes you should be able to get a simple 2D projection by flattening one of the axes. After that it’s a simple plot.

I did not get it.

If I look from top the mesh (on a stl viewer) it is what I want π

But I’d like extract as coordinates. For instance, in numpy-stl I can extract the vectors and the points (but everything).

I’d like create a numpy array (2D) which would be a binary image. Then it would be able to do operations and/or plots.

That’s really a job for matplotlib, here’s an example of how you can convert the matplotlib output to a numpy array: http://stackoverflow.com/questions/7821518/matplotlib-save-plot-to-numpy-array

Hi !

I’ve tried your suggestion.

However it has returned a huge array (the same size of the huge stl file) with every element 255 .

I’d preffer coordinates or an image π

Probably the image has so much density that it isn’t impossible to draw a proper triangular mesh with your code.

I use this code: http://pastebin.com/Fk3nDyqP

With this stl : https://dl.dropboxusercontent.com/u/710615/stlMidpoint.stl

You might have to increase the resolution quite a bit to make it work properly. But that’s all related to matplotlib and has very little to do with this library π

I don’t know if it will solve.

If you take a look into the stl, it is huge.

I need to plot the triangular mesh of the top or the bottom (it’s the same) in order to use it as mask to other layers, which are 2D images.

The sizes of layers and “mesh mask” should be the same Using matplotlib I would create a distortion on the mesh probably.

you did a great job with your stl module, I don’t know if I’m able to properly use it.

For your case, because of the size and complexity of the model, you probably need to create some sort of flatten function which squashes the model towards a single dimension.

Before you can do that it needs to be properly rotated, but I believe that’s already the case looking at the file. So the only job that remains is drawing the triangles on a 2D grid. That should be simple enough since you have the coordinates but the problem might be that this can cause holes in your render due to rounding errors…

How is it organized your “vectors” object?

For instance, if I have mymesh.vectors, how are they organized?

Perhaps it is possible isolate only x,y from one of the surfaces.

I’m thinking, if I have the xs and ys I’m able to render as coordinates using the opencv module.

Opencv polylines supports numpy arrays.

Or even the Points vector. Is it possible extract coordinates or points to re-organize the numpy arrays in order to load into a figure?

The vectors object is simply a Nx3x3 array where N is the number of triangles and per triangle it’s a collection of 3 points with the X, Y and Z coordinates.

Docs about the data model:

http://numpy-stl.readthedocs.org/en/latest/stl.html#stl.base.BaseMesh.dtype

http://numpy-stl.readthedocs.org/en/latest/stl.html#module-stl.base

For your case you can also use the x and y attributes which is a Nx3 array with the 3 x and y coordinates of a triangle.

Thank you !

I’m close to a dead end.

I even open a stackoverflow questions, without any sucess π

http://stackoverflow.com/questions/36063129/opencv-python-triangular-mesh-having-coordinates

I’ll re-read all information, trying to figure it out.

Hi,

I like your library numpy-stl. Its quick, simple and very easy to use.

I would like to use it for reading stl files from zip file but I didnt find a way how to give Mesh parameters to read it from opened Zipfile instance.

#opening zip file in memory

opened_zipfile = ZipFile(“example.zip”, ‘r’)

#reading specific file

opened_zipfile.open(“cube.stl”)

But how to connect it to Mesh.from_file or I overlook some special function for it?

Many thanks for help

Hi,

It’s actually not as hard as you might expect π

#opening zip file in memory

opened_zipfile = ZipFile(βexample.zipβ, βrβ)

#reading specific file

fh = opened_zipfile.open(βcube.stlβ)

`your_mesh = mesh.Mesh.from_file('cube or some other name', fh=fh)`

Hi! Great library!

I have a software that takes in “surfaces” as a list of nodes(vertices) and elements(faces). I would like to take an .stl I’ve generated using photogrammetry and parse it, but I’m unsure how to determine the elements. Essentially, an element is defined as the 3 corners of each triangle. Is there a way to determine and list out the 3 corners of each triangle?

Thank You!

The mesh objects have several attributes which you can use. In your case it’s probably easiest to use the

`vectors`

attribute. That’s a Nx3x3 matrix where every item is a vertice with 3 triangles within it. And those triangles consist of the X, Y and Z coordinates.The relevaent documentation: http://numpy-stl.readthedocs.io/en/latest/stl.html#stl.base.BaseMesh

nice posting

How to generate STL from x y z? in python ?

can share idea’s , x y z is co-ordinates [n dimension ],

If you simply have coordinates and nothing else you’ll need to do triangulation first. Which is not something this library does but you can let scipy take care of that.

This library is simply for loading/saving/modifying STL files, converting point data to a mesh is a whole different topic.

Can you share sample code?

Can you share small code about triangulation creation from x y z and convert to stl file?

For general usage of te library you can try the readme: https://github.com/WoLpH/numpy-stl/

Or the documentation: https://numpy-stl.readthedocs.io/en/latest/

As for triangulation, I can’t help you there. That’s not my area of expertise.

No use, this code.

in any other api is there STL writ from x y z 3d co-ordiantes using python ?you dont know about this ?