mmatl / pyrender

Easy-to-use glTF 2.0-compliant OpenGL renderer for visualization of 3D scenes.
http://pyrender.readthedocs.io/
MIT License
1.29k stars 225 forks source link

pyglet.gl.ContextException: Could not create GL context #13

Closed henryclever closed 5 years ago

henryclever commented 5 years ago

Hi, I'm trying to run the following code:

import trimesh
import pyrender
tm = trimesh.load('/home/henry/Downloads/fuze.obj')
pymesh = pyrender.Mesh.from_trimesh(tm)

scene = pyrender.Scene()
scene.add(pymesh)
pyrender.Viewer(scene, use_raymond_lighting=True)

And I get the following error:

File "generate_pose_dataset.py", line 172, in mesh_render
    pyrender.Viewer(scene, use_raymond_lighting=True)
  File "/usr/local/lib/python2.7/dist-packages/pyrender/viewer.py", line 347, in __init__
    self._init_and_start_app()
  File "/usr/local/lib/python2.7/dist-packages/pyrender/viewer.py", line 995, in _init_and_start_app
    height=self._viewport_size[1])
  File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/__init__.py", line 170, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/window/__init__.py", line 595, in __init__
    context = config.create_context(gl.current_context)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 216, in create_context
    return XlibContextARB(self, share)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 322, in __init__
    super(XlibContext13, self).__init__(config, share)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 230, in __init__
    raise gl.ContextException('Could not create GL context')
pyglet.gl.ContextException: Could not create GL context

My computer shows that PyOpenGL 3.1.0, PyOpenGL_accelerate 3.1.0, and pyglet appear to be correctly installed.

To test pyglet externally from pyrender, I ran the following code:

import pyglet
pyglet.window.Window()

and a window opens as expected.

Any ideas why this error is occurring?

I'm running 16.04 with 64 bit.

Thanks! Henry C.

mmatl commented 5 years ago

Are you running this over SSH?

henryclever commented 5 years ago

Hi,

Thanks for the quick response! No, I am not running over ssh. It is all local.

Henry C.

mmatl commented 5 years ago

Which display drivers are you running? NVIDIA? If so, which version? The error message means that your system was unable to create an OpenGL 3.0+ context, which could be caused by faulty display drivers.

henryclever commented 5 years ago

No Nvidia GPU on this machine. Here's what I've got:


  *-display               
       description: VGA compatible controller
       product: Sky Lake Integrated Graphics
       vendor: Intel Corporation
       physical id: 2
       bus info: pci@0000:00:02.0
       version: 07
       width: 64 bits
       clock: 33MHz
       capabilities: pciexpress msi pm vga_controller bus_master cap_list rom
       configuration: driver=i915_bpo latency=0
       resources: irq:132 memory:de000000-deffffff memory:c0000000-cfffffff ioport:f000(size=64)

Blender works fine, if that's any indication about the display drivers being faulty or not.

I've got a 1070Ti with Cuda 9.2 on my work computer but it would be nice to get this working on my laptop.

Henry C.

mmatl commented 5 years ago

Could you run the following tiny script and let me know what it outputs?

from pyglet.window import Window
from pyglet.gl import Config;
w = Window(config=Config(major_version=4, minor_version=1))
print('{}.{}'.format(w.context.config.major_version, w.context.config.minor_version))
henryclever commented 5 years ago

Yes:

File "generate_pose_dataset.py", line 176, in mesh_render
    w = Window(config=Config(major_version=4, minor_version=1))
  File "/usr/local/lib/python2.7/dist-packages/pyglet/window/xlib/__init__.py", line 170, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/window/__init__.py", line 595, in __init__
    context = config.create_context(gl.current_context)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 216, in create_context
    return XlibContextARB(self, share)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 322, in __init__
    super(XlibContext13, self).__init__(config, share)
  File "/usr/local/lib/python2.7/dist-packages/pyglet/gl/xlib.py", line 230, in __init__
    raise gl.ContextException('Could not create GL context')
pyglet.gl.ContextException: Could not create GL context
mmatl commented 5 years ago

Ah, interesting. Yeah, you're not able to create an OpenGL 3+ context, which is a problem for pyrender since it uses modern OpenGL for better efficiency. I'd try uninstalling and re-installing PyOpenGL_accelerate, and make sure it builds properly. Let me know if that fixes anything.

henryclever commented 5 years ago

