I have the following code which generally works for my trimesh.
box = trimesh.creation.box(bounds=[[min_x, min_y, 0], [max_x, max_y, max_z]])
cut_mesh = trimesh.intersections.slice_mesh_plane(mesh, -box.facets_normal, box.facets_origin)
My problem is that the color of my mesh is not copied with this code as seen in this github issue: https://github.com/mikedh/trimesh/issues/1125.
Is there another easy way to do the slicing including the colors?
With the nearest neighbor method from scipy one can find the nearest vertex and then assign the color or other things to the sliced mesh.
from scipy.spatial import cKDTree
if hasattr(mesh.visual, 'vertex_colors') and mesh.visual.vertex_colors is not None:
tree = cKDTree(mesh.vertices)
_, indices = tree.query(cut_mesh.vertices)
cut_mesh.visual.vertex_colors = mesh.visual.vertex_colors[indices]