'Scaling stl mesh object in Python
I imported a .stl model into Python and plotted it in a 3D plot. However, the mesh model is too small compared to the rest of the figure, which contains various other lines and objects. How can I scale a stl.mesh object in Python?
(I am not using Blender or any other software. This is purely a Python plot that I am trying to create)
Code:
from stl import mesh
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.mplot3d import proj3d
fig = plt.figure()
ax = plt.axes(projection="3d")
my_mesh = mesh.Mesh.from_file('model.stl')
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(my_mesh.vectors))
Solution 1:[1]
Short answer
Just add the scaling factor with multiplication to the mesh.vectors, like:
collection1 = Poly3DCollection(street.vectors * SCALE, linewidths=1, alpha=0.2)
And it will "inflate" the model. Surely numbers between 0 and 1 will "deflate" the object.
Long answer
After pushing here, I discussed this problem with a colleague and found the (very simple) solution.
First the regular way to use a mesh stl file, from numpy-stl:
# load mesh file
street = mesh.Mesh.from_file('../../stls_files/strasen.stl')
figure = plt.figure()
ax = mplot3d.Axes3D(figure)
collection1 = Poly3DCollection(street.vectors, linewidths=1, alpha=0.2)
street_color = [0.2, 0.5, 0.7]
collection1.set_facecolor(street_color)
ax.add_collection3d(collection1)
# Auto scale to the mesh size
scale1 = street.points.flatten()
ax.auto_scale_xyz(scale1, scale1, scale1)
plt.show()
The next box shows the modification:
# load mesh file
street = mesh.Mesh.from_file('../../stls_files/strasen.stl')
figure = plt.figure()
ax = mplot3d.Axes3D(figure)
scale_factor = 4
collection1 = Poly3DCollection(street.vectors * scale_factor, linewidths=1, alpha=0.2)
street_color = [0.2, 0.5, 0.7]
collection1.set_facecolor(street_color)
ax.add_collection3d(collection1)
# Auto scale to the mesh size
scale1 = street.points.flatten()
ax.auto_scale_xyz(scale1, scale1, scale1)
plt.show()
Please note I have just added a factor and multiplication with the mesh.vectors of the stl.Mesh.
With subtraction/addition you can also move the object.
Solution 2:[2]
If you have the ParaView library in your python, you can move, rotate, and scale an STL file and save it in another file. The below code is for scaling:
from paraview.simple import *
def ScaleStl(inputFileName, scaledFileName, scale):
# create a new 'STL Reader'
myStl = STLReader(registrationName='myStl.stl', FileNames=[inputFileName])
# create a new 'Transform'
transform1 = Transform(registrationName='Transform1', Input=myStl)
transform1.Transform = 'Transform'
# Properties modified on transform1.Transform
transform1.Transform.Scale = scale
# save data
SaveData(scaledFileName, proxy=transform1, CellDataArrays=['STLSolidLabeling'], FileType='Ascii')
Use it like:
ScaleStl("body.stl","scaledbody.stl",[0.5,0.5,0.5])
If installing the ParaView library is complicated, the Paraview package comes with its own Python binary, called pvpython which runs the above code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tomerikoo |
| Solution 2 | Sorush |
