stl package

stl.Mesh

class stl.Mesh(data, calculate_normals=True, remove_empty_areas=False, remove_duplicate_polygons=<RemoveDuplicates.NONE: 0>, name='', speedups=True, **kwargs)[source]

Bases: stl.stl.BaseStl

areas

Mesh areas

debug(msg, *args, **kwargs)

Log a message with severity ‘DEBUG’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

dtype = dtype([('normals', '<f4', (3,)), ('vectors', '<f4', (3, 3)), ('attr', '<u2', (1,))])
error(msg, *args, **kwargs)

Log a message with severity ‘ERROR’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

exception(msg, *args, exc_info=True, **kwargs)

Log a message with severity ‘ERROR’ on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.

from_file(filename, calculate_normals=True, fh=None, mode=<Mode.AUTOMATIC: 0>, speedups=True, **kwargs)

Load a mesh from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
from_multi_file(filename, calculate_normals=True, fh=None, mode=<Mode.ASCII: 1>, speedups=True, **kwargs)

Load multiple meshes from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
get_mass_properties()
Evaluate and return a tuple with the following elements:
  • the volume
  • the position of the center of gravity (COG)
  • the inertia matrix expressed at the COG

Documentation can be found here: http://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf

info(msg, *args, **kwargs)

Log a message with severity ‘INFO’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
load(fh, mode=<Mode.AUTOMATIC: 0>, speedups=True)

Load Mesh from STL file

Automatically detects binary versus ascii STL files.

Parameters:
  • fh (file) – The file handle to open
  • mode (int) – Automatically detect the filetype or force binary
log(level, msg, *args, **kwargs)

Log ‘msg % args’ with the integer severity ‘level’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

logger = <logging.Logger object>
max_

Mesh maximum value

min_

Mesh minimum value

remove_duplicate_polygons(data, value=<RemoveDuplicates.SINGLE: 1>)
remove_empty_areas(data)
rotate(axis, theta, point=None)

Rotate the matrix over the given axis by the given theta (angle)

Uses the rotation_matrix() in the background.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
  • point (numpy.array) – Rotation point so manual translation is not required
rotation_matrix(axis, theta)

Generate a rotation matrix to Rotate the matrix over the given axis by the given theta (angle)

Uses the Euler-Rodrigues formula for fast rotations.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
save(filename, fh=None, mode=<Mode.AUTOMATIC: 0>, update_normals=True)

Save the STL to a (binary) file

If mode is AUTOMATIC an ASCII file will be written if the output is a TTY and a BINARY file otherwise.

Parameters:
  • filename (str) – The file to load
  • fh (file) – The file handle to open
  • mode (int) – The mode to write, default is AUTOMATIC.
  • update_normals (bool) – Whether to update the normals
transform(matrix)

Transform the mesh with a rotation and a translation stored in a single 4x4 matrix

Parameters:matrix (numpy.array) – Transform matrix with shape (4, 4), where matrix[0:3, 0:3] represents the rotation part of the transformation matrix[0:3, 3] represents the translation part of the transformation
translate(translation)

Translate the mesh in the three directions

Parameters:translation (numpy.array) – Translation vector (x, y, z)
units

Mesh unit vectors

update_areas()
update_max()
update_min()
update_normals()

Update the normals for all points

update_units()
values() → an object providing a view on D's values
warning(msg, *args, **kwargs)

Log a message with severity ‘WARNING’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

stl.main module

stl.main.main()[source]
stl.main.to_ascii()[source]
stl.main.to_binary()[source]

stl.base module

stl.base.AREA_SIZE_THRESHOLD = 0

When removing empty areas, remove areas that are smaller than this

class stl.base.BaseMesh(data, calculate_normals=True, remove_empty_areas=False, remove_duplicate_polygons=<RemoveDuplicates.NONE: 0>, name='', speedups=True, **kwargs)[source]

Bases: python_utils.logger.Logged, collections.abc.Mapping

Mesh object with easy access to the vectors through v0, v1 and v2. The normals, areas, min, max and units are calculated automatically.

Parameters:
  • data (numpy.array) – The data for this mesh
  • calculate_normals (bool) – Whether to calculate the normals
  • remove_empty_areas (bool) – Whether to remove triangles with 0 area (due to rounding errors for example)