Hi,

I've tried that, both (1) using pip and (2) from source. It does not appear to have solved the issue. As stated in the first post, it works when you leave the input for Window() blank. I'll keep poking around bit. Couldn't get PyMesh running either with a different but similarly frustrating problem.

Warmth, Henry C.

mmatl commented 5 years ago

@henryclever Yeah, sorry about that :( It seems that pyglet is just having a struggle getting an OpenGL 3+ context on your machine. If you can get that short script I gave you to work, everything will work in Pyrender, but unfortunately it may require some tweaks to pyglet. I would debug this further for you, but without your hardware, I'm unable to replicate the issue. For now, I'll leave the issue open. Let me know if there's anything else I can do to help you out.

henryclever commented 5 years ago

Thanks for the attention to this!

So, I've made some progress.

A key problem was that I was getting a GPU error when I used the command glxinfo | grep PyOpenGL. I installed the following to make it go away:

sudo apt-get install linux-generic-lts-wily xserver-xorg-lts-wily libgl1-mesa-glx-lts-wily libglapi-mesa-lts-wily libwayland-egl1-mesa-lts-wily libgl1-mesa-glx-lts-wily:i386 libglapi-mesa-lts-wily:i386

However, I was still getting an error when creating the GL context with pyglet. The problem seems quite silly now. I had installed pyglet with the following: sudo pip install pyglet

This installs pyglet 1.3.2, which throws an error upon executing Window(config=Config(major_version=4, minor_version=1)). However, if I installed install pyglet with aptitude: sudo apt-get install python-pyglet

Then pyglet 1.1.4 is installed, and the error goes away. FYI, When I was building pyglet from source, I was also using 1.3.2.

However, an error is still present with the following code: print('{}.{}'.format(w.context.config.major_version, w.context.config.minor_version)) as well as with: pyrender.Viewer(scene, use_raymond_lighting=True).

Error:

    print('{}.{}'.format(w.context.config.major_version, w.context.config.minor_version))
AttributeError: 'XlibGLConfig13' object has no attribute 'major_version'

And I'm haven't figured it out yet. I'll keep prying, but any if you have any tips, I'm all ears!

Warmth, Henry C.

henryclever commented 5 years ago

Also: the error is slightly different with pyrender.Viewer(scene). Here it is:

    pyrender.Viewer(scene, use_raymond_lighting=True)
  File "/usr/local/lib/python2.7/dist-packages/pyrender/viewer.py", line 347, in __init__
    self._init_and_start_app()
  File "/usr/local/lib/python2.7/dist-packages/pyrender/viewer.py", line 996, in _init_and_start_app
    if self.context.config.major_version < 3:
AttributeError: 'XlibGLConfig13' object has no attribute 'major_version'
mmatl commented 5 years ago

The error you're now seeing is because of code changes between Pyglet version 1.1.4 and 1.3.2, I believe. Basically, somewhere between those versions, the XlibGLConfig13 type got updated. You'll want to get this working with a modern (>1.3) version of Pyglet if possible.

henryclever commented 5 years ago

Got it!

Thanks for all the help.

Ran into yet more problems with my Intel GPU on my labtop. I installed pyrender on my work computer that has a 1070Ti (also running Ubuntu) and pyrender worked in less than a minute. Something about Ubuntu and Nvidia products .... things just seem to work easier in general!

I'll post here again if I find a solution to my Intel GPU problems regarding pyrender.

H

mmatl commented 5 years ago

Sounds good, sorry for your trouble! I have pyrender running on my Macbook with an Intel GPU, so it might be something even more version-specific...

On Tue, Mar 26, 2019 at 11:22 AM Henry M. Clever notifications@github.com wrote:

Got it!

Thanks for all the help.

Ran into yet more problems with my Intel GPU on my labtop. I installed pyrender on my work computer that has a 1070Ti (also running Ubuntu) and pyrender worked in less than a minute. Something about Ubuntu and Nvidia products .... things just seem to work easier in general!

I'll post here again if I find a solution to my Intel GPU problems regarding pyrender.

H

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mmatl/pyrender/issues/13#issuecomment-476784828, or mute the thread https://github.com/notifications/unsubscribe-auth/AGNyRvFIrDjv8ivwkItGPT7mAN5LHxybks5vamVigaJpZM4cEJ5M .

oarriaga commented 5 years ago

Hi I had a similar issue and fixed it by looking at which OpenGL version I had by calling:

glxinfo | grep OpenGL
>> OpenGL core profile version string: 3.3 (Core Profile) Mesa 19.1.3

Then I changed in the constants.py file in pyrender.py (you can find it wherever you installed pyrender in my case: ~/.local/lib/python3.7/site-packages/pyrender/constants.py)

OPEN_GL_MAJOR = 3
OPEN_GL_MINOR = 3

After that I was able to render :)!

