Python --Trimesh 包的基本用法

Python --Trimesh 包的基本用法

1. 读取三维模型
 

import trimesh
import numpy as np


PlyPath = "E:/data/0.ply"
mesh = trimesh.load(PlyPath)
v = mesh.vertices
f = mesh.faces

#这样得到的v,f格式是trimesh 内置的格式,不能直接用于其它计算,需要转换为numpy

v = np.array(v)
f = np.array(f)

2.已知v,f 写入mesh,这里指不输出实际的三维模型文件,而是以变量的形式存在

import trimesh
import numpy as np

v = [[1, 0, 0], [1, 1, 0], [0, 1, 0],[1, 1, 1]]
f = [[0, 1, 3], [0, 1, 3], [1, 2, 3], [0,2,3]]
mesh = trimesh.Trimesh(vertices = v, faces = f)

# by default, Trimesh will do a light processing, which will
# remove any NaN values and merge vertices that share position
# if you want to not do this on load, you can pass `process=False`

mesh = trimesh.Trimesh(vertices = v, faces = f, process = False)
mesh.show()

Python --Trimesh 包的基本用法_第1张图片

a 展示坐标轴, w只展示线条

Python --Trimesh 包的基本用法_第2张图片

g 展示背景方格

Python --Trimesh 包的基本用法_第3张图片

其它字母表示:

“鼠标单击拖动”旋转视图
*`ctl鼠标单击拖动`平移视图
*鼠标滚轮放大
*`z`返回基本视图
*`w'切换线框模式
*`c`切换背面剔除
*`g`切换XY网格,将Z设置为最低点
*`a`可以在三种状态之间切换XYZ-RGB轴标记:关,在世界帧或每帧
*`f`在全屏和窗口模式之间切换
*`m`最大化窗口
*`q`关闭窗口wf

4. 为模型随机展示颜色

for facet in mesh.facets:
    mesh.visual.face_colors[facet] = trimesh.visual.random_color()

Python --Trimesh 包的基本用法_第4张图片

修正:上面三面体的面显示,一面是透明的,一面遮光,经评论区的朋友指出,如果内部透明,外部遮光,四个面修改如下:

import trimesh
import numpy as np
 
v = [[1, 0, 0], [1, 1, 0], [0, 1, 0],[1, 1, 1]]
#原来的面
#f = [[0, 1, 3], [0, 1, 3], [1, 2, 3], [0,2,3]]
#修正的面
f = [[0, 2, 1],[1, 2, 3],[0, 3, 2],[0, 1, 3]]
mesh = trimesh.Trimesh(vertices = v, faces = f)
 
# by default, Trimesh will do a light processing, which will
# remove any NaN values and merge vertices that share position
# if you want to not do this on load, you can pass `process=False`
 
mesh = trimesh.Trimesh(vertices = v, faces = f, process = False)
mesh.show()

 

 

注意事项:

1.字母表示为英文;

2.若报错,根据提示安装所需要的包,注意和python版本匹配。

若无法找到对应版本的包,可以从这个网址下载: https://www.lfd.uci.edu/~gohlke/pythonlibs/

3.附上我自己安装的一些包:

pip install networkx -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install pyglet -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install Shapely-1.6.4.post2-cp35-cp35m-win_amd64.whl

pip install Rtree-0.9.3-cp35-cp35m-win_amd64.whl

 

最后两个包直接像上面那样pip安装,版本不对会报以下错误,

ImportError: DLL load failed: 找不到指定的模块。

如果出现这个错误,把最新安装的包先卸载掉, pip uninstall .........

从上面那个网址下载对应的包,再pip安装

 

 

你可能感兴趣的:(三维模型,python)