question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

obj file loaded as Scene object instead of Trimesh object

See original GitHub issue

Hi,

I’m trying to load a mesh from an .obj file (from ShapeNet dataset) using Trimesh, and then use the repair. fix_winding(mesh) function.

But when I load the mesh, via trimesh.load('/path/to/file.obj') or trimesh.load_mesh('/path/to/file.obj'), the object class returned is Scene, which is incompatible with repair. fix_winding(mesh), only Trimesh object are accepted.

How can I force it to load and return a Trimesh object or parse the Scene object to Trimesh object?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
jackdcommented, Jul 25, 2019

My understanding is that some .obj files specify different sub-meshes. trimesh attempts to recreate this as faithfully as possible. You can always concatenate the meshes together, though not all aspect have concatenation which is supported. If all you care about is the vertices/faces, you can use something like the following:

import trimesh


def as_mesh(scene_or_mesh):
    """
    Convert a possible scene to a mesh.

    If conversion occurs, the returned mesh has only vertex and face data.
    """
    if isinstance(scene_or_mesh, trimesh.Scene):
        if len(scene_or_mesh.geometry) == 0:
            mesh = None  # empty scene
        else:
            # we lose texture information here
            mesh = trimesh.util.concatenate(
                tuple(trimesh.Trimesh(vertices=g.vertices, faces=g.faces)
                    for g in scene_or_mesh.geometry.values()))
    else:
        assert(isinstance(mesh, trimesh.Trimesh))
        mesh = scene_or_mesh
    return mesh
5reactions
mikedhcommented, Aug 13, 2020

Yeah the material concatenation is pretty expensive, we could add Yet Another Keyword Argument™️ to the OBJ loader haha. It still complains about concatenation because it’s still loading UV coordinates, but speeds it up by an order of magnitude:

In [7]: %timeit m = trimesh.load('bigmodel/bigben/model.obj', force='mesh', skip_texture=True)
concatenating texture: may result in visual artifacts

123 ms ± 1.16 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [8]: %timeit m = trimesh.load('bigmodel/bigben/model.obj', force='mesh', skip_texture=False)
concatenating texture: may result in visual artifacts

1.9 s ± 27.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

Happy to include in the next release if that’s useful.

Read more comments on GitHub >

github_iconTop Results From Across the Web

obj file loaded as Scene object instead of Trimesh object
With the current trimesh version (3.9.42), you need to use trimesh.load with force='mesh' : trimesh.load('/path/to/file.obj', force='mesh').
Read more >
trimesh.exchange.obj — trimesh 3.17.1 documentation
Load a Wavefront OBJ file into kwargs for a trimesh.Scene object. Parameters. file_obj (file like object) – Contains OBJ data.
Read more >
Python Examples of trimesh.load - ProgramCreek.com
GFile object or a string specifying the mesh file path. file_type: A string ... be passed to trimesh.load(). Returns: A trimesh.Trimesh or trimesh.Scene....
Read more >
Example Usage — meshrender 0.0.1 documentation
Everything in the meshrender model revolves around the Scene object, ... Begin by loading meshes cube_mesh = trimesh.load_mesh('cube.obj') sphere_mesh ...
Read more >
trimesh/Lobby - Gitter
Does anyone know whether the collision manager considers an object fully ... _ = trimesh.collision.scene_to_collision(scene) # Sliding box collider box ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found