Name | numpy-stl JSON |
Version |
3.1.2
JSON |
| download |
home_page | https://github.com/WoLpH/numpy-stl/ |
Summary | Library to make reading, writing and modifying both binary and ascii STL files easy. |
upload_time | 2024-08-07 23:08:23 |
maintainer | None |
docs_url | https://pythonhosted.org/numpy-stl/ |
author | Rick van Hattem |
requires_python | >3.6.0 |
license | BSD |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
|
numpy-stl
==============================================================================
.. image:: https://github.com/WoLpH/numpy-stl/actions/workflows/main.yml/badge.svg?branch=master
:alt: numpy-stl test status
:target: https://github.com/WoLpH/numpy-stl/actions/workflows/main.yml
.. image:: https://ci.appveyor.com/api/projects/status/cbv7ak2i59wf3lpj?svg=true
:alt: numpy-stl test status
:target: https://ci.appveyor.com/project/WoLpH/numpy-stl
.. image:: https://badge.fury.io/py/numpy-stl.svg
:alt: numpy-stl Pypi version
:target: https://pypi.python.org/pypi/numpy-stl
.. image:: https://coveralls.io/repos/WoLpH/numpy-stl/badge.svg?branch=master
:alt: numpy-stl code coverage
:target: https://coveralls.io/r/WoLpH/numpy-stl?branch=master
.. image:: https://img.shields.io/pypi/pyversions/numpy-stl.svg
Simple library to make working with STL files (and 3D objects in general) fast
and easy.
Due to all operations heavily relying on `numpy` this is one of the fastest
STL editing libraries for Python available.
Security contact information
------------------------------------------------------------------------------
To report a security vulnerability, please use the
`Tidelift security contact <https://tidelift.com/security>`_.
Tidelift will coordinate the fix and disclosure.
Issues
------
If you encounter any issues, make sure you report them `here <https://github.com/WoLpH/numpy-stl/issues>`_. Be sure to search for existing issues however. Many issues have been covered before.
While this project uses `numpy` as it's main dependency, it is not in any way affiliated to the `numpy` project or the NumFocus organisation.
Links
-----
- The source: https://github.com/WoLpH/numpy-stl
- Project page: https://pypi.python.org/pypi/numpy-stl
- Reporting bugs: https://github.com/WoLpH/numpy-stl/issues
- Documentation: http://numpy-stl.readthedocs.org/en/latest/
- My blog: https://wol.ph/
Requirements for installing:
------------------------------------------------------------------------------
- `numpy`_ any recent version
- `python-utils`_ version 1.6 or greater
Installation:
------------------------------------------------------------------------------
`pip install numpy-stl`
Initial usage:
------------------------------------------------------------------------------
After installing the package, you should be able to run the following commands
similar to how you can run `pip`.
.. code-block:: shell
$ stl2bin your_ascii_stl_file.stl new_binary_stl_file.stl
$ stl2ascii your_binary_stl_file.stl new_ascii_stl_file.stl
$ stl your_ascii_stl_file.stl new_binary_stl_file.stl
Contributing:
------------------------------------------------------------------------------
Contributions are always welcome. Please view the guidelines to get started:
https://github.com/WoLpH/numpy-stl/blob/develop/CONTRIBUTING.rst
Quickstart
------------------------------------------------------------------------------
.. code-block:: 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')
Plotting using `matplotlib`_ is equally easy:
------------------------------------------------------------------------------
.. code-block:: python
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
# Create a new plot
figure = pyplot.figure()
axes = figure.add_subplot(projection='3d')
# Load the STL files and add the vectors to the plot
your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
# Auto scale to the mesh size
scale = your_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
.. _numpy: http://numpy.org/
.. _matplotlib: http://matplotlib.org/
.. _python-utils: https://github.com/WoLpH/python-utils
Experimental support for reading 3MF files
------------------------------------------------------------------------------
.. code-block:: python
import pathlib
import stl
path = pathlib.Path('tests/3mf/Moon.3mf')
# Load the 3MF file
for m in stl.Mesh.from_3mf_file(path):
# Do something with the mesh
print('mesh', m)
Note that this is still experimental and may not work for all 3MF files.
Additionally it only allows reading 3mf files, not writing them.
Modifying Mesh objects
------------------------------------------------------------------------------
.. code-block:: python
from stl import mesh
import math
import numpy
# Create 3 faces of a cube
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
# Top of the cube
data['vectors'][0] = numpy.array([[0, 1, 1],
[1, 0, 1],
[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],
[0, 1, 1],
[1, 1, 1]])
# Front face
data['vectors'][2] = numpy.array([[1, 0, 0],
[1, 0, 1],
[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# Left face
data['vectors'][4] = numpy.array([[0, 0, 0],
[1, 0, 0],
[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],
[0, 0, 1],
[1, 0, 1]])
# Since the cube faces are from 0 to 1 we can move it to the middle by
# substracting .5
data['vectors'] -= .5
# Generate 4 different meshes so we can rotate them later
meshes = [mesh.Mesh(data.copy()) for _ in range(4)]
# Rotate 90 degrees over the Y axis
meshes[0].rotate([0.0, 0.5, 0.0], math.radians(90))
# Translate 2 points over the X axis
meshes[1].x += 2
# Rotate 90 degrees over the X axis
meshes[2].rotate([0.5, 0.0, 0.0], math.radians(90))
# Translate 2 points over the X and Y points
meshes[2].x += 2
meshes[2].y += 2
# Rotate 90 degrees over the X and Y axis
meshes[3].rotate([0.5, 0.0, 0.0], math.radians(90))
meshes[3].rotate([0.0, 0.5, 0.0], math.radians(90))
# Translate 2 points over the Y axis
meshes[3].y += 2
# Optionally render the rotated cube faces
from matplotlib import pyplot
from mpl_toolkits import mplot3d
# Create a new plot
figure = pyplot.figure()
axes = figure.add_subplot(projection='3d')
# Render the cube faces
for m in meshes:
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m.vectors))
# Auto scale to the mesh size
scale = numpy.concatenate([m.points for m in meshes]).flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
Extending Mesh objects
------------------------------------------------------------------------------
.. code-block:: python
from stl import mesh
import math
import numpy
# Create 3 faces of a cube
data = numpy.zeros(6, dtype=mesh.Mesh.dtype)
# Top of the cube
data['vectors'][0] = numpy.array([[0, 1, 1],
[1, 0, 1],
[0, 0, 1]])
data['vectors'][1] = numpy.array([[1, 0, 1],
[0, 1, 1],
[1, 1, 1]])
# Front face
data['vectors'][2] = numpy.array([[1, 0, 0],
[1, 0, 1],
[1, 1, 0]])
data['vectors'][3] = numpy.array([[1, 1, 1],
[1, 0, 1],
[1, 1, 0]])
# Left face
data['vectors'][4] = numpy.array([[0, 0, 0],
[1, 0, 0],
[1, 0, 1]])
data['vectors'][5] = numpy.array([[0, 0, 0],
[0, 0, 1],
[1, 0, 1]])
# Since the cube faces are from 0 to 1 we can move it to the middle by
# substracting .5
data['vectors'] -= .5
cube_back = mesh.Mesh(data.copy())
cube_front = mesh.Mesh(data.copy())
# Rotate 90 degrees over the X axis followed by the Y axis followed by the
# X axis
cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))
cube_back.rotate([0.0, 0.5, 0.0], math.radians(90))
cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))
cube = mesh.Mesh(numpy.concatenate([
cube_back.data.copy(),
cube_front.data.copy(),
]))
# Optionally render the rotated cube faces
from matplotlib import pyplot
from mpl_toolkits import mplot3d
# Create a new plot
figure = pyplot.figure()
axes = figure.add_subplot(projection='3d')
# Render the cube
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(cube.vectors))
# Auto scale to the mesh size
scale = cube_back.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
# Show the plot to the screen
pyplot.show()
Creating Mesh objects from a list of vertices and faces
------------------------------------------------------------------------------
.. code-block:: python
import numpy as np
from stl import mesh
# Define the 8 vertices of the cube
vertices = np.array([\
[-1, -1, -1],
[+1, -1, -1],
[+1, +1, -1],
[-1, +1, -1],
[-1, -1, +1],
[+1, -1, +1],
[+1, +1, +1],
[-1, +1, +1]])
# Define the 12 triangles composing the cube
faces = np.array([\
[0,3,1],
[1,3,2],
[0,4,7],
[0,7,3],
[4,5,6],
[4,6,7],
[5,1,2],
[5,2,6],
[2,3,6],
[3,7,6],
[0,1,5],
[0,5,4]])
# Create the mesh
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]
# Write the mesh to file "cube.stl"
cube.save('cube.stl')
Evaluating Mesh properties (Volume, Center of gravity, Inertia)
------------------------------------------------------------------------------
.. code-block:: python
import numpy as np
from stl import mesh
# Using an existing closed stl file:
your_mesh = mesh.Mesh.from_file('some_file.stl')
volume, cog, inertia = your_mesh.get_mass_properties()
print("Volume = {0}".format(volume))
print("Position of the center of gravity (COG) = {0}".format(cog))
print("Inertia matrix at expressed at the COG = {0}".format(inertia[0,:]))
print(" {0}".format(inertia[1,:]))
print(" {0}".format(inertia[2,:]))
Combining multiple STL files
------------------------------------------------------------------------------
.. code-block:: python
import math
import stl
from stl import mesh
import numpy
# find the max dimensions, so we can know the bounding box, getting the height,
# width, length (because these are the step size)...
def find_mins_maxs(obj):
minx = obj.x.min()
maxx = obj.x.max()
miny = obj.y.min()
maxy = obj.y.max()
minz = obj.z.min()
maxz = obj.z.max()
return minx, maxx, miny, maxy, minz, maxz
def translate(_solid, step, padding, multiplier, axis):
if 'x' == axis:
items = 0, 3, 6
elif 'y' == axis:
items = 1, 4, 7
elif 'z' == axis:
items = 2, 5, 8
else:
raise RuntimeError('Unknown axis %r, expected x, y or z' % axis)
# _solid.points.shape == [:, ((x, y, z), (x, y, z), (x, y, z))]
_solid.points[:, items] += (step * multiplier) + (padding * multiplier)
def copy_obj(obj, dims, num_rows, num_cols, num_layers):
w, l, h = dims
copies = []
for layer in range(num_layers):
for row in range(num_rows):
for col in range(num_cols):
# skip the position where original being copied is
if row == 0 and col == 0 and layer == 0:
continue
_copy = mesh.Mesh(obj.data.copy())
# pad the space between objects by 10% of the dimension being
# translated
if col != 0:
translate(_copy, w, w / 10., col, 'x')
if row != 0:
translate(_copy, l, l / 10., row, 'y')
if layer != 0:
translate(_copy, h, h / 10., layer, 'z')
copies.append(_copy)
return copies
# Using an existing stl file:
main_body = mesh.Mesh.from_file('ball_and_socket_simplified_-_main_body.stl')
# rotate along Y
main_body.rotate([0.0, 0.5, 0.0], math.radians(90))
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(main_body)
w1 = maxx - minx
l1 = maxy - miny
h1 = maxz - minz
copies = copy_obj(main_body, (w1, l1, h1), 2, 2, 1)
# I wanted to add another related STL to the final STL
twist_lock = mesh.Mesh.from_file('ball_and_socket_simplified_-_twist_lock.stl')
minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(twist_lock)
w2 = maxx - minx
l2 = maxy - miny
h2 = maxz - minz
translate(twist_lock, w1, w1 / 10., 3, 'x')
copies2 = copy_obj(twist_lock, (w2, l2, h2), 2, 2, 1)
combined = mesh.Mesh(numpy.concatenate([main_body.data, twist_lock.data] +
[copy.data for copy in copies] +
[copy.data for copy in copies2]))
combined.save('combined.stl', mode=stl.Mode.ASCII) # save as ASCII
Known limitations
------------------------------------------------------------------------------
- When speedups are enabled the STL name is automatically converted to
lowercase.
Raw data
{
"_id": null,
"home_page": "https://github.com/WoLpH/numpy-stl/",
"name": "numpy-stl",
"maintainer": null,
"docs_url": "https://pythonhosted.org/numpy-stl/",
"requires_python": ">3.6.0",
"maintainer_email": null,
"keywords": null,
"author": "Rick van Hattem",
"author_email": "Wolph@Wol.ph",
"download_url": "https://files.pythonhosted.org/packages/c2/7a/89fab400cf50cbb95d3b332617797d0a753d866355d51fc0fcfcdc6da8f5/numpy_stl-3.1.2.tar.gz",
"platform": null,
"description": "numpy-stl\n==============================================================================\n\n.. image:: https://github.com/WoLpH/numpy-stl/actions/workflows/main.yml/badge.svg?branch=master\n :alt: numpy-stl test status \n :target: https://github.com/WoLpH/numpy-stl/actions/workflows/main.yml\n\n.. image:: https://ci.appveyor.com/api/projects/status/cbv7ak2i59wf3lpj?svg=true\n :alt: numpy-stl test status \n :target: https://ci.appveyor.com/project/WoLpH/numpy-stl\n\n.. image:: https://badge.fury.io/py/numpy-stl.svg\n :alt: numpy-stl Pypi version \n :target: https://pypi.python.org/pypi/numpy-stl\n\n.. image:: https://coveralls.io/repos/WoLpH/numpy-stl/badge.svg?branch=master\n :alt: numpy-stl code coverage \n :target: https://coveralls.io/r/WoLpH/numpy-stl?branch=master\n\n.. image:: https://img.shields.io/pypi/pyversions/numpy-stl.svg\n\nSimple library to make working with STL files (and 3D objects in general) fast\nand easy.\n\nDue to all operations heavily relying on `numpy` this is one of the fastest\nSTL editing libraries for Python available.\n\nSecurity contact information\n------------------------------------------------------------------------------\n\nTo report a security vulnerability, please use the\n`Tidelift security contact <https://tidelift.com/security>`_.\nTidelift will coordinate the fix and disclosure.\n\nIssues\n------\n\nIf you encounter any issues, make sure you report them `here <https://github.com/WoLpH/numpy-stl/issues>`_. Be sure to search for existing issues however. Many issues have been covered before.\nWhile this project uses `numpy` as it's main dependency, it is not in any way affiliated to the `numpy` project or the NumFocus organisation.\n\nLinks\n-----\n\n - The source: https://github.com/WoLpH/numpy-stl\n - Project page: https://pypi.python.org/pypi/numpy-stl\n - Reporting bugs: https://github.com/WoLpH/numpy-stl/issues\n - Documentation: http://numpy-stl.readthedocs.org/en/latest/\n - My blog: https://wol.ph/\n\nRequirements for installing:\n------------------------------------------------------------------------------\n\n - `numpy`_ any recent version\n - `python-utils`_ version 1.6 or greater\n\nInstallation:\n------------------------------------------------------------------------------\n\n`pip install numpy-stl`\n\nInitial usage:\n------------------------------------------------------------------------------\n\nAfter installing the package, you should be able to run the following commands\nsimilar to how you can run `pip`.\n\n.. code-block:: shell\n \n $ stl2bin your_ascii_stl_file.stl new_binary_stl_file.stl\n $ stl2ascii your_binary_stl_file.stl new_ascii_stl_file.stl\n $ stl your_ascii_stl_file.stl new_binary_stl_file.stl\n\nContributing:\n------------------------------------------------------------------------------\n\nContributions are always welcome. Please view the guidelines to get started:\nhttps://github.com/WoLpH/numpy-stl/blob/develop/CONTRIBUTING.rst\n\nQuickstart\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n import numpy\n from stl import mesh\n\n # Using an existing stl file:\n your_mesh = mesh.Mesh.from_file('some_file.stl')\n\n # Or creating a new mesh (make sure not to overwrite the `mesh` import by\n # naming it `mesh`):\n VERTICE_COUNT = 100\n data = numpy.zeros(VERTICE_COUNT, dtype=mesh.Mesh.dtype)\n your_mesh = mesh.Mesh(data, remove_empty_areas=False)\n\n # The mesh normals (calculated automatically)\n your_mesh.normals\n # The mesh vectors\n your_mesh.v0, your_mesh.v1, your_mesh.v2\n # Accessing individual points (concatenation of v0, v1 and v2 in triplets)\n assert (your_mesh.points[0][0:3] == your_mesh.v0[0]).all()\n assert (your_mesh.points[0][3:6] == your_mesh.v1[0]).all()\n assert (your_mesh.points[0][6:9] == your_mesh.v2[0]).all()\n assert (your_mesh.points[1][0:3] == your_mesh.v0[1]).all()\n\n your_mesh.save('new_stl_file.stl')\n\nPlotting using `matplotlib`_ is equally easy:\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n from stl import mesh\n from mpl_toolkits import mplot3d\n from matplotlib import pyplot\n\n # Create a new plot\n figure = pyplot.figure()\n axes = figure.add_subplot(projection='3d')\n\n # Load the STL files and add the vectors to the plot\n your_mesh = mesh.Mesh.from_file('tests/stl_binary/HalfDonut.stl')\n axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))\n\n # Auto scale to the mesh size\n scale = your_mesh.points.flatten()\n axes.auto_scale_xyz(scale, scale, scale)\n\n # Show the plot to the screen\n pyplot.show()\n\n.. _numpy: http://numpy.org/\n.. _matplotlib: http://matplotlib.org/\n.. _python-utils: https://github.com/WoLpH/python-utils\n\nExperimental support for reading 3MF files\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n import pathlib\n import stl\n\n path = pathlib.Path('tests/3mf/Moon.3mf')\n\n # Load the 3MF file\n for m in stl.Mesh.from_3mf_file(path):\n # Do something with the mesh\n print('mesh', m)\n\nNote that this is still experimental and may not work for all 3MF files.\nAdditionally it only allows reading 3mf files, not writing them.\n\nModifying Mesh objects\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n from stl import mesh\n import math\n import numpy\n\n # Create 3 faces of a cube\n data = numpy.zeros(6, dtype=mesh.Mesh.dtype)\n\n # Top of the cube\n data['vectors'][0] = numpy.array([[0, 1, 1],\n [1, 0, 1],\n [0, 0, 1]])\n data['vectors'][1] = numpy.array([[1, 0, 1],\n [0, 1, 1],\n [1, 1, 1]])\n # Front face\n data['vectors'][2] = numpy.array([[1, 0, 0],\n [1, 0, 1],\n [1, 1, 0]])\n data['vectors'][3] = numpy.array([[1, 1, 1],\n [1, 0, 1],\n [1, 1, 0]])\n # Left face\n data['vectors'][4] = numpy.array([[0, 0, 0],\n [1, 0, 0],\n [1, 0, 1]])\n data['vectors'][5] = numpy.array([[0, 0, 0],\n [0, 0, 1],\n [1, 0, 1]])\n\n # Since the cube faces are from 0 to 1 we can move it to the middle by\n # substracting .5\n data['vectors'] -= .5\n\n # Generate 4 different meshes so we can rotate them later\n meshes = [mesh.Mesh(data.copy()) for _ in range(4)]\n\n # Rotate 90 degrees over the Y axis\n meshes[0].rotate([0.0, 0.5, 0.0], math.radians(90))\n\n # Translate 2 points over the X axis\n meshes[1].x += 2\n\n # Rotate 90 degrees over the X axis\n meshes[2].rotate([0.5, 0.0, 0.0], math.radians(90))\n # Translate 2 points over the X and Y points\n meshes[2].x += 2\n meshes[2].y += 2\n\n # Rotate 90 degrees over the X and Y axis\n meshes[3].rotate([0.5, 0.0, 0.0], math.radians(90))\n meshes[3].rotate([0.0, 0.5, 0.0], math.radians(90))\n # Translate 2 points over the Y axis\n meshes[3].y += 2\n\n\n # Optionally render the rotated cube faces\n from matplotlib import pyplot\n from mpl_toolkits import mplot3d\n\n # Create a new plot\n figure = pyplot.figure()\n axes = figure.add_subplot(projection='3d')\n\n # Render the cube faces\n for m in meshes:\n axes.add_collection3d(mplot3d.art3d.Poly3DCollection(m.vectors))\n\n # Auto scale to the mesh size\n scale = numpy.concatenate([m.points for m in meshes]).flatten()\n axes.auto_scale_xyz(scale, scale, scale)\n\n # Show the plot to the screen\n pyplot.show()\n\nExtending Mesh objects\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n from stl import mesh\n import math\n import numpy\n\n # Create 3 faces of a cube\n data = numpy.zeros(6, dtype=mesh.Mesh.dtype)\n\n # Top of the cube\n data['vectors'][0] = numpy.array([[0, 1, 1],\n [1, 0, 1],\n [0, 0, 1]])\n data['vectors'][1] = numpy.array([[1, 0, 1],\n [0, 1, 1],\n [1, 1, 1]])\n # Front face\n data['vectors'][2] = numpy.array([[1, 0, 0],\n [1, 0, 1],\n [1, 1, 0]])\n data['vectors'][3] = numpy.array([[1, 1, 1],\n [1, 0, 1],\n [1, 1, 0]])\n # Left face\n data['vectors'][4] = numpy.array([[0, 0, 0],\n [1, 0, 0],\n [1, 0, 1]])\n data['vectors'][5] = numpy.array([[0, 0, 0],\n [0, 0, 1],\n [1, 0, 1]])\n\n # Since the cube faces are from 0 to 1 we can move it to the middle by\n # substracting .5\n data['vectors'] -= .5\n\n cube_back = mesh.Mesh(data.copy())\n cube_front = mesh.Mesh(data.copy())\n\n # Rotate 90 degrees over the X axis followed by the Y axis followed by the\n # X axis\n cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))\n cube_back.rotate([0.0, 0.5, 0.0], math.radians(90))\n cube_back.rotate([0.5, 0.0, 0.0], math.radians(90))\n\n cube = mesh.Mesh(numpy.concatenate([\n cube_back.data.copy(),\n cube_front.data.copy(),\n ]))\n\n # Optionally render the rotated cube faces\n from matplotlib import pyplot\n from mpl_toolkits import mplot3d\n\n # Create a new plot\n figure = pyplot.figure()\n axes = figure.add_subplot(projection='3d')\n\n # Render the cube\n axes.add_collection3d(mplot3d.art3d.Poly3DCollection(cube.vectors))\n\n # Auto scale to the mesh size\n scale = cube_back.points.flatten()\n axes.auto_scale_xyz(scale, scale, scale)\n\n # Show the plot to the screen\n pyplot.show()\n\nCreating Mesh objects from a list of vertices and faces\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n import numpy as np\n from stl import mesh\n\n # Define the 8 vertices of the cube\n vertices = np.array([\\\n [-1, -1, -1],\n [+1, -1, -1],\n [+1, +1, -1],\n [-1, +1, -1],\n [-1, -1, +1],\n [+1, -1, +1],\n [+1, +1, +1],\n [-1, +1, +1]])\n # Define the 12 triangles composing the cube\n faces = np.array([\\\n [0,3,1],\n [1,3,2],\n [0,4,7],\n [0,7,3],\n [4,5,6],\n [4,6,7],\n [5,1,2],\n [5,2,6],\n [2,3,6],\n [3,7,6],\n [0,1,5],\n [0,5,4]])\n\n # Create the mesh\n cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))\n for i, f in enumerate(faces):\n for j in range(3):\n cube.vectors[i][j] = vertices[f[j],:]\n\n # Write the mesh to file \"cube.stl\"\n cube.save('cube.stl')\n\n\nEvaluating Mesh properties (Volume, Center of gravity, Inertia)\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n import numpy as np\n from stl import mesh\n\n # Using an existing closed stl file:\n your_mesh = mesh.Mesh.from_file('some_file.stl')\n\n volume, cog, inertia = your_mesh.get_mass_properties()\n print(\"Volume = {0}\".format(volume))\n print(\"Position of the center of gravity (COG) = {0}\".format(cog))\n print(\"Inertia matrix at expressed at the COG = {0}\".format(inertia[0,:]))\n print(\" {0}\".format(inertia[1,:]))\n print(\" {0}\".format(inertia[2,:]))\n\nCombining multiple STL files\n------------------------------------------------------------------------------\n\n.. code-block:: python\n\n import math\n import stl\n from stl import mesh\n import numpy\n\n\n # find the max dimensions, so we can know the bounding box, getting the height,\n # width, length (because these are the step size)...\n def find_mins_maxs(obj):\n minx = obj.x.min()\n maxx = obj.x.max()\n miny = obj.y.min()\n maxy = obj.y.max()\n minz = obj.z.min()\n maxz = obj.z.max()\n return minx, maxx, miny, maxy, minz, maxz\n\n\n def translate(_solid, step, padding, multiplier, axis):\n if 'x' == axis:\n items = 0, 3, 6\n elif 'y' == axis:\n items = 1, 4, 7\n elif 'z' == axis:\n items = 2, 5, 8\n else:\n raise RuntimeError('Unknown axis %r, expected x, y or z' % axis)\n\n # _solid.points.shape == [:, ((x, y, z), (x, y, z), (x, y, z))]\n _solid.points[:, items] += (step * multiplier) + (padding * multiplier)\n\n\n def copy_obj(obj, dims, num_rows, num_cols, num_layers):\n w, l, h = dims\n copies = []\n for layer in range(num_layers):\n for row in range(num_rows):\n for col in range(num_cols):\n # skip the position where original being copied is\n if row == 0 and col == 0 and layer == 0:\n continue\n _copy = mesh.Mesh(obj.data.copy())\n # pad the space between objects by 10% of the dimension being\n # translated\n if col != 0:\n translate(_copy, w, w / 10., col, 'x')\n if row != 0:\n translate(_copy, l, l / 10., row, 'y')\n if layer != 0:\n translate(_copy, h, h / 10., layer, 'z')\n copies.append(_copy)\n return copies\n\n # Using an existing stl file:\n main_body = mesh.Mesh.from_file('ball_and_socket_simplified_-_main_body.stl')\n\n # rotate along Y\n main_body.rotate([0.0, 0.5, 0.0], math.radians(90))\n\n minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(main_body)\n w1 = maxx - minx\n l1 = maxy - miny\n h1 = maxz - minz\n copies = copy_obj(main_body, (w1, l1, h1), 2, 2, 1)\n\n # I wanted to add another related STL to the final STL\n twist_lock = mesh.Mesh.from_file('ball_and_socket_simplified_-_twist_lock.stl')\n minx, maxx, miny, maxy, minz, maxz = find_mins_maxs(twist_lock)\n w2 = maxx - minx\n l2 = maxy - miny\n h2 = maxz - minz\n translate(twist_lock, w1, w1 / 10., 3, 'x')\n copies2 = copy_obj(twist_lock, (w2, l2, h2), 2, 2, 1)\n combined = mesh.Mesh(numpy.concatenate([main_body.data, twist_lock.data] +\n [copy.data for copy in copies] +\n [copy.data for copy in copies2]))\n\n combined.save('combined.stl', mode=stl.Mode.ASCII) # save as ASCII\n\nKnown limitations\n------------------------------------------------------------------------------\n\n - When speedups are enabled the STL name is automatically converted to\n lowercase.\n",
"bugtrack_url": null,
"license": "BSD",
"summary": "Library to make reading, writing and modifying both binary and ascii STL files easy.",
"version": "3.1.2",
"project_urls": {
"Homepage": "https://github.com/WoLpH/numpy-stl/"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "13a14b6e01765f1709483b54ad2241ad5d0c5cfb2dc62800acde8ea26fde43c8",
"md5": "c2236eae279f0176fe6bcca15a53bed8",
"sha256": "a55288340c837378bf44753a1c595c6823312995acda97f27ed04db4ff1d25f3"
},
"downloads": -1,
"filename": "numpy_stl-3.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c2236eae279f0176fe6bcca15a53bed8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">3.6.0",
"size": 20039,
"upload_time": "2024-08-07T23:08:20",
"upload_time_iso_8601": "2024-08-07T23:08:20.298198Z",
"url": "https://files.pythonhosted.org/packages/13/a1/4b6e01765f1709483b54ad2241ad5d0c5cfb2dc62800acde8ea26fde43c8/numpy_stl-3.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c27a89fab400cf50cbb95d3b332617797d0a753d866355d51fc0fcfcdc6da8f5",
"md5": "2fe1a23e65863f628b2a807266178d73",
"sha256": "72b46950dfa3642df1c7b873cfa78a548533724b907478c567db42fdf57ee3d2"
},
"downloads": -1,
"filename": "numpy_stl-3.1.2.tar.gz",
"has_sig": false,
"md5_digest": "2fe1a23e65863f628b2a807266178d73",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">3.6.0",
"size": 821613,
"upload_time": "2024-08-07T23:08:23",
"upload_time_iso_8601": "2024-08-07T23:08:23.356289Z",
"url": "https://files.pythonhosted.org/packages/c2/7a/89fab400cf50cbb95d3b332617797d0a753d866355d51fc0fcfcdc6da8f5/numpy_stl-3.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-07 23:08:23",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "WoLpH",
"github_project": "numpy-stl",
"travis_ci": false,
"coveralls": true,
"github_actions": true,
"appveyor": true,
"tox": true,
"lcname": "numpy-stl"
}