Trimesh介绍及基本使用
Trimesh是一个纯Python 工具库(支持3.7版本以上),用于加载和使用三角形Mesh网格,支持多种常见的三维数据格式,如二进制/文本格式的STL、Wavefront OBJ、二进制/文本格式的PLY、GLTF/GLB 2.0、3MF、XAML、3DXML等输入及输出。
Trimesh
自带可视化接口,同时支持jupyter-notebook
conda安装Trimesh
conda install -c conda-forge trimesh
官方介绍 https://trimesh.org/install.html
Trimesh的基本使用
# trimesh
import trimesh
import numpy as np
1、三维模型加载模型并显示
Import meshes from binary/ASCII STL, Wavefront OBJ, ASCII OFF, binary/ASCII PLY, GLTF/GLB 2.0, 3MF, XAML, 3DXML, etc.
Export meshes as binary STL, binary PLY, ASCII OFF, OBJ, GLTF/GLB 2.0, COLLADA, etc.
# 加载3D模型, stl/obj
mesh = trimesh.load_mesh('assets/T520.obj')
mesh.show()
2、获取模型的数据
1)顶点数据
# 顶点数值
mesh.vertices, len(mesh.vertices)
(TrackedArray([[ 7.69345e-01, -1.68524e-01, -5.17700e-03],
[ 7.95310e-01, -1.30030e-01, -1.49920e-02],
[ 7.96241e-01, -2.50368e-01, 7.82000e-04],
[ 7.89202e-01, -2.47317e-01, -1.20380e-02],
...
[-7.28633e-01, -4.08450e-02, -2.48030e-02],
[-7.75898e-01, -7.34740e-02, -1.63650e-02]]),
252)
2)三角面片数据
# 具体的三角面片数值
mesh.triangles, mesh.triangles.shape
(array([
[[ 0.706239, -0.098673, -0.017216],
[ 0.691089, -0.012151, -0.031851],
[ 0.632243, -0.153526, -0.011587]],
[[-0.072159, -0.088825, -0.011398],
[-0.103937, -0.111302, -0.012344],
[-0.11107 , -0.110316, -0.043356]],
[[ 0.22723 , -0.144808, -0.022788],
[ 0.174921, -0.039651, -0.027571],
[ 0.160237, -0.11074 , 0.001933]],
...,
[[-0.188532, -0.037858, -0.043866],
[-0.156491, 0.026905, -0.063014],
[-0.249915, 0.023218, -0.060589]],
[[ 0.769345, -0.168524, -0.005177],
[ 0.79531 , -0.13003 , -0.014992],
[ 0.764495, -0.065438, -0.018574]],
[[ 0.764495, -0.065438, -0.018574],
[ 0.716619, -0.036048, -0.035477],
[ 0.758556, -0.016973, -0.031903]]
]),
(500, 3, 3))
3)获取三角面的索引
# 三角面索引
mesh.faces, mesh.faces.shape
(TrackedArray([[ 8, 7, 15],
[172, 192, 194],
[ 53, 58, 65],
...,
[207, 204, 205],
[ 0, 1, 4],
[ 4, 5, 6]]),
(500, 3))
4)获取法向
# 法向
mesh.face_normals, mesh.face_normals.shape
(array([[-0.0430527 , 0.15929062, 0.98629253],
[ 0.57338015, -0.80401884, -0.15744496],
[ 0.44854726, 0.26199037, 0.85449775],
...,
[ 0.01400081, 0.27712567, 0.96073167],
[ 0.16915266, 0.13484063, 0.97632238],
[ 0.18972766, -0.23867821, -0.95238444]]),
(500, 3))
5)面角度
mesh.face_angles, mesh.face_angles.shape
(array([[2.0388429 , 0.56233828, 0.54041148],
[0.60782991, 1.76058396, 0.77317879],
[0.72379323, 0.73242608, 1.68537335],
...,
[1.21296721, 1.08268034, 0.84594511],
[0.63579868, 2.10158121, 0.40421277],
[0.86144328, 0.98109206, 1.29905732]]),
(500, 3))
6)面片面积
mesh.area_faces, mesh.area_faces.shape
(array([0.00366692, 0.00060866, 0.00307942, 0.00190347, 0.00148136,
0.00144005, 0.00038739, 0.00108227, 0.00081048, 0.00068454,
0.00144096, 0.00436192, 0.00092929, 0.00109818, 0.00200931,
0.00293418, 0.00155164, 0.00069498, 0.00211061, 0.00245476,
...
0.00041811, 0.00288816, 0.00024923, 0.00102368, 0.00124401,
0.00203138, 0.00155559, 0.0017429 , 0.00130824, 0.00059137,
0.00061473, 0.00168217, 0.00308738, 0.00146638, 0.00112652]),
(500,))
7)总面积、总体积、最大最小点(aabb包围盒)、实体数量
# 边界包围盒
mesh.area, mesh.volume,mesh.bounds,mesh.body_count
(1.170476162025276,
0.018943660462617348,
array([[-0.797947, -0.333213, -0.095076],
[ 0.796241, 0.442033, 0.285582]]),
1)
3、导出模型
导出模型
mesh.export('output/export-models.stl')