Team Ai
Apppublic

OpenMotionLab/MotionGPT

sourceHugging Facemitupdated 1y agoView on Hugging Face
118likes
pyglet_platform.py91 linesDownload Raw Back to platforms
1from pyrender.constants import (TARGET_OPEN_GL_MAJOR, TARGET_OPEN_GL_MINOR,2                                MIN_OPEN_GL_MAJOR, MIN_OPEN_GL_MINOR)3from .base import Platform4 5import OpenGL6 7 8__all__ = ['PygletPlatform']9 10 11class PygletPlatform(Platform):12    """Renders on-screen using a 1x1 hidden Pyglet window for getting13    an OpenGL context.14    """15 16    def __init__(self, viewport_width, viewport_height):17        super(PygletPlatform, self).__init__(viewport_width, viewport_height)18        self._window = None19 20    def init_context(self):21        import pyglet22        pyglet.options['shadow_window'] = False23 24        try:25            pyglet.lib.x11.xlib.XInitThreads()26        except Exception:27            pass28 29        self._window = None30        confs = [pyglet.gl.Config(sample_buffers=1, samples=4,31                                  depth_size=24,32                                  double_buffer=True,33                                  major_version=TARGET_OPEN_GL_MAJOR,34                                  minor_version=TARGET_OPEN_GL_MINOR),35                 pyglet.gl.Config(depth_size=24,36                                  double_buffer=True,37                                  major_version=TARGET_OPEN_GL_MAJOR,38                                  minor_version=TARGET_OPEN_GL_MINOR),39                 pyglet.gl.Config(sample_buffers=1, samples=4,40                                  depth_size=24,41                                  double_buffer=True,42                                  major_version=MIN_OPEN_GL_MAJOR,43                                  minor_version=MIN_OPEN_GL_MINOR),44                 pyglet.gl.Config(depth_size=24,45                                  double_buffer=True,46                                  major_version=MIN_OPEN_GL_MAJOR,47                                  minor_version=MIN_OPEN_GL_MINOR)]48        for conf in confs:49            try:50                self._window = pyglet.window.Window(config=conf, visible=False,51                                                    resizable=False,52                                                    width=1, height=1)53                break54            except pyglet.window.NoSuchConfigException as e:55                pass56 57        if not self._window:58            raise ValueError(59                'Failed to initialize Pyglet window with an OpenGL >= 3+ '60                'context. If you\'re logged in via SSH, ensure that you\'re '61                'running your script with vglrun (i.e. VirtualGL). The '62                'internal error message was "{}"'.format(e)63            )64 65    def make_current(self):66        if self._window:67            self._window.switch_to()68 69    def make_uncurrent(self):70        try:71            import pyglet72            pyglet.gl.xlib.glx.glXMakeContextCurrent(self._window.context.x_display, 0, 0, None)73        except Exception:74            pass75 76    def delete_context(self):77        if self._window is not None:78            self.make_current()79            cid = OpenGL.contextdata.getContext()80            try:81                self._window.context.destroy()82                self._window.close()83            except Exception:84                pass85            self._window = None86            OpenGL.contextdata.cleanupContext(cid)87            del cid88 89    def supports_framebuffers(self):90        return True91