How to fix "Could not create GL context" error [pyrender/trimesh]

I am building a 3d game from scratch on replit for fun in python. I was trying to make a very basic engine using classes to make some built in levels.
Here is my code so far:

import trimesh as tri, pyrender as r, numpy as np, io


class NewScene:
  def __init__(self):
    print(self)
    global scene, camera
    self.scene = r.Scene()
    camera = r.PerspectiveCamera(yfov=np.pi / 3.0, aspectRatio=1.0)
    light = r.SpotLight(color=np.ones(3), intensity=3.0,
                               innerConeAngle=np.pi/16.0,
                               outerConeAngle=np.pi/6.0)

    s = np.sqrt(2)/2
    cam_pose = np.array([
       [0.0, -s,   s,   0.3],
       [1.0,  0.0, 0.0, 0.0],
       [0.0,  s,   s,   0.35],
       [0.0,  0.0, 0.0, 1.0],
    ])
    
    self.scene.add(camera, pose=cam_pose)
    self.scene.add(light, pose=cam_pose)
  
  def LoadNewModel(self, x, y, model, texture=None):   
    newmodel = tri.load(model, force='mesh')
    modelmesh = r.Mesh.from_trimesh(newmodel)
    self.scene.add(modelmesh)
  
  def RenderScene(self, use_lighting=True, use_prespective=True, win_title="Uni3D Renderer: New Scene"):
    r.Viewer(self.scene, 
             use_raymond_lighting=use_lighting, 
             use_perspective_cam=use_prespective, 
             window_title=win_title, 
             refresh_rate=60.0)
    print("scene has been rendered")

  def ExitScene():
    r.on_close()
    print("scene has ended")
    

Here is the error I received:

libGL error: MESA-LOADER: failed to open swrast: ��XUU (search paths /nix/store/p36pgcv991mq5srvg765b78yqpxvk3qs-mesa-21.3.3-drivers/lib/dri, suffix _dri)
libGL error: failed to load driver: swrast
Traceback (most recent call last):
  File "main.py", line 8, in <module>
    gameLoader.Script()
  File "/home/runner/Uni3D/game/coreFiles/gameLoader.py", line 15, in Script
    scene.RenderScene()
  File "/home/runner/Uni3D/game/api.py", line 32, in RenderScene
    r.Viewer(self.scene, 
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyrender/viewer.py", line 349, in __init__
    self._init_and_start_app()
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyrender/viewer.py", line 1016, in _init_and_start_app
    super(Viewer, self).__init__(config=conf, resizable=True,
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyglet/window/xlib/__init__.py", line 133, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyglet/window/__init__.py", line 538, in __init__
    context = config.create_context(gl.current_context)
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyglet/gl/xlib.py", line 105, in create_context
    return XlibContext(self, share)
  File "/home/runner/Uni3D/venv/lib/python3.10/site-packages/pyglet/gl/xlib.py", line 127, in __init__
    raise gl.ContextException('Could not create GL context')
pyglet.gl.ContextException: Could not create GL context

All help is appreciated! Thanks!

That’s because for some reason Replit’s graphics display doesn’t work.
Also here.

1 Like

That issue was temporary and was resolved shortly after it arose.

I believe this issue is likely a result of not having access to a GPU (which is a power up you can purchase).

I am aware there is a power up, but I do not have the funds for it. Is there any other way I could fix this?

You could try running the script locally on your device?

4 Likes

It returns errors that it can’t find the modules trimesh and pyrender, even though i pip installed them. Even then, I was wondering if there was a way to get it working on replit.

Try using a venv locally

1 Like