trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

The quick start example triggers typing errors

Open aminya opened this issue 1 year ago • 2 comments

In the quick_start example, trimesh.load_mesh returns a Geometry, and this class doesn't have a property of vertices or is_watertight. The typing should be fixed if this is a correct example.

https://trimesh.org/quick_start.html

# load a file by name or from a buffer
mesh: Geometry = trimesh.load_mesh('../models/featuretype.STL')

mesh.is_watertight # Error

Cannot access member "is_watertight" for type "Geometry"
  Member "is_watertight" is unknown

aminya avatar Mar 28 '24 23:03 aminya

The issue here is that load_mesh returns Geometry | list[Geometry], which are the parents of Trimesh and Scene. So, to correctly use those methods, there needs to be a type check after loading the mesh so that the type is more specific.

# load a file by name or from a buffer
mesh = trimesh.load_mesh('../models/featuretype.STL')

if isinstance(mesh, list):
  raise RuntimeError("Multiple meshes found.")

# cast Geometry to the more specific Trimesh type
if not isinstance(mesh, trimesh.Trimesh) and not isinstance(mesh, trimesh.Scene):
  raise RuntimeError(f"The mesh geometry is not a trimesh.Trimesh or trimesh.Scene.")

# Then use methods of Trimesh or Scene: ...

aminya avatar May 30 '24 20:05 aminya

So I can't define load_mesh with keyword argument for Trimesh? PyCharm seems to still think this is a scene.

        _file = fd.File
        path = _file.Open(_file)
        mesh = trimesh.load_mesh(path, "obj")
        if isinstance(mesh, list):
            raise RuntimeError(f"Multiple meshes found.")
        # cast Geometry to the more specific Trimesh type
        if not isinstance(mesh, trimesh.Trimesh) and not isinstance(mesh, trimesh.Scene):
            raise RuntimeError(f"The mesh geometry is not a trimesh.Trimesh or trimesh.Scene.")
        psmesh = ps.register_surface_mesh("my mesh", vertices=mesh.vertices, faces=mesh.faces)

ps stands for Polyscope, but PyCharm thinks that mesh.vertices and mesh.faces is pointing to Scene and not available. On VSCode suggestion works correctly.

JPLost avatar Jun 10 '24 07:06 JPLost