Spaces:
Runtime error
Runtime error
found a way to get image from 3d object
Browse files- render_stl.py +28 -0
render_stl.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Create a new plot with a larger figure size
|
| 9 |
+
fig = plt.figure(figsize=(10, 10))
|
| 10 |
+
ax = fig.add_subplot(111, projection='3d')
|
| 11 |
+
|
| 12 |
+
# Add the STL file to the plot
|
| 13 |
+
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
|
| 14 |
+
|
| 15 |
+
# Calculate the limits of the mesh
|
| 16 |
+
max_dim = max(your_mesh.points.flatten())
|
| 17 |
+
min_dim = min(your_mesh.points.flatten())
|
| 18 |
+
|
| 19 |
+
# Set the limits of the plot
|
| 20 |
+
ax.set_xlim([min_dim, max_dim])
|
| 21 |
+
ax.set_ylim([min_dim, max_dim])
|
| 22 |
+
ax.set_zlim([min_dim, max_dim])
|
| 23 |
+
|
| 24 |
+
# Save the plot as an image
|
| 25 |
+
plt.savefig('mesh.png')
|
| 26 |
+
|
| 27 |
+
# Show the plot (optional)
|
| 28 |
+
# plt.show()
|