trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

Trimesh.Scene vs Trimesh.Trimesh as .obj import

Open albertotono opened this issue 2 years ago • 2 comments

After I export the obj from Blender and then load in Trimesh,

Some obj are imported as:

  • Scene <trimesh.Scene(len(geometry)=3)>
  • other as <trimesh.Trimesh(vertices.shape=(24, 3), faces.shape=(36, 3))>

Why is this happening, should I export the OBJ from Blender in a particular way? Should I import the obj differently? [ Right now I am using trimesh.load(path) ]

albertotono avatar Dec 09 '22 20:12 albertotono

Hey, yeah it depends on whether there are multiple materials or multiple o (object) directives in the OBJ. You can load with mesh = trimesh.load(file_name, force='mesh') or scene=trimesh.load(file_name, force='scene') to force it into a particular container type.

mikedh avatar Dec 09 '22 20:12 mikedh

@mikedh I would call the following behavior of export() and then load() a bug.

Repro steps

import numpy as np
import trimesh
triangles = {
    "triangle_1": trimesh.Trimesh(
        vertices=np.array([[0, 0, 0],
                           [1, 0, 0],
                           [0, 1, 0]]),
        faces=np.array([[0, 1, 2]])
    ),
    "triangle_2": trimesh.Trimesh(
        vertices= np.array([[2, 0, 0],
                              [3, 0, 0],
                              [2, 1, 0]]),
        faces=np.array([[0, 1, 2]])
    ),
    "triangle_3": trimesh.Trimesh(
        vertices=np.array([[4, 0, 0],
                              [5, 0, 0],
                              [4, 1, 0]]),
        faces= np.array([[0, 1, 2]])
    )
}

# Create a trimesh scene using a dictionary of {name: mesh}
triangle_scene = trimesh.Scene(triangles)

print(triangle_scene)


# Export to OBJ (this will keep object group names)
triangle_scene.export("triangles.obj")

imported_triangles = trimesh.load("triangles.obj",force = "scene")

print(imported_triangles)

outputs of the two prints:

<trimesh.Scene(len(geometry)=3)>
<trimesh.Scene(len(geometry)=1)>

Note that calling show() on imported_triangles and on triangle_scene shows expected scene in both cases.

I would expect that for any given scene, the consecutive operations export() and then load() should result in recovering the same object.

This might be slightly different than the OP, but stumbled upon this open issue while googling it.

gaelgoron avatar Apr 04 '25 19:04 gaelgoron