marian42 / mesh_to_sdf

Calculate signed distance fields for arbitrary meshes
https://pypi.org/project/mesh-to-sdf/
MIT License
1.03k stars 109 forks source link

pyglet hanging after calling sample_sdf_near_surface() for many times #19

Closed chenzhaiyu closed 3 years ago

chenzhaiyu commented 3 years ago

Description

The process being stuck forever after calling sample_sdf_near_surface() for certain amount of times in a loop. In my case, it's always stuck after sampling 327 meshes, and it has nothing to do with the 328th mesh itself (working fine as input alone, or in a small batch). It seems pyrender / pyglet is just hanging. Could you please give some clues?

Traceback (when KeyboardInterrupt)

34%|███▍      | 327/953 [1:08:03<2:02:08, 11.71s/it]
Exception ignored in: <function Platform.__del__ at 0x7fdb8e5e8790>
Traceback (most recent call last):
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyrender/platforms/base.py", line 74, in __del__
    self.delete_context()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyrender/platforms/pyglet_platform.py", line 62, in delete_context
    self._window.close()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/window/xlib/__init__.py", line 520, in close
    super(XlibWindow, self).close()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/window/__init__.py", line 843, in close
    app.event_loop.dispatch_event('on_window_close', self)
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/event.py", line 415, in dispatch_event
    if getattr(self, event_type)(*args):
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/app/base.py", line 307, in on_window_close
    self.exit()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/app/base.py", line 281, in exit
    app.platform_event_loop.notify()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/app/xlib.py", line 99, in notify
    self._notification_device.set()
  File "/home/zhaiyu/anaconda3/envs/sog/lib/python3.8/site-packages/pyglet/app/xlib.py", line 80, in set
    os.write(self._sync_file_write, asbytes('1'))
KeyboardInterrupt: 

Process finished with exit code 137 (interrupted by signal 9: SIGKILL)

Env

Ubuntu 18.04 Python 3.8 pyrender 0.1.43 pyglet 1.5.11

chenzhaiyu commented 3 years ago

Update:

This was solved by

import os
os.environ['PYOPENGL_PLATFORM'] = 'egl'
allenzren commented 3 years ago

I encountered the same issue, and then I had to use egl instead of pyplet. Wonder if anyone knows the issue with pyplet.

liangpan99 commented 2 years ago

i meet the same problem, use egl can't help, any solutions?

liangpan99 commented 2 years ago

I know the reason why the process being stuck.

In my case, there are too many renderers are created in this function in mesh_to_sdf/pyrender_wrapper.py.

def render_normal_and_depth_buffers(mesh, camera, camera_transform, resolution):
    global suppress_multisampling
    suppress_multisampling = True
    scene = pyrender.Scene()
    scene.add(pyrender.Mesh.from_trimesh(mesh, smooth = False))
    scene.add(camera, pose=camera_transform)

    renderer = pyrender.OffscreenRenderer(resolution, resolution)
    renderer._renderer._program_cache = CustomShaderCache()

    color, depth = renderer.render(scene, flags=pyrender.RenderFlags.SKIP_CULL_FACES)
    suppress_multisampling = False
    return color, depth

So, I only create the renderer once to tackle the problem successfully.

resolution = 400
global renderer_g
renderer_g = pyrender.OffscreenRenderer(resolution, resolution)
renderer_g._renderer._program_cache = CustomShaderCache()

def render_normal_and_depth_buffers(mesh, camera, camera_transform, resolution):
    global suppress_multisampling
    suppress_multisampling = True
    scene = pyrender.Scene()
    scene.add(pyrender.Mesh.from_trimesh(mesh, smooth = False))
    scene.add(camera, pose=camera_transform)

    # renderer = pyrender.OffscreenRenderer(resolution, resolution)
    # renderer._renderer._program_cache = CustomShaderCache()

    # color, depth = renderer.render(scene, flags=pyrender.RenderFlags.SKIP_CULL_FACES)
    color, depth = renderer_g.render(scene, flags=pyrender.RenderFlags.SKIP_CULL_FACES)

    suppress_multisampling = False
    return color, depth