-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathview.py
27 lines (23 loc) · 1.22 KB
/
view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from stl import mesh
from mpl_toolkits import mplot3d
from matplotlib import pyplot
def view_stl():
''' The following function renders a 3D geometry mesh in Matplotlib, if helps you visualize a 3D object in a simple and fast way
Params: file_dir = to store the directory location
figure = to create an empty Pyplot figure
axes = subgraph to plot the mesh
view_mesh = stores the 3D mesh from the directory
scale = stores the relative scale of vertices:plot size
'''
file_dir = "REPLACE WITH THE LOCATION OF THE FILE" #example "c:\users\username\..."
figure = pyplot.figure(figsize=(30,30))
axes = mplot3d.Axes3D(figure)
view_mesh = mesh.Mesh.from_file(file_dir)
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))
''' Here we calculated the total number of flat vertices, that are present in a 3D object and then scale it according to the total Figsize of the plot
autoscale allows us to fit the object within the constraints of a PyPlot Graph, and also fixes the object at 0,0,0 coordinates which can help us understand the
true dimensions of the object in inches
'''
scale = view_mesh.points.flatten()
axes.auto_scale_xyz(scale, scale, scale)
pyplot.show()