Closed sudip5052 closed 3 years ago
Hi! below is my code to slice the voxelized 3d mesh along the z-axis. My code should be able to take a screenshot of each slice and store it as .png. But I am not able to store each slice image( just store the first slice image only i.e slice1.png). can you please guide me where am I making mistake?
import numpy as np
import trimesh
import pyvista as pv
import matplotlib.pyplot as plt
mesh= pv.read('exhaust.stl')
pv.set_plot_theme('document')
voxels=pv.voxelize(mesh,3)
x=mesh.bounds
c=mesh.center
zmin=x[4]
zmax=x[5]
z_level=np.linspace(zmin+1,zmax,1) #slice thickness=1
l=len(z_level)
#slicethickness=1
#n=(zmax-zmin)/slicethickenes
#line=pv.Line(zmin,zmax,n)
n=1
for i in range(l):
slice=voxels.slice(normal=[0,0,1],origin=[c[0],c[1],z_level[i]])
p = pv.Plotter(off_screen=True)
p.add_mesh(slice,color='lightblue',opacity=0.75)
p.view_xy(negative=False)
p.show(screenshot='slice%d.png'%n)
p.store_image=True
p.close()
n=n+1
Likely your issue is with np.linspace(zmin + 1, zmax, 1)
as that is only producing one value for the z slice. Try changing it to np.linspace(zmin + 1, zmax, n)
where n
is some number larger than 1
Otherwise, there is a method in PyVista to produce all of these slices. See slice_along_axis
Here is code to use that:
import pyvista as pv
mesh = pv.read('exhaust.stl')
voxels = pv.voxelize(mesh, 3)
zslices = voxels.slice_along_axis(n=5, axis='z')
zslices.plot(color='lightblue', opacity=0.75, show_grid=True)
Then if you want each slice in its own image:
for i, slc in enumerate(zslices):
p = pv.Plotter(off_screen=True, notebook=False)
p.add_mesh(slc, color='lightblue', opacity=0.75)
p.view_xy(negative=False)
p.show(screenshot='slice%d.png' % i)
p.store_image = True
p.close()
Which wil produce an image for each slice.
Hi! is it possible to slice the 3d mesh into an N number of slices along any chosen axis at a certain increment height. Slicing is such that instead of showing all slices in the same plot, every slice contour is shown in a different window step by step.