oarriaga commented 5 years ago

@mmatl Maybe it would be a good idea to create a config file to change the OpenGL version without having to change the source code

neonb88 commented 5 years ago

@henryclever Did you fix your issue? I think I bumped into an identical one, still going through your steps.

Cheers, Nathan

neonb88 commented 5 years ago

@oarriaga Your comment fixed the issue for me!

I mean this comment: " Then I changed in the constants.py file in pyrender.py (you can find it wherever you installed pyrender in my case: ~/.local/lib/python3.7/site-packages/pyrender/constants.py)

OPEN_GL_MAJOR = 3 OPEN_GL_MINOR = 3 "

@henryclever I hope this solves the issue for you and any future readers.

monacv commented 3 years ago

@mmatl

(smplifyx) mona@ubuntu:~/mona/code$ python test_piglet.py
Traceback (most recent call last):
  File "test_piglet.py", line 1, in <module>
    from pyglet.window import Window
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/window/__init__.py", line 1897, in <module>
    gl._create_shadow_window()
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/gl/__init__.py", line 206, in _create_shadow_window
    _shadow_window = Window(width=1, height=1, visible=False)
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/window/xlib/__init__.py", line 173, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/window/__init__.py", line 606, in __init__
    context = config.create_context(gl.current_context)
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 204, in create_context
    return XlibContextARB(self, share)
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 314, in __init__
    super(XlibContext13, self).__init__(config, share)
  File "/home/mona/venv/smplifyx/lib/python3.6/site-packages/pyglet/gl/xlib.py", line 218, in __init__
    raise gl.ContextException('Could not create GL context')
pyglet.gl.ContextException: Could not create GL context

and this is your code snippet:


(smplifyx) mona@ubuntu:~/mona/code$ cat test_piglet.py 
from pyglet.window import Window
from pyglet.gl import Config;
w = Window(config=Config(major_version=4, minor_version=1))
print('{}.{}'.format(w.context.config.major_version, w.context.config.minor_version))

and I have:

$ glxinfo | grep OpenGL
X Error of failed request:  BadValue (integer parameter out of range for operation)
  Major opcode of failed request:  152 (GLX)
  Minor opcode of failed request:  24 (X_GLXCreateNewContext)
  Value in failed request:  0x0
  Serial number of failed request:  41
  Current serial number in output stream:  42
XihuaQiao commented 3 years ago

Hi, I met with the same problem. I wonder where should I put_' OPEN_GL_MAJOR = 3, OPEN_GL_MINOR = 3'? Did you mean global variables? This is my constant.py file. I try to add OPEN_GL_MAJOR = 3, OPEN_GL_MINOR = 3_after this, and it still didn't work. image

nicolasugrinovic commented 3 years ago

@mmatl I have the same problem as @henryclever, however I am running over SSH.


  File "/mnt/Data/nugrinovic/code/visualize/renderer.py", line 85, in __init__
    self.renderer = pyrender.OffscreenRenderer(height, width)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyrender/offscreen.py", line 31, in __init__
    self._create()
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyrender/offscreen.py", line 149, in _create
    self._platform.init_context()
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyrender/platforms/pyglet_platform.py", line 52, in init_context
    width=1, height=1)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyglet/window/xlib/__init__.py", line 173, in __init__
    super(XlibWindow, self).__init__(*args, **kwargs)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyglet/window/__init__.py", line 606, in __init__
    context = config.create_context(gl.current_context)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyglet/gl/xlib.py", line 204, in create_context
    return XlibContextARB(self, share)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyglet/gl/xlib.py", line 314, in __init__
    super(XlibContext13, self).__init__(config, share)
  File "/home/nugrinovic/miniconda3/envs/torch1.4/lib/python3.7/site-packages/pyglet/gl/xlib.py", line 218, in __init__
    raise gl.ContextException('Could not create GL context')
nicolasugrinovic commented 3 years ago

Just for the record, I added:

os.environ['PYOPENGL_PLATFORM'] = 'egl'

