我正在测试OSMesa是否正常运行,但遇到以下错误。如何解决这个错误?
完整的错误消息:
---------------------------------------------------------------------------
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'
最小可重现示例:
from OpenGL import osmesa
ctx = osmesa.OSMesaCreateContext(osmesa.OSMESA_RGBA, None)
我的环境如下:
pyrender==0.1.45
OpenGL==3.1.0
要解决此问题,应将环境变量
PYOPENGL_PLATFORM
设置为 osmesa
。 pyrender 文档建议在执行程序时在 shell 中执行此操作:
PYOPENGL_PLATFORM=osmesa python render.py
或者在程序顶部添加以下行:
import os
os.environ["PYOPENGL_PLATFORM"] = "osmesa"
否则,如错误所示:GLX平台不提供OSMesa功能。