Variables:
>>> data = numpy.zeros(10, dtype=BaseMesh.dtype)
>>> mesh = BaseMesh(data, remove_empty_areas=False)
>>> # Increment vector 0 item 0
>>> mesh.v0[0] += 1
>>> mesh.v1[0] += 2
>>> # Check item 0 (contains v0, v1 and v2)
>>> mesh[0]
array([ 1.,  1.,  1.,  2.,  2.,  2.,  0.,  0.,  0.], dtype=float32)
>>> mesh.vectors[0] 
array([[ 1.,  1.,  1.],
       [ 2.,  2.,  2.],
       [ 0.,  0.,  0.]], dtype=float32)
>>> mesh.v0[0]
array([ 1.,  1.,  1.], dtype=float32)
>>> mesh.points[0]
array([ 1.,  1.,  1.,  2.,  2.,  2.,  0.,  0.,  0.], dtype=float32)
>>> mesh.data[0] 
([0.0, 0.0, 0.0],
[[1.0, 1.0, 1.0], [2.0, 2.0, 2.0], [0.0, 0.0, 0.0]],
[0])
>>> mesh.x[0]
array([ 1.,  2.,  0.], dtype=float32)
>>> mesh[0] = 3
>>> mesh[0]
array([ 3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.,  3.], dtype=float32)
>>> len(mesh) == len(list(mesh))
True
>>> (mesh.min_ < mesh.max_).all()
True
>>> mesh.update_normals()
>>> mesh.units.sum()
0.0
>>> mesh.v0[:] = mesh.v1[:] = mesh.v2[:] = 0
>>> mesh.points.sum()
0.0
areas

Mesh areas

debug(msg, *args, **kwargs)

Log a message with severity ‘DEBUG’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

dtype = dtype([('normals', '<f4', (3,)), ('vectors', '<f4', (3, 3)), ('attr', '<u2', (1,))])
  • normals: numpy.float32(), (3, )
  • vectors: numpy.float32(), (3, 3)
  • attr: numpy.uint16(), (1, )
error(msg, *args, **kwargs)

Log a message with severity ‘ERROR’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

exception(msg, *args, exc_info=True, **kwargs)

Log a message with severity ‘ERROR’ on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
get_mass_properties()[source]
Evaluate and return a tuple with the following elements:
  • the volume
  • the position of the center of gravity (COG)
  • the inertia matrix expressed at the COG

Documentation can be found here: http://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf

info(msg, *args, **kwargs)

Log a message with severity ‘INFO’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
log(level, msg, *args, **kwargs)

Log ‘msg % args’ with the integer severity ‘level’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

logger = <logging.Logger object>
max_

Mesh maximum value

min_

Mesh minimum value

classmethod remove_duplicate_polygons(data, value=<RemoveDuplicates.SINGLE: 1>)[source]
classmethod remove_empty_areas(data)[source]
rotate(axis, theta, point=None)[source]

Rotate the matrix over the given axis by the given theta (angle)

Uses the rotation_matrix() in the background.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
  • point (numpy.array) – Rotation point so manual translation is not required
classmethod rotation_matrix(axis, theta)[source]

Generate a rotation matrix to Rotate the matrix over the given axis by the given theta (angle)

Uses the Euler-Rodrigues formula for fast rotations.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
transform(matrix)[source]

Transform the mesh with a rotation and a translation stored in a single 4x4 matrix

Parameters:matrix (numpy.array) – Transform matrix with shape (4, 4), where matrix[0:3, 0:3] represents the rotation part of the transformation matrix[0:3, 3] represents the translation part of the transformation
translate(translation)[source]

Translate the mesh in the three directions

Parameters:translation (numpy.array) – Translation vector (x, y, z)
units

Mesh unit vectors

update_areas()[source]
update_max()[source]
update_min()[source]
update_normals()[source]

Update the normals for all points

update_units()[source]
values() → an object providing a view on D's values
warning(msg, *args, **kwargs)

Log a message with severity ‘WARNING’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

stl.base.DIMENSIONS = 3

Dimensions used in a vector

class stl.base.Dimension[source]

Bases: enum.IntEnum

An enumeration.

X = 0

X index (for example, mesh.v0[0][X])

Y = 1

Y index (for example, mesh.v0[0][Y])

Z = 2

