经常玩几何模型的人都知道,meshlab 是一个强大的mesh模型处理工具。
一、在Ubuntu中的使用MeshLab
安装
sudo apt install meshlab
启动
直接在terminal中输入:meshlab
导入模型
菜单栏: File → Import Mesh
或直接拖拽模型文件到 MeshLab 窗口
查看当前坐标系
选择菜单: Render → Show Axis
平移坐标系
选择菜单: Filters → Normals, Curvatures and Orientation → Transform: Move, Translate, Center
参数设置:
Translation X/Y/Z: 输入平移量勾选 Center to Origin 可将模型中心移至原点
点击 Apply 执行
旋转坐标系
选择菜单: Filters → Normals, Curvatures and Orientation → Transform: Rotate
参数设置:
选择旋转轴(X/Y/Z)输入旋转角度(度)
点击 Apply 执行
缩放坐标系
选择菜单: Filters → Normals, Curvatures and Orientation → Transform: Scale
参数设置:
Uniform Scale: 统一缩放比例或分别设置 X/Y/Z 缩放比例
点击 Apply 执行
简化网格
选择菜单: Filters → Remeshing, Simplification and Reconstruction → Simplification: clustering decimation
参数设置:
Cell Size: 统一缩放比例
点击 Apply 执行
二、在ubuntu中使用pymeshlab
PyMeshLab 是 MeshLab 的 Python 封装,允许用户通过 Python 脚本进行 3D 网格批量处理。以下是详细安装和使用方法。
通过 pip 安装最新稳定版
pip install pymeshlab
验证安装
python -c "import pymeshlab; print(pymeshlab.__version__)"
加载和保存网格
import pymeshlab as ml
ms = ml.MeshSet()
ms.load_new_mesh("input.obj") # 支持 obj/stl/ply/off 等格式
ms.save_current_mesh("output.ply")
简化网格 (顶点数减半)
ms.apply_filter("simplification_quadric_edge_collapse_decimation",
targetperc=50)
计算顶点法线
ms.apply_filter("compute_normals_for_point_sets")
平滑处理 (10次迭代)
ms.apply_filter("laplacian_smooth", stepsmoothnum=10)
平移网格 (X+10, Y-5, Z不变)
ms.apply_filter("compute_matrix_from_translation", axisx=10, axisy=-5, axisz=0)
绕Z轴旋转45度
ms.apply_filter("compute_matrix_from_rotation", rotaxis="Z axis", angle=45)
均匀缩放2倍
ms.apply_filter("compute_matrix_from_scaling", scalex=2, scaley=2, scalez=2)
批量处理多个文件
import os
input_dir = "input_meshes/"
output_dir = "processed_meshes/"
for filename in os.listdir(input_dir):
if filename.endswith(".obj"):
ms = ml.MeshSet()
ms.load_new_mesh(os.path.join(input_dir, filename))
ms.apply_filter("remove_isolated_pieces")
ms.apply_filter("compute_normals_for_point_sets")
ms.save_current_mesh(os.path.join(output_dir, filename))
可视化处理结果
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
mesh = ms.current_mesh()
vertices = mesh.vertex_matrix()
faces = mesh.face_matrix()
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.plot_trisurf(vertices[:,0], vertices[:,1], faces, vertices[:,2])
plt.show()
三、总结
以上是简单的示例,更详细的功能可以参考官网~
https://www.meshlab.net/
https://pymeshlab.readthedocs.io/en/latest/