鉴于以下数据:
我正在尝试导出一些3D文件,可以很容易地导入到三维切割机中,以便根据VTK文件进行注册。
脸:https://pastebin.com/qWq7aiEs
顶点:https://pastebin.com/VxsxsdcM
我一直在尝试各种包/库等,并不断地遇到各种问题。下面的问题是使用Python中的Trimesh,我把这个问题归因于我糟糕的python能力,而不是包的问题。我也对其他解决方案持开放态度。
我导入了CSV,提取了数组,然后转换成列表来匹配这个例子。给出了这个例子,我不确定如何使用faces数据,所以我只列出了一个与顶点长度相匹配的列表,不确定这是否正确。
# Example: mesh objects can be created from existing faces and vertex data
mesh = trimesh.Trimesh(vertices=[[0, 0, 0], [0, 0, 1], [0, 1, 0]],
faces=[[0, 1, 2]])
# My attempt
vertex_x = vertices_csv[:,0]
vertex_y = vertices_csv[:,1]
vertex_z = vertices_csv[:,2]
vertex_x_list = vertex_x.tolist()
vertex_y_list = vertex_y.tolist()
vertex_z_list = vertex_z.tolist()
faces_list_mesh = [i for i in range(len(vertex_x_list))]
mesh = trimesh.Trimesh(vertices=[vertex_x_list, vertex_y_list, vertex_z_list],
... faces=[faces_list_mesh])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\...\Python\Python310\lib\site-packages\trimesh\base.py", line 190, in __init__
self.process(validate=validate,
File "C:\...\Python\Python310\lib\site-packages\trimesh\base.py", line 232, in process
self.merge_vertices(merge_tex=merge_tex,
File "C:\...\Python\Python310\lib\site-packages\trimesh\base.py", line 1123, in merge_vertices
grouping.merge_vertices(
File "C:\...\Python\Python310\lib\site-packages\trimesh\grouping.py", line 70, in merge_vertices
referenced[mesh.faces] = True
IndexError: index 3 is out of bounds for axis 0 with size 3
任何关于如何排除故障的建议都是非常感谢的!
发布于 2022-07-28 22:29:08
通过下载您提供的数据,我编写了以下代码:
import numpy as np
import trimesh
vertices = np.loadtxt("vertices.txt", delimiter=",")
faces = np.loadtxt("faces.txt", delimiter=",") - 1
mesh = trimesh.Trimesh(vertices=vertices, faces=faces)
mesh.export("mesh.stl")
因此,代码确实有效,但是我不太确定输入数据的正确性。顶点是从0到vertex_count - 1
的索引,所以在这里,[0, 6500]
,这就是为什么我把-1
减去faces
,因为顶点是0索引的。faces
数组由顶点索引组成,但其最大索引值(减去-1后)为6500,这很好,但最小值为262。所以仔细检查你的数据。
上面的代码导出以下网格:
https://stackoverflow.com/questions/73147271
复制相似问题