question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attempt to retrieve context when no valid context

See original GitHub issue

Im following a very simple code example to draw something on screen using glfw and opengl but I get met with

Traceback (most recent call last):
  File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/src/__unused/test.py", line 75, in <module>
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
  File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/latebind.py", line 63, in __call__
    return self.wrapperFunction( self.baseFunction, *args, **named )
  File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/GL/VERSION/GL_2_0.py", line 469, in glVertexAttribPointer
    contextdata.setValue( key, array )
  File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/contextdata.py", line 58, in setValue
    context = getContext( context )
  File "/home/nanyo/Documents/ProgrammingEnvs/PythonEnvs/pyVisuals/lib/python3.10/site-packages/OpenGL/contextdata.py", line 40, in getContext
    raise error.Error(
OpenGL.error.Error: Attempt to retrieve context when no valid context

I have reduced my code as much as possible so I can post it here, I am doing glfw.make_context_current(window) to make the context current so I am not sure what is going wrong:

import glfw
from OpenGL.GL import *
import numpy as np

glfw.init()

glfw.window_hint(glfw.FOCUSED, glfw.FALSE)

window = glfw.create_window(640, 400, "Window", None, None)
glfw.make_context_current(window)

vertices = [
    0.5,  0.5, 0.0,
    0.5, -0.5, 0.0,
    -0.5, -0.5, 0.0,
    -0.5,  0.5, 0.0
]

indices = [
    0, 1, 3,
    1, 2, 3
]

vertices = np.array(vertices, dtype=np.float32)
indices = np.array(indices, dtype=np.int32)

vertex_shader_source = """
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
"""

frag_shader_source = """
#version 330 core
out vec4 FragColor;
void main()
{
    FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
}
"""

vertex_shader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertex_shader, [vertex_shader_source])
glCompileShader(vertex_shader)
print(glGetShaderiv(vertex_shader, GL_COMPILE_STATUS))

frag_shader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(frag_shader, [frag_shader_source])
glCompileShader(frag_shader)
print(glGetShaderiv(frag_shader, GL_COMPILE_STATUS))

shader_program = glCreateProgram()
glAttachShader(shader_program, vertex_shader)
glAttachShader(shader_program, frag_shader)
glLinkProgram(shader_program)
print(glGetProgramiv(shader_program, GL_LINK_STATUS))

glDeleteShader(vertex_shader)
glDeleteShader(frag_shader)

VAO = glGenVertexArrays(1)
VBO = glGenBuffers(1)
EBO = glGenBuffers(1)

glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)

glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)

glClearColor(1, 0, 0, 1)

while not glfw.window_should_close(window):
    glClear(GL_COLOR_BUFFER_BIT)
    glUseProgram(shader_program)
    glBindVertexArray(VAO)
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0)
    glfw.swap_buffers(window)
    glfw.poll_events()

glfw.terminate()

Putting:

print(glfw.get_current_context())

just before:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 12, ctypes.c_void_p(0))

prints <glfw.LP__GLFWwindow object at 0x7f822c987340> meaning that some context was created for that window, as far as I understand the glfw docs.

EDIT: might be worth mentioning that I compiled glfw from source and replaced the wheel package with this newly compiled version as glfw 3.3.x does not work with ubuntu 21 and its wayland version

EDIT2: I am currently investigating if this has potentially something to do with my nvidia drivers and my gpu not showing up on some places as it should. Although I have an igpu capable of OpenGL 4.6 support

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
FlorianRhiemcommented, Mar 13, 2022

I can’t tell you what the future will hold for OpenGL and I don’t know your use cases, but take a look at a Vulkan tutorial for drawing a single triangle. Vulkan is a wee bit more verbose and complex, the learning curve isn’t any less steep than that of OpenGL. I think even if you don’t end up using OpenGL all that much, it can be a useful tool to learn and understand.

1reaction
FlorianRhiemcommented, Mar 13, 2022

There are plenty of tutorials for OpenGL in general, but for pitfalls such as this, I think suffering is the way to go. 😂 Though it’s not as bad as my statement might’ve made it seem, it’s just that issues like this one that can be pretty disheartening/annoying, when you don’t even manage to draw something.

Also: note that you actually got it right first try with glVertexAttribPointer, you’re passing a null pointer there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python OpenGL - No valid context in thread - Stack Overflow
... getContext """Attempt to retrieve context when no valid context""" OpenGL.error.Error: Attempt to retrieve context when no valid context.
Read more >
glcube.py OpenGL.error.Error: Attempt to retrieve context ...
Hi, I am getting this error trying to run examples/glcube.py. Any ideas? Ubuntu 20.04 pygame 2.1.2 (SDL 2.0.16, Python 3.8.10) python ...
Read more >
[PyOpenGL-Users] Attempt to retrieve context when no valid ...
Hi, I am running an app in an PyQt QGLWidget. If the window is minimized, and the method for rendering the object that...
Read more >
Cura 13.03 error: 'Attempt to retrieve context when no valid ...
When Cura starts, a popup window shows this message 'Attempt to retrieve context when no valid context', and then the 3D view is...
Read more >
How can I handle errors when there is no valid OpenGL context?
You can use these vendor specific functions: wglGetCurrentContext() on Windows,; aglGetCurrentContext() on OSX,; glxGetCurrentContext() on ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found