to the main script and solved the issue.

kumar665manjeet commented 3 years ago

Are you running this over SSH?

I need answer of this problem, can you help...

HaFred commented 3 years ago

Are you running this over SSH?

I need answer of this problem, can you help...

For SSH, seems like the only way is to use the offscreen rendering. I am also curious about how can we run pyrender.Viewer(scene, use_raymond_lighting=True) with SSH, please? @mmatl Thank you!

zouyuancc commented 3 years ago

Are you running this over SSH?

I need answer of this problem, can you help...

I just reboot to solve the problem,but I don't know why,you can try to see whether it work or not

kumar665manjeet commented 3 years ago

I reboot my ssh server, now not able to connect it, showing timeout error

UttaranB127 commented 2 years ago

Hi I had a similar issue and fixed it by looking at which OpenGL version I had by calling:

glxinfo | grep OpenGL
>> OpenGL core profile version string: 3.3 (Core Profile) Mesa 19.1.3

Then I changed in the constants.py file in pyrender.py (you can find it wherever you installed pyrender in my case: ~/.local/lib/python3.7/site-packages/pyrender/constants.py)

OPEN_GL_MAJOR = 3
OPEN_GL_MINOR = 3

After that I was able to render :)!

I was facing a very similar issue and this was the solution that finally fixed it for me! One small update: in my pyrender/constants.py file, I had to make the changes

TARGET_OPEN_GL_MAJOR = 3
TARGET_OPEN_GL_MINOR = 3
cleemesser commented 2 years ago

Writing from 2022, it is useful to note that many things can cause this error. It sounds like anything that disrupts creating the context, from very general problems with accessing underlying C libraries, to specific issues like like of access to the display or inadequate OpenGL version being available.

I just had this error when using a conda environment installed version of pyrender and pyglet after upgrading from ubuntu 20.04 to 22.04. Evidently glibc was upgraded from 20.04->22.04, so the libraries installed in the conda environment previously.

Documentation of what to do is discussed in: libGL Error with Ubuntu 22.04 fix in https://github.com/conda-forge/ctng-compilers-feedstock/pull/98

Newly created conda environments pulling from conda-forge will not have this problem as in May conda-forge updated to use more recent libraries.

OleBialas commented 1 year ago

I still had the problem after re-installing anaconda and creating a new environment but it was fixed by running conda install -c conda-forge libstdcxx-ng (on Ubuntu 22.04)

toannguyen1904 commented 1 year ago

I still had the problem after re-installing anaconda and creating a new environment but it was fixed by running conda install -c conda-forge libstdcxx-ng (on Ubuntu 22.04)

Worked on Ubuntu 22.04. Thanks a lot!!!!!

sunyuan789 commented 1 year ago

I still had the problem after re-installing anaconda and creating a new environment but it was fixed by running conda install -c conda-forge libstdcxx-ng (on Ubuntu 22.04)

Thanks a lot!!!!!!!!!!! It works for me on WSL2(Ubuntu22.04)!

YixuannnL commented 6 months ago

Hi I had a similar issue and fixed it by looking at which OpenGL version I had by calling:

glxinfo | grep OpenGL
>> OpenGL core profile version string: 3.3 (Core Profile) Mesa 19.1.3

Then I changed in the constants.py file in pyrender.py (you can find it wherever you installed pyrender in my case: ~/.local/lib/python3.7/site-packages/pyrender/constants.py)

OPEN_GL_MAJOR = 3
OPEN_GL_MINOR = 3

After that I was able to render :)!

I was facing a very similar issue and this was the solution that finally fixed it for me! One small update: in my pyrender/constants.py file, I had to make the changes

TARGET_OPEN_GL_MAJOR = 3
TARGET_OPEN_GL_MINOR = 3

It also works for me. version of my openGL is 1.4, so I only modify:

TARGET_OPEN_GL_MAJOR = 3

but then I encounter another error:

OpenGL.error.NullFunctionError: Attempt to call an undefined function glGenVertexArrays, check for bool(glGenVertexArrays) before calling

Can anyone help me with this out?

chaitanya1chawla commented 2 months ago

In an anaconda environment, I was able to solve it by conda install -c conda-forge libstdcxx-ng

Ubuntu 22.04, python 3.7

YixuannnL commented 2 months ago

您好,我已经收到您的邮件。我将尽快给您回复。谢谢!