pythonmatplotlibmatplotlib-3dstl-format

How to display a 3d image of .stl file


I've got a task of reading a stl image and need to display in my python environment. To do that, I have written the code below, but it is not working. When I tried with plt.imshow instead of plt.show(), it shows only vertical column with shape (92520, 3, 3). Is it possible to view the full 3D image I have or not?

from mpl_toolkits import mplot3d
import matplotlib.pyplot as plt
from stl import mesh

# Load the STL mesh
your_mesh = mesh.Mesh.from_file('/content/stlimage.stl')

# Create a new figure
fig = plt.figure()
ax = mplot3d.Axes3D(fig)

# Add the mesh to the plot
ax.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))

# Set plot labels
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

# Show the plot
plt.show()

Solution

  • You can use the open3d package to 3D visualize a .stl file. Here's how you can do it:

    pip install open3d
    
    import open3d as o3d
    
    mesh = o3d.io.read_triangle_mesh("DeathStarTop_Hollow.stl")
    mesh = mesh.compute_vertex_normals()
    o3d.visualization.draw_geometries([mesh], window_name="STL", left=1000, top=200, width=800, height=650)
    

    Output: enter image description here

    You will see an interactable 3D stl file.

    Check out open3d documentation to know more.