Skip to content

Working with tetrahedral meshes in Pybullet

Loading OBJ files for deformables is great for objects that are "floppy" or "thin" in nature, such as a tote bag, but not so good for objects that are more "squishy", like a sponge. This is because files such as .OBJ or .STL are surface (triangular) meshes, so they have no volumetric information to them.

So, instead we'll need to use a tetrahedral mesh in .VTK format

Using GMSH

  1. Open the GMSH GUI (./gmsh within the bin/ directory of your GMSH install)
  2. File -> Open -> Click on the STL file to load. Note: In theory GMSH should be able to open an OBJ, but OBJs tend to lead to import errors. If this is the case, use a STL of the same mesh instead.
  3. Geometry -> Elementary entities -> Add -> Volume
  4. When prompted with "Select volume boundary", click on the mesh
  5. If a popup mentions "A scripting command is going to be appended to a non-geo file", click on "Create new .geo file"
  6. When prompted with "Select hole boundaries", press 'e' since there are no holes in the cargo bag
  7. Click on Mesh -> 3D to view and enable the tetrahedral mesh
  8. Save the file
  9. File -> Export -> Specify the filename with a .VTK extension. If a VTK options window shows up, keep it in ASCII format. The "save all elements" checkbox does not seem to make a difference

This .VTK file will look something like:

# vtk DataFile Version 2.0
bag_remesh, Created by Gmsh 4.11.1 
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 518 double
0.125 -0.125 -0.0250000003725
0.107393302023 -0.139757603407 -0.0250000003725
...
0.06279948249067006 -0.07503616573439448 0.09226952087427626

CELLS 2577 12107
3 0 1 2
3 0 37 1
... 
3 384 385 382
4 308 427 121 442
4 188 405 186 422
...
4 424 468 357 481

CELL_TYPES 2577
5
5
...
5
10
10
...
10

Modifying the VTK

Pybullet will not be able to load this file right now, because it is a combination of surface (triangle) meshes and volume (tetrahedral) meshes. But, it is not too difficult to modify this file to remove the surface mesh and just leave behind the tet mesh.

Before this, we need to understand the file format:

  • The line starting with CELLS has two values corresponding to
  • NUM_CELLS: The total number of (surface + volume) cells in the mesh
  • LIST_SIZE: The number of values in the CELLS list. This is equal to 4*NUM_TRIS + 5*NUM_TETS
  • Each line in the CELLS list starting with 3 corresponds to a triangular cell, and any line starting with 4 is a tetrahedral cell.
  • The CELL_TYPES line contains the same value (NUM_CELLS) as was seen in CELLS
  • Each line in CELL_TYPES is a 5 for a triangular cell and 10 for a tetrahedral cell.

To delete the surface (triangular) mesh,

  1. Count the number of triangular cells (NUM_TRIS)
  2. Update the counts:
  3. NUM_CELLS -= NUM_TRIS
  4. LIST_SIZE -= 4*NUM_TRIS
  5. Delete the lines in CELLS starting with 3, and all 5s in the CELL_TYPES
  6. Update the CELLS and CELL_TYPES lines with the new counts

After these modifications, Pybullet should be able to successfully import the mesh.

Texturing

To texture a tetrahedral mesh, there are two options:

  1. Generate the tetrahedral mesh from an OBJ which already has the correct UV mapping for a specified texture file, then apply the texture with loadTexture / changeVisualShape
  2. This should work given some preliminary tests with a VTK with a random UV mapping. But, this hasn't been fully tested yet (as of 4/10/23)
  3. Use the texture from an OBJ by loading both the OBJ and VTK together
  4. The OBJ and the VTK should have the exact same geometry
  5. Load the OBJ file through the main filename parameter, but pass the VTK file in through simFileName. The resulting softbody will have the physical properties you'd expect from a tetrahedral mesh, but the same texture as the OBJ
  6. This has been tested and works. Note: if you removeBody using the ID returned from loadSoftBody, it will leave a "ghost" visual of the OBJ. There may be a separate ID attached to the visual, but it is unclear what it is right now

Future TODOs

Apparently, there is a way to export just the tetrahedral mesh elements via Physical groups -- see this thread for info. For now though, this manual modification process works.