pythonmeshmplot3d

How to save a triangular mesh in python


I have a set of vertices and triangular faces, which together form a triangular mesh:

import numpy as np

verts = [[0.1, 1.,  1. ]  [1.,  1.,  0.1]  [1.,  0.1, 1. ]  [1.,  1.,  1.9]  [1.,  1.9, 1. ]
 [1.9, 1.,  1. ] ]
faces = [[ 2,  1,  0]  [ 0,  3,  2]  [ 1,  4,  0]  [ 0,  4,  3]  [ 5,  1,  2]  [ 3,  5,  2]
 [ 5,  4,  1]  [ 4,  5,  3]]

The only way I know to create from these a triangular mesh is using Poly3Dcollection

from mpl_toolkits.mplot3d.art3d import Poly3DCollection

myMesh = Poly3DCollection(verts[faces])

Next, I want to use the pygalmesh module to create a volume mesh. It should take in a surface mesh and output a volume mesh, like is shown here.

According to the tutorial, I should be able to create a volume mesh using:

import pygalmesh

mesh = pygalmesh.generate_volume_mesh_from_surface_mesh(
    "elephant.vtu",
    facet_angle=25.0,
    facet_size=0.15,
    facet_distance=0.008,
    cell_radius_edge_ratio=3.0,
    verbose=False
)

However, when I run:

mesh = pygalmesh.generate_volume_mesh_from_surface_mesh(myMesh)

I am returned:

TypeError: expected str, bytes or os.PathLike object, not Poly3DCollection

I think this is because I am either creating my mesh incorrectly, or that I am meant to save it in some form before using pygalmesh.generate_volume_mesh_from_surface_mesh. I'm not sure. I've raised the issue in the modules github, but haven't received good feedback yet.


Solution

  • Following @Paddy Harrison's advice, I used meshio's write function. Specifically, with my dataset, it is done like so:

    import meshio 
    
    points = np.array(verts)
    cells = [("triangle", np.array(faces)]
    meshio.write_points_cells('out.vtu',points,cells)