I thought it would be easy if the things to be modeled with a 3D printer became a pattern and I could create a model without permission by entering numbers, but I found out what is called numpy-stl, so I summarized it as a memo.
A rough translation of the official document looks like this.
A simple library for working with STL files (as well as common 3D models) quickly and easily. All operations rely heavily on numpy and are one of the fastest STL libraries for Python.
In short, it's a library where you can create 3D models like numpy and play with existing STL files.
--numpy-stl project page https://pypi.org/project/numpy-stl/ --numpy-stl documentation https://numpy-stl.readthedocs.io/en/latest/ --3D printing x Python ~ 3D printing approaching from code ~ https://www.slideshare.net/TakuroWada/3dpython3d
Basically, pip is OK. This time, I ran it on macOS Catalina 10.15.6, python 3.7.7.
install
pip3 install numpy-stl
If you want to check the generated 3D model, also install the following libraries.
install
pip3 install mpl_toolkits
pip3 install matplotlib
I will add it when I feel like rotating. (However, if you google with numpy-stl, you will see quite a few articles like that.)
I made it a function because it was a big deal. Enter the size in the arguments scale_x, scale_y, scale_z. Depending on the unit system, you should be able to create a cube with a height and width of 1 in the default state with no arguments.
cube_model.py
import numpy as np
from stl import mesh
def cube_model(scaleX=1, scaleY=1, scaleZ=1):
scaleX = scaleX / 2
scaleY = scaleY / 2
scaleZ = scaleZ / 2
vertices = np.array([\
[-1*scaleX, -1*scaleY, -1*scaleZ],
[+1*scaleX, -1*scaleY, -1*scaleZ],
[+1*scaleX, +1*scaleY, -1*scaleZ],
[-1*scaleX, +1*scaleY, -1*scaleZ],
[-1*scaleX, -1*scaleY, +1*scaleZ],
[+1*scaleX, -1*scaleY, +1*scaleZ],
[+1*scaleX, +1*scaleY, +1*scaleZ],
[-1*scaleX, +1*scaleY, +1*scaleZ]])
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]])
cube = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype))
cube.remove_duplicate_polygons=True
for i, f in enumerate(faces):
for j in range(3):
cube.vectors[i][j] = vertices[f[j],:]
return cube
Click here for the program to load
test_plot.py
import numpy as np
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from cube_model import cube_model
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
your_mesh = cube_model(10,10,10)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
scale = your_mesh.points.flatten()
print(scale)
axes.auto_scale_xyz(scale, scale, scale)
pyplot.show()
Specify your own STL file in your_stl_model.stl.
read_stl_file.py
import numpy as np
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
your_mesh = mesh.Mesh.from_file('your_stl_model.stl')
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
scale = cube_comb.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
pyplot.show()
It is used to align the center of the model with (0,0,0) when the read STL is flying in a strange direction. In the argument my_mesh, put the mesh object when the STL file etc. is read.
mesh_location_zero.py
import numpy as np
from stl import mesh
def mesh_location_zero(my_mesh):
midPosRel = (my_mesh.max_ - my_mesh.min_)/2
my_mesh.x = my_mesh.x - (midPosRel[0] + my_mesh.min_[0])
my_mesh.y = my_mesh.y - (midPosRel[1] + my_mesh.min_[1])
my_mesh.z = my_mesh.z - (midPosRel[2] + my_mesh.min_[2])
return my_mesh
In order to update the members in the mesh object, this is also executed when moving, including adjusting the coordinate system, or expanding, rotating, or changing the model. The arguments are the same.
mesh_update.py
import numpy as np
from stl import mesh
def mesh_update(my_mesh):
my_mesh.update_areas()
my_mesh.update_max()
my_mesh.update_min()
my_mesh.update_units()
return my_mesh
Enlargement / reduction has also been made into a function. Put your own 3D model in my_mesh, and put 1.0 as 100% in scale_x, scale_y, and scale_z.
mesh_scale.py
import numpy as np
from stl import mesh
def mesh_scale(my_mesh, scale_x, scale_y, scale_z):
my_mesh.x = my_mesh.x * scale_x
my_mesh.y = my_mesh.y * scale_y
my_mesh.z = my_mesh.z * scale_z
return my_mesh
You can move it with mesh.translate. Arguments are specified in numpy.array. (This time, I also use cube_model.py.)
move_model.py
import numpy as np
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from cube_model import cube_model
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
your_mesh = cube_model(5,20,5)
your_mesh.translate(np.array([1,3,1]))
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
scale = cube_comb.points.flatten()
print(scale)
axes.auto_scale_xyz(scale, scale, scale)
pyplot.show()
You can combine models with numpy.concatenate. I'm just selling that I can make a model with numpy. (This time, I also use cube_model.py.)
mesh_scale.py
import numpy as np
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
from cube_model import cube_model
figure = pyplot.figure()
axes = mplot3d.Axes3D(figure)
your_mesh = cube_model(10,10,10)
your_mesh2 = cube_model(5,20,5)
your_mesh2.translate(np.array([1,1,1]))
cube_comb = mesh.Mesh(np.concatenate([
your_mesh.data.copy(),
your_mesh2.data.copy(),
]))
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(cube_comb.vectors))
scale = cube_comb.points.flatten()
print(scale)
axes.auto_scale_xyz(scale, scale, scale)
pyplot.show()
Can be saved with mesh.save. The arguments are the save destination path and file name.
save_model.py
import numpy as np
from stl import mesh
from cube_model import cube_model
your_mesh = cube_model(10,10,10)
your_mesh2 = cube_model(5,20,5)
your_mesh2.translate(np.array([1,1,1]))
cube_comb = mesh.Mesh(np.concatenate([
your_mesh.data.copy(),
your_mesh2.data.copy(),
]))
cube_comb.save('your_model.stl')
I tried to stream the STL data created in the software "XYZprint" used to stream the modeling data with a 3D printer made by XYZprinting. I'm glad that the stacked data can be created properly.
This time I tried to operate numpy-stl. If there are other operations that you might use in general, add them when you feel like it.
Recommended Posts