导入、导出、处理、分析和查看三角形网格。

trimesh的Python项目详细描述


trimesh


构建状态构建状态coverage status>>>>>>>>>>>>>>>>circleci加入聊天,网址:https://gitter.im/trimsh/lobby

trimesh是一个纯python(2.7-3.4+)库,用于加载和使用三角形网格。该库的目标是提供一个功能齐全且经过良好测试的trimesh对象,该对象允许以shapely库中的多边形对象的样式进行简单的操作和分析。

api基本上是稳定的,但这不应该依赖,也不能保证:如果计划使用trimesh部署某些内容,请安装特定版本。

我们很感激您的请求并及时回复!如果您愿意参与,这里有一个最新的潜在增强功能列表,但也欢迎不在该列表中的内容。下面是用python编写网格代码的一些技巧。

基本安装

保持trimesh易于安装是一个核心目标,因此只有硬依赖才是numpy。安装其他软件包会增加功能,但不是必需的。对于只使用numpy的最简单安装,pip通常可以在windows、linux和osx上干净地安装trimesh

pip install trimesh

对于更多功能,如凸面外壳(scipy)、图形操作(networkx)、更快的光线查询(pyembree)、矢量路径处理(shapelyrtree)、预览窗口(pyglet)、更快的缓存检查(xxhash)更重要的是,获得完整的rimesh安装是一个conda环境:

# this will install all soft dependencies available on your current platform
conda install -c conda-forge trimesh

安装trimesh

pip install trimesh[easy]

有关更多信息,请参阅高级安装文档

快速启动

下面是从文件加载网格并为其面着色的示例。这是一个格式很好的 本例的ipython笔记本版本。另外,还可以查看截面示例或网格示例上函数的集成

importnumpyasnpimporttrimesh# attach to logger so trimesh messages will be printed to consoletrimesh.util.attach_to_log()# mesh objects can be created from existing faces and vertex datamesh=trimesh.Trimesh(vertices=[[0,0,0],[0,0,1],[0,1,0]],faces=[[0,1,2]])# 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=[[0,0,0],[0,0,1],[0,1,0]],faces=[[0,1,2]],process=False)# mesh objects can be loaded from a file name or from a buffer# you can pass any of the kwargs for the `Trimesh` constructor# to `trimesh.load`, including `process=False` if you would like# to preserve the original loaded data without merging vertices# STL files will be a soup of disconnected triangles without# merging vertices however and will not register as watertightmesh=trimesh.load('../models/featuretype.STL')# is the current mesh watertight?mesh.is_watertight# what's the euler number for the mesh?mesh.euler_number# the convex hull is another Trimesh object that is available as a property# lets compare the volume of our mesh with the volume of its convex hullprint(mesh.volume/mesh.convex_hull.volume)# since the mesh is watertight, it means there is a# volumetric center of mass which we can set as the origin for our meshmesh.vertices-=mesh.center_mass# what's the moment of inertia for the mesh?mesh.moment_inertia# if there are multiple bodies in the mesh we can split the mesh by# connected components of face adjacency# since this example mesh is a single watertight body we get a list of one meshmesh.split()# facets are groups of coplanar adjacent faces# set each facet to a random color# colors are 8 bit RGBA by default (n, 4) np.uint8forfacetinmesh.facets:mesh.visual.face_colors[facet]=trimesh.visual.random_color()# preview mesh in an opengl window if you installed pyglet with pipmesh.show()# transform method can be passed a (4, 4) matrix and will cleanly apply the transformmesh.apply_transform(trimesh.transformations.random_rotation_matrix())# axis aligned bounding box is availablemesh.bounding_box.extents# a minimum volume oriented bounding box also available# primitives are subclasses of Trimesh objects which automatically generate# faces and vertices from data stored in the 'primitive' attributemesh.bounding_box_oriented.primitive.extentsmesh.bounding_box_oriented.primitive.transform# show the mesh appended with its oriented bounding box# the bounding box is a trimesh.primitives.Box object, which subclasses# Trimesh and lazily evaluates to fill in vertices and faces when requested# (press w in viewer to see triangles)(mesh+mesh.bounding_box_oriented).show()# bounding spheres and bounding cylinders of meshes are also# available, and will be the minimum volume version of each# except in certain degenerate cases, where they will be no worse# than a least squares fit version of the primitive.print(mesh.bounding_box_oriented.volume,mesh.bounding_cylinder.volume,mesh.bounding_sphere.volume)

