import numpy as np
import trimesh
debug_material = trimesh.visual.material.PBRMaterial(baseColorFactor=np.array((255,0,0,80), dtype=np.uint), alphaMode='BLEND')
scene = trimesh.load_mesh('./data/simple.glb')
scene_bounds = scene.bounding_box
max_bounds = scene_bounds.extents.min()
box_count = 2
grid_width = int(max_bounds/box_count)
grid = scene_bounds.sample_grid(step=grid_width)
cm, _ = trimesh.collision.scene_to_collision(scene)
# Sliding box collider
box = trimesh.primitives.Box()
box.apply_scale(grid_width)
cm.add_object('selection', box)
# List of unique objects that were hit
was_hit = set([])
for g in grid:
print('####')
print(g)
matrix = np.eye(4)
matrix[:3, 3] = np.array(g)
debug_box = box.copy()
debug_box.apply_transform(matrix)
debug_box.visual.material = debug_material
hit, hit_list, l = cm.in_collision_single(box, transform=matrix, return_names=True, return_data=True)
if hit:
for item in hit_list:
if item is not 'selection':
mesh_name = scene.graph.get(item)[1]
if item not in was_hit:
was_hit.add(mesh_name)
scene.add_geometry(debug_box)
scene.export(file_obj=open('/tmp/debug.glb', 'wb'), file_type='glb')
print('Total hit %s' % len(was_hit))
print('Total geo %s' % len(scene.geometry))
closest_point
to find points on the lower surface of interest and the indexes for the normals. then use ray.intersects_location with those lower surface points using the closes face normals to those points from the index returned by closest_point. I can tell something is off because if I explicitly set the normals to be [0,0,1], ray.intersects_location return points with different x and y coordinates then the ray origins (the rays should be orthogonal to the xy axis plane). am I missing something?
scene.save_image
?
from trimesh import viewer as trimesh_viewer
data = trimesh_viewer.render_scene(mesh.scene(), resolution=[480,480], visible=True)
You need to install meshlab for that work.
To change the number of triangles in the final mesh, edit the value on line 21:
<Param type="RichInt" value="2000" name="TargetFaceNum"/>