convert python mesh object to vtkPolyData

Hello,
In Python, I have an object of the following type:

your_mesh
<stl.mesh.Mesh object at 0x7fb108f06160>
>>> your_mesh.points
array([[ 250., -475., -100., -250., -475., -100., -250., -475., 1900.],
       [ 250., -475., -100., -250., -475., 1900.,  250., -475., 1900.],
       [ 250., -515., -100.,  250., -475., -100.,  250., -475., 1900.],
       [ 250., -515., -100.,  250., -475., 1900.,  250., -515., 1900.],
       [-250., -515., -100.,  250., -515., -100.,  250., -515., 1900.],
       [-250., -515., -100.,  250., -515., 1900., -250., -515., 1900.],
       [-250., -475., -100., -250., -515., -100., -250., -515., 1900.],
       [-250., -475., -100., -250., -515., 1900., -250., -475., 1900.],
       [-250., -515., -100.,  250., -475., -100.,  250., -515., -100.],
       [-250., -475., -100.,  250., -475., -100., -250., -515., -100.],
       [ 250., -475., 1900., -250., -515., 1900.,  250., -515., 1900.],
       [ 250., -475., 1900., -250., -475., 1900., -250., -515., 1900.]],
      dtype=float32)
>>> your_mesh.normals
array([[       0.,  1000000.,        0.],
       [       0.,  1000000.,       -0.],
       [   80000.,        0.,        0.],
       [   80000.,        0.,        0.],
       [       0., -1000000.,        0.],
       [       0., -1000000.,        0.],
       [  -80000.,        0.,        0.],
       [  -80000.,        0.,        0.],
       [       0.,        0.,   -20000.],
       [       0.,        0.,   -20000.],
       [       0.,        0.,    20000.],
       [       0.,        0.,    20000.]], dtype=float32)

And I was wondering if there is any short way to convert it to a vtkPolyData format, using Python. Something like:

import vtk
myData = vtk.vtkPolyData(your_mesh.points, your_mesh.normals)

Thanks in advance!

Hello,

There is a LOT of information missing. You have a set of XYZ coordinates in an array. So, what? I mean, how do they relate? Is the mesh a surface? Are they triangles? Are they quads with an implicit 4th edge? Are they tetras? Is the mesh a volume? Are they tetras? Are they hexas?

Essentially, you gave only the dots but you forgot to give us a clue of how to connect the dots.

regards,

Paulo

Thanks for the reply. Sorry, I forgot to mention that this mesh object comes from an STL file, so triangles. The file is originally opened using numpy-stl:

from stl import mesh
your_mesh = mesh.Mesh.from_file('testfile.stl')

So my question would be how to convert from a numpy-stl-mesh object to a vtkPolyData. (I know that I could open the STL file directly in VTK using the STLreader, but for one application I would better use a conversion step between mesh and vtkPolyData).

1 Like

this would be easy with either vedo or pyvista (with similar syntax), e.g.:

from stl import mesh
import vedo

fpath = vedo.download("https://ozeki.hu/attachments/116/Eiffel_tower_sample.STL")
your_mesh = mesh.Mesh.from_file(fpath)

verts, faces = [], []
for i in range(len(your_mesh.v0)):
	verts.append(your_mesh.v0[i])
	verts.append(your_mesh.v1[i])
	verts.append(your_mesh.v2[i])
	faces.append([i*3,i*3+1,i*3+2])

vmesh = vedo.Mesh([verts, faces]).clean()
vmesh.show(axes=1)
print(type(vmesh.polydata())) #vtkPolyData

2 Likes

Thanks Marco for the nice example, works like a charm. I have adapted it slightly to avoid the for-loop and make it more compact:

import itertools
from stl import mesh
import vedo

fpath = vedo.download("https://ozeki.hu/attachments/116/Eiffel_tower_sample.STL")
your_mesh = mesh.Mesh.from_file(fpath)
verts = list(itertools.chain(*(your_mesh.vectors)))
faces = [[i*3, i*3+1, i*3+2] for i in range(len(verts)//3)]
vpoly = vedo.Mesh([verts, faces]).clean().polydata()
1 Like