Z index (for example, mesh.v0[0][Z])

class stl.base.RemoveDuplicates[source]

Bases: enum.Enum

Choose whether to remove no duplicates, leave only a single of the duplicates or remove all duplicates (leaving holes).

ALL = 2
NONE = 0
SINGLE = 1
stl.base.VECTORS = 3

Vectors in a point

stl.base.logged(class_)[source]

stl.mesh module

class stl.mesh.Mesh(data, calculate_normals=True, remove_empty_areas=False, remove_duplicate_polygons=<RemoveDuplicates.NONE: 0>, name='', speedups=True, **kwargs)[source]

Bases: stl.stl.BaseStl

areas

Mesh areas

debug(msg, *args, **kwargs)

Log a message with severity ‘DEBUG’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

dtype = dtype([('normals', '<f4', (3,)), ('vectors', '<f4', (3, 3)), ('attr', '<u2', (1,))])
error(msg, *args, **kwargs)

Log a message with severity ‘ERROR’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

exception(msg, *args, exc_info=True, **kwargs)

Log a message with severity ‘ERROR’ on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.

from_file(filename, calculate_normals=True, fh=None, mode=<Mode.AUTOMATIC: 0>, speedups=True, **kwargs)

Load a mesh from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
from_multi_file(filename, calculate_normals=True, fh=None, mode=<Mode.ASCII: 1>, speedups=True, **kwargs)

Load multiple meshes from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
get_mass_properties()
Evaluate and return a tuple with the following elements:
  • the volume
  • the position of the center of gravity (COG)
  • the inertia matrix expressed at the COG

Documentation can be found here: http://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf

info(msg, *args, **kwargs)

Log a message with severity ‘INFO’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
load(fh, mode=<Mode.AUTOMATIC: 0>, speedups=True)

Load Mesh from STL file

Automatically detects binary versus ascii STL files.

Parameters:
  • fh (file) – The file handle to open
  • mode (int) – Automatically detect the filetype or force binary
log(level, msg, *args, **kwargs)

Log ‘msg % args’ with the integer severity ‘level’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

logger = <logging.Logger object>
max_

Mesh maximum value

min_

Mesh minimum value

remove_duplicate_polygons(data, value=<RemoveDuplicates.SINGLE: 1>)
remove_empty_areas(data)
rotate(axis, theta, point=None)

Rotate the matrix over the given axis by the given theta (angle)

Uses the rotation_matrix() in the background.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
  • point (numpy.array) – Rotation point so manual translation is not required
rotation_matrix(axis, theta)

Generate a rotation matrix to Rotate the matrix over the given axis by the given theta (angle)

Uses the Euler-Rodrigues formula for fast rotations.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
save(filename, fh=None, mode=<Mode.AUTOMATIC: 0>, update_normals=True)

Save the STL to a (binary) file

If mode is AUTOMATIC an ASCII file will be written if the output is a TTY and a BINARY file otherwise.

Parameters:
  • filename (str) – The file to load
  • fh (file) – The file handle to open
  • mode (int) – The mode to write, default is AUTOMATIC.
  • update_normals (bool) – Whether to update the normals
transform(matrix)

Transform the mesh with a rotation and a translation stored in a single 4x4 matrix

Parameters:matrix (numpy.array) – Transform matrix with shape (4, 4), where matrix[0:3, 0:3] represents the rotation part of the transformation matrix[0:3, 3] represents the translation part of the transformation
translate(translation)

Translate the mesh in the three directions

Parameters:translation (numpy.array) – Translation vector (x, y, z)
units

Mesh unit vectors

update_areas()
update_max()
update_min()
update_normals()

Update the normals for all points

update_units()
values() → an object providing a view on D's values
warning(msg, *args, **kwargs)

Log a message with severity ‘WARNING’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

stl.stl module

stl.stl.BUFFER_SIZE = 4096

Amount of bytes to read while using buffered reading

class stl.stl.BaseStl(data, calculate_normals=True, remove_empty_areas=False, remove_duplicate_polygons=<RemoveDuplicates.NONE: 0>, name='', speedups=True, **kwargs)[source]

Bases: stl.base.BaseMesh

areas

Mesh areas

debug(msg, *args, **kwargs)

Log a message with severity ‘DEBUG’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

dtype = dtype([('normals', '<f4', (3,)), ('vectors', '<f4', (3, 3)), ('attr', '<u2', (1,))])
error(msg, *args, **kwargs)

Log a message with severity ‘ERROR’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

exception(msg, *args, exc_info=True, **kwargs)

Log a message with severity ‘ERROR’ on the root logger, with exception information. If the logger has no handlers, basicConfig() is called to add a console handler with a pre-defined format.

classmethod from_file(filename, calculate_normals=True, fh=None, mode=<Mode.AUTOMATIC: 0>, speedups=True, **kwargs)[source]

Load a mesh from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
classmethod from_multi_file(filename, calculate_normals=True, fh=None, mode=<Mode.ASCII: 1>, speedups=True, **kwargs)[source]

Load multiple meshes from a STL file

Parameters:
  • filename (str) – The file to load
  • calculate_normals (bool) – Whether to update the normals
  • fh (file) – The file handle to open
  • **kwargs (dict) – The same as for stl.mesh.Mesh
get(k[, d]) → D[k] if k in D, else d. d defaults to None.
get_mass_properties()
Evaluate and return a tuple with the following elements:
  • the volume
  • the position of the center of gravity (COG)
  • the inertia matrix expressed at the COG

Documentation can be found here: http://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf

info(msg, *args, **kwargs)

Log a message with severity ‘INFO’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
classmethod load(fh, mode=<Mode.AUTOMATIC: 0>, speedups=True)[source]

Load Mesh from STL file

Automatically detects binary versus ascii STL files.

Parameters:
  • fh (file) – The file handle to open
  • mode (int) – Automatically detect the filetype or force binary
log(level, msg, *args, **kwargs)

Log ‘msg % args’ with the integer severity ‘level’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

logger = <logging.Logger object>
max_

Mesh maximum value

min_

Mesh minimum value

remove_duplicate_polygons(data, value=<RemoveDuplicates.SINGLE: 1>)
remove_empty_areas(data)
rotate(axis, theta, point=None)

Rotate the matrix over the given axis by the given theta (angle)

Uses the rotation_matrix() in the background.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
  • point (numpy.array) – Rotation point so manual translation is not required
rotation_matrix(axis, theta)

Generate a rotation matrix to Rotate the matrix over the given axis by the given theta (angle)

Uses the Euler-Rodrigues formula for fast rotations.

Parameters:
  • axis (numpy.array) – Axis to rotate over (x, y, z)
  • theta (float) – Rotation angle in radians, use math.radians to convert degrees to radians if needed.
save(filename, fh=None, mode=<Mode.AUTOMATIC: 0>, update_normals=True)[source]

Save the STL to a (binary) file

If mode is AUTOMATIC an ASCII file will be written if the output is a TTY and a BINARY file otherwise.

Parameters:
  • filename (str) – The file to load
  • fh (file) – The file handle to open
  • mode (int) – The mode to write, default is AUTOMATIC.
  • update_normals (bool) – Whether to update the normals
transform(matrix)

Transform the mesh with a rotation and a translation stored in a single 4x4 matrix

Parameters:matrix (numpy.array) – Transform matrix with shape (4, 4), where matrix[0:3, 0:3] represents the rotation part of the transformation matrix[0:3, 3] represents the translation part of the transformation
translate(translation)

Translate the mesh in the three directions

Parameters:translation (numpy.array) – Translation vector (x, y, z)
units

Mesh unit vectors

update_areas()
update_max()
update_min()
update_normals()

Update the normals for all points

update_units()
values() → an object providing a view on D's values
warning(msg, *args, **kwargs)

Log a message with severity ‘WARNING’ on the root logger. If the logger has no handlers, call basicConfig() to add a console handler with a pre-defined format.

stl.stl.COUNT_SIZE = 4

The amount of bytes in the count field

stl.stl.HEADER_SIZE = 80

The amount of bytes in the header field

stl.stl.MAX_COUNT = 100000000.0

The maximum amount of triangles we can read from binary files

class stl.stl.Mode[source]

Bases: enum.IntEnum

An enumeration.

ASCII = 1

Force writing ASCII

AUTOMATIC = 0

Automatically detect whether the output is a TTY, if so, write ASCII otherwise write BINARY

BINARY = 2

Force writing BINARY