SoFunction
Updated on 2025-03-02

How to use the Trimesh library in Python

Use of Python Trimesh library

Trimesh is a pure Python (2.7-3.5+) library for loading and using triangle meshes.

The goal of the library is to provide a fully functional, well-tested Trimesh object that allows for simple manipulation and analysis in the same style as the Polygon object in the Shapely library.

Here are some related usage methods!

Model loading

mesh = (obj_path)

vertices and faces output

v =  
f = 
#The v and f format obtained in this way are trimesh built-in formats and cannot be used directly for other calculations. They need to be converted to numpyv1 = (v)
f1 = (f)

vertices and faces are converted into models and displayed

obj = (vertices = v1, faces = f1)
()
#Then clickaDisplay coordinate axes,wShow only the model lines

Sampling surface points and calculating nearest points

"""
 tgt_mesh:sampled mesh
 sampled_points_num: Number of sampled points
 gt_surface_pts: sampling point coordinates
 face_index: the face index corresponding to the sampling point
 """
gt_surface_pts, face_index = .sample_surface_even(tgt_mesh, sampled_points_num)
"""
 src_mesh: target mesh
 gt_surface_pts: The point to calculate the nearest point
 pred_surface_pts: The nearest point obtained on the triangle sheet
 dist_pred_gt: The distance from point to the nearest triangle
 triangle_id: index of the most recent triangle facet
 """
pred_surface_pts, dist_pred_gt, triangle_id = .closest_point(
            src_mesh, gt_surface_pts)

Transform the mesh with homogeneous transformation matrix

# matrix((4,4)float) - Homogeneous transformation matrixmesh = (obj_path)
mesh = mesh.apply_transform(matrix)

Other Trimesh functions can be explained in detail and used.Search the official website

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.