Wolph

Reading/writing 3D STL files with numpy-stl

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:
[bash]pip install numpy-stl
# Or if you don’t have pip available
easy_install numpy-stl
[/bash]
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
[python]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’)[/python]

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

Exit mobile version