pythonlinuxpyopenglglxosmesa

'GLXPlatform' object has no attribute 'OSMesa'


I am testing whether OSMesa functions properly, but I encountered the following error. How can this error be resolved?

The full error message:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[1], line 2
      1 from OpenGL import GL
----> 2 from OpenGL import osmesa
      4 ctx = osmesa.OSMesaCreateContext(osmesa.OSMESA_RGBA, None)
      5 if ctx:

File ~/anaconda3/envs/flame/lib/python3.8/site-packages/OpenGL/osmesa/__init__.py:2
      1 from OpenGL.raw.osmesa._types import *
----> 2 from OpenGL.raw.osmesa.mesa import *

File ~/anaconda3/envs/flame/lib/python3.8/site-packages/OpenGL/raw/osmesa/mesa.py:34
     30 OSMesaGetCurrentContext = _p.GetCurrentContext
     32 @_f
     33 @_p.types(OSMesaContext,GLenum, OSMesaContext)
---> 34 def OSMesaCreateContext(format,sharelist): pass
     36 @_f
     37 @_p.types(OSMesaContext,GLenum, GLint, GLint, GLint, OSMesaContext)
     38 def OSMesaCreateContextExt(format, depthBits, stencilBits,accumBits,sharelist ): pass

File ~/anaconda3/envs/flame/lib/python3.8/site-packages/OpenGL/raw/osmesa/mesa.py:9, in _f(function)
      7 def _f( function ):
      8     return _p.createFunction( 
----> 9         function,_p.PLATFORM.OSMesa,
     10         None,
     11         error_checker=None
     12     )

AttributeError: 'GLXPlatform' object has no attribute 'OSMesa'

Minimal reproducible example:

from OpenGL import osmesa

ctx = osmesa.OSMesaCreateContext(osmesa.OSMESA_RGBA, None)

My environment as follows:

pyrender==0.1.45
OpenGL==3.1.0

Solution

  • To resolve this, the environment variable PYOPENGL_PLATFORM should be set to osmesa before importing pyrender. The pyrender documentation suggests to do this either in the shell when executing the program:

    PYOPENGL_PLATFORM=osmesa python render.py
    

    or by adding the following lines at the top of the program:

    import os
    os.environ["PYOPENGL_PLATFORM"] = "osmesa"
    

    Otherwise, it is as the error indicates: the GLX platform doesn't provide OSMesa functionality.


    Full code example:

    import os
    os.environ["PYOPENGL_PLATFORM"] = "osmesa"
    
    from OpenGL import osmesa
    
    ctx = osmesa.OSMesaCreateContext(osmesa.OSMESA_RGBA, None)
    
    if ctx:
        print("OSMesa is working correctly")