Given the following data:
I'm trying to export some 3D file that I can readily import to 3D Slicer for registration against a VTK file.
Faces: https://pastebin.com/qWq7aiEs
Vertices: https://pastebin.com/VxsxsdcM
I have been trying various packages/libraries etc. and keep running into various issues. The following issue is using the Trimesh in Python and I'm attributing the problem to my poor python ability rather than an issue with the package. I'm open to other solution as well.
I imported the CSV, extracted the arrays then converted to lists to match the example. I wasn't certain how to use the faces data given the example, so I just made a list that matched the length of vertices, not sure if that's right at all.
# 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
Any advice how to troubleshoot is greatly appreciated!
By downloading the data you provided, I coded the following:
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")
So the code does work, however I am not so sure about the correctness of your input data. Vertices are indexed from 0 to vertex_count - 1
, so in this case [0, 6500]
That's why I have subtracted -1
to faces
since vertices are 0-indexed.
The faces
array consists of vertex indices, however its maximum index values (after subtracting -1) is 6500 and that's fine, but the minimum is 262. So check your data carefully.