功能

  • 从binary/ascii stl、wavefront obj、ascii off、binary/ascii ply、gltf/glb 2.0、3mf、xaml、3dxml等导入网格。
  • 从/到DXF或SVG文件导入和导出二维或三维矢量路径
  • 如果已安装,则使用gmsh sdk导入几何文件(brep、step、iges、inp、bdf等)
  • 将网格导出为二进制stl、二进制ply、ascii off、obj、gltf/glb 2.0、collada等。
  • 如果已安装,则使用gmsh sdk导出网格(abaqus inp、nastran bdf等)
  • 使用pyglet预览网格或使用3.js在jupyter笔记本中在线预览网格
  • 使用md5、zlib crc或xxhash自动散列numpy数组进行更改跟踪
  • 从哈希验证的计算值的内部缓存
  • 通过定义自定义numpy数据类型编写的导入程序快速加载二进制文件
  • 计算面邻接、面角度、顶点缺陷等。
  • 计算横截面,即3D打印中使用的切片操作
  • 使用一个或多个任意平面剖切网格并返回结果曲面
  • 使用networkx、graph tool或scipy.sparse基于面连接拆分网格
  • 计算质量特性,包括体积、质量中心、惯性矩、惯性矢量主分量和分量
  • 修复三角形缠绕、法线和四/三孔的简单问题
  • 凸面网格壳
  • 计算旋转/平移/镶嵌不变标识符并查找重复网格
  • 确定网格是否防水、凸出等。
  • 均匀采样网格表面
  • 光线网格查询,包括位置、三角形索引等。
  • 使用openscad或blender作为后端对网格(交集、并集、差分)执行布尔操作。请注意,网格布尔值通常是缓慢且不可靠的
  • 体素化无间隙网格
  • 使用gmsh sdk生成卷网格(tetgen)
  • 使用拉普拉斯平滑算法(经典、陶宾、汉弗莱)平滑无间隙网格
  • 细分网格的面
  • 网格的最小面向体积的边界框
  • 最小体积边界球体
  • 三角形上函数的符号积分
  • 计算网格曲面上最近的点和有符号距离
  • 使用符号距离确定点是否位于构造良好的网格的内部或外部
  • 基本体对象(长方体、圆柱体、球体、拉伸)是子类Trimesh对象,具有所有相同的功能(惯性、查看器等)
  • 可渲染或导出的简单场景图和转换树(Pyglet窗口,Jupyter笔记本中的three.js,pyrender)。
  • 许多实用功能,如转换点、统一向量、对齐向量、跟踪numpy数组以进行更改、分组行等。

查看器

trimesh包括一个可选的基于pyglet的查看器,用于调试和思辨。在用mesh.show()打开的"网格视图"窗口中,可以使用以下命令:

  • 鼠标单击+拖动旋转视图
  • ctl+鼠标单击+拖动平移视图
  • 鼠标滚轮缩放
  • z返回基本视图
  • w切换线框模式
  • c切换背面消隐
  • f在全屏和窗口模式之间切换
  • m最大化窗口
  • q关闭窗口
  • a在三种状态之间切换XYZ-RGB轴标记:关、在世界帧或在每一帧

如果从jupyter笔记本中调用,mesh.show()显示使用three.js显示网格或场景的联机预览。为了获得更完整的渲染(PBR、更好的照明、着色器、更好的屏幕外支持等)pyrender被设计为与trimesh对象交互操作。

使用trimesh的项目

您可以查看github network中使用trimesh的内容。精选几个:

我应该使用哪种网格格式?

快速推荐:glbstl。每次你用glb替换obj时,一个天使就会长出翅膀。

如果您想要按索引面、实例、颜色、纹理等,那么glb是一个非常好的选择。gltf/glb是一种指定得非常好的现代格式,易于快速解析:它有一个json头,描述二进制blob中的数据。它有一个简单的分层场景图,一个非常漂亮的基于物理的现代材料系统,支持几十到几百个库和一个库。r-efficient-transmission-of-3d-scenes-models" rel="nofollow">John Carmack endorsment

在一般情况下,stl可能是最常见的格式。stl文件非常简单:它基本上只是一个三角形列表。它们非常坚固,是基本几何的最佳选择。

wavefrontobj也很常见:不幸的是obj没有一个被广泛接受的规范,因此每个进口商和出口商实现的东西都略有不同,这使得它很难支持。它还允许一些不幸的事情,比如任意大小的多边形,有一个容易弄乱的面部表示,为材质和纹理引用其他文件,任意交错数据,并且解析速度慢。把glbply作为一种替代方法试试!

我怎么能引用这个图书馆?

一个经常出现的问题是如何引用库。一个快速的bibtex建议:

@software{trimesh,
	author = {{Dawson-Haggerty et al.}},
	title = {trimesh},
	url = {https://trimsh.org/},
	version = {3.2.0},
	date = {2019-12-8},
}

容器

如果你想在一个使用trimesh的容器中部署一些东西,docker hub上提供了基于trimesh和依赖项的自动构建

Docker拉Mikedh/Trimesh

以下是如何在容器中使用llvmpipe和xvfb渲染网格的示例。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
带Maven的Eclipse Java存储库:缺少工件:compile   java如何以编程方式停止RMI服务器并通知所有客户端   java Roboguice抛出ClassNotFoundException:AnnotationDatabaseImpl   java为什么lucene 4.0删除IndexWriter类的两个构造函数?   nls如何避免java项目上不需要的日志消息?   测试无法在Selenium Webdriver(java)中定位iframe   使用XML的java servlet   java如何使用jxl用****屏蔽单元格   java使用SQLite从数据库中选择“没有这样的列”   导入扫描程序后出现java编译错误   插入查询的java空指针异常   使用创建PostgreSQL数据库。Java应用中的sql脚本   java使用jsoup将HTML解析为格式化的明文