Search code examples
pythonmeshtrimesh

Load Mesh from a glb file using trimesh


I am new to 3D geometry. I am trying to extract a mesh from a glb file using python library trimesh. I couldn't figure out the proper way of doing that. My requirement is that I need a 3D mesh (as adjacency matrix) of an object so that I can apply non-euclidian convolutional operators on them.

Any suggestions on what I should be looking at?


Solution

  • You can use trimesh.load to load your glTF file. Note that the return type depends on the filetype of your model file. For glTF files it will return an instance of trimesh.Scene. The scene has all sorts of attributes like the camera, lights but also geometries. This is because glTF files can contain more that just model data. Each geometry is an instance of trimesh.Trimesh, which is the base class for geometries and has a edges_sparse property which represents the adjacency matrix of the model.

    To put it all together:

    scene = trimesh.load("some_file.glb")
    geometries = list(scene.geometry.values())
    geometry = geometries[0]
    adjacency_matrix = geometry.edges_sparse
    

    It's a bit tedious to figure this out using the documentation. I tend to look at the source code or turn on the debugger in my IDE: