mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.96k stars 576 forks source link

SceneViewer.add_geometry() does not add a geometry #1618

Open thatuserb opened 2 years ago

thatuserb commented 2 years ago

I cannot seem to add geometries, specifically Trimesh objects to a viewer (the final goal is to freely replace one of the displayed meshes while rendering). When I run this code snipped, the first 2 attempts show the box and the cylinder, but the 3rd attempt does not show the cylinder:

import trimesh
import trimesh.viewer
import pyglet
import logging

# I added logging
logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s [%(module)s] [%(levelname)s] %(message)s",
    handlers=[logging.StreamHandler()])
logger = logging.getLogger(__name__)

cyl = trimesh.primitives.Cylinder()
box = trimesh.primitives.Box()

# this works perfectly fine
print("\n1st")
scene = trimesh.Scene([box, cyl])
trimesh.viewer.SceneViewer(scene, flags={"wireframe": True}, start_loop=False)
pyglet.app.run()

# so does this
print("\n2nd")
scene = trimesh.Scene([box])
scene.add_geometry(cyl)
trimesh.viewer.SceneViewer(scene, flags={"wireframe": True}, start_loop=False)
pyglet.app.run()

# but this does not
print("\n3rd")
scene = trimesh.Scene([box])
viewer = trimesh.viewer.windowed.SceneViewer(
    scene, flags={"wireframe": True}, start_loop=False)
viewer.add_geometry("cyl", cyl)
viewer.init_gl()
pyglet.app.run()

The log output goes

1st
2022-06-30 12:01:19,589 [primitives] [DEBUG] creating mesh for Box primitive
2022-06-30 12:01:19,590 [base] [DEBUG] apply_transform passed identity matrix
2022-06-30 12:01:19,590 [primitives] [DEBUG] creating mesh for Cylinder primitive
2022-06-30 12:01:19,591 [base] [DEBUG] generating face normals
2022-06-30 12:01:19,592 [caching] [DEBUG] 1 items cleared from cache: ['nodes']
2022-06-30 12:01:19,592 [caching] [DEBUG] 1 items cleared from cache: ['nodes']
2022-06-30 12:01:19,593 [caching] [DEBUG] 3 items cleared from cache: ['nodes_geometry', ('world', 'geometry_0'), ('world', 'geometry_1')]
2022-06-30 12:01:19,594 [caching] [DEBUG] 1 items cleared from cache: ['bounds_corners']
2022-06-30 12:01:19,748 [constants] [DEBUG] smoothed executed in 0.0022 seconds.
2022-06-30 12:01:19,756 [constants] [DEBUG] smoothed executed in 0.0023 seconds.
2022-06-30 12:01:19,758 [caching] [DEBUG] 4 items cleared from cache: [('world', 'camera_C9DF96'), 'nodes_geometry', ('world', 'geometry_0'), ('world', 'geometry_1')]
2022-06-30 12:01:19,759 [caching] [DEBUG] 5 items cleared from cache: ['bounds_corners', 'bounds', 'extents', 'scale', 'centroid']

2nd
2022-06-30 12:01:21,037 [caching] [DEBUG] 1 items cleared from cache: ['nodes']
2022-06-30 12:01:21,038 [caching] [DEBUG] 1 items cleared from cache: ['nodes']
2022-06-30 12:01:21,038 [caching] [DEBUG] 3 items cleared from cache: ['nodes_geometry', ('world', 'geometry_0'), ('world', 'geometry_1')]
2022-06-30 12:01:21,039 [caching] [DEBUG] 1 items cleared from cache: ['bounds_corners']
2022-06-30 12:01:21,117 [constants] [DEBUG] smoothed executed in 0.0000 seconds.
2022-06-30 12:01:21,123 [constants] [DEBUG] smoothed executed in 0.0000 seconds.
2022-06-30 12:01:21,125 [caching] [DEBUG] 4 items cleared from cache: [('world', 'camera_24EAAB'), 'nodes_geometry', ('world', 'geometry_0'), ('world', 'geometry_1')]
2022-06-30 12:01:21,127 [caching] [DEBUG] 5 items cleared from cache: ['bounds_corners', 'bounds', 'extents', 'scale', 'centroid']

3rd
2022-06-30 12:01:21,223 [caching] [DEBUG] 1 items cleared from cache: ['nodes']
2022-06-30 12:01:21,225 [caching] [DEBUG] 2 items cleared from cache: ['nodes_geometry', ('world', 'geometry_0')]
2022-06-30 12:01:21,226 [caching] [DEBUG] 1 items cleared from cache: ['bounds_corners']
2022-06-30 12:01:21,265 [constants] [DEBUG] smoothed executed in 0.0000 seconds.
2022-06-30 12:01:21,275 [caching] [DEBUG] 3 items cleared from cache: [('world', 'camera_F2E737'), 'nodes_geometry', ('world', 'geometry_0')]
2022-06-30 12:01:21,276 [caching] [DEBUG] 5 items cleared from cache: ['bounds_corners', 'bounds', 'extents', 'scale', 'centroid']
2022-06-30 12:01:21,277 [constants] [DEBUG] smoothed executed in 0.0000 seconds.

I am using Python 3.9.7 in Windows 10 with trimesh 3.12.6 and pyglet 1.5.26 (EDIT1): I added the log output

whateverforever commented 1 year ago

I have a similar issue. I subclassed trimesh.viewer.SceneViewer and want to add meshes on user interaction. I do this in on_key_press() by calling self.add_geometry(), however nothing happens. Same with self.scene.add_geometry()

Edit: I got it somewhat working. Seems like you need to add the geometry both, to the viewer, and to the scene, while making sure that the names match.

# inside SceneViewer subclass
geom_name = "some_name"
geom = trimesh.primitives.Sphere(radius=0.01, center=[1,2,3])
self.add_geometry(geom_name, geom)
self.scene.add_geometry([geom], geom_name=geom_name)