Spaces:
Runtime error
Runtime error
added a script to get images from .stl files
Browse files- multi_angle_stl.py +39 -0
multi_angle_stl.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from stl import mesh
|
| 2 |
+
from mpl_toolkits import mplot3d
|
| 3 |
+
from matplotlib import pyplot as plt
|
| 4 |
+
|
| 5 |
+
# Load the STL file
|
| 6 |
+
your_mesh = mesh.Mesh.from_file('sample_data.stl')
|
| 7 |
+
|
| 8 |
+
# Define three different viewing angles
|
| 9 |
+
viewing_angles = [(30, 45), (60, 90), (45, 135)]
|
| 10 |
+
|
| 11 |
+
# Iterate over each viewing angle and generate an image
|
| 12 |
+
for i, (elev, azim) in enumerate(viewing_angles, start=1):
|
| 13 |
+
# Create a new plot with a larger figure size
|
| 14 |
+
fig = plt.figure(figsize=(10, 10))
|
| 15 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 16 |
+
|
| 17 |
+
# Add the STL file to the plot
|
| 18 |
+
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
|
| 19 |
+
|
| 20 |
+
# Calculate the limits of the mesh
|
| 21 |
+
max_dim = max(your_mesh.points.flatten())
|
| 22 |
+
min_dim = min(your_mesh.points.flatten())
|
| 23 |
+
|
| 24 |
+
# Set the limits of the plot
|
| 25 |
+
ax.set_xlim([min_dim, max_dim])
|
| 26 |
+
ax.set_ylim([min_dim, max_dim])
|
| 27 |
+
ax.set_zlim([min_dim, max_dim])
|
| 28 |
+
|
| 29 |
+
# Set the viewing angle
|
| 30 |
+
ax.view_init(elev=elev, azim=azim)
|
| 31 |
+
|
| 32 |
+
# Save the plot as an image
|
| 33 |
+
plt.savefig(f'mesh_{i}.png')
|
| 34 |
+
|
| 35 |
+
# Close the plot to avoid memory leaks
|
| 36 |
+
plt.close()
|
| 37 |
+
|
| 38 |
+
# Optional: Show the last generated plot
|
| 39 |
+
# plt.show()
|