pythonopenglpygameheadlesspsychopy

PsychoPy headless on Windows 10


Problem: I am trying to run PsychoPy headless on Windows 10 but am struggling to do so.

Error: Currently, my window type is set to "pygame," so I tried running os.environ["SDL_VIDEODRIVER"] = "dummy" at the top of my code, but got: pygame.error: OpenGL not available.

Code: This is where I specify the window and were I am getting the error:

os.environ["SDL_VIDEODRIVER"] = "dummy"

# This line gives the error
win = visual.Window(
    size=[1440, 900], fullscr=False, screen=0,
    winType='pygame', allowGUI=False, allowStencil=False,
    monitor='testMonitor', color=[-1,-1,-1], colorSpace='rgb',
    blendMode='avg', useFBO=True)

Extra info: I am open to using another window type like Pyglet if it would make running headless easier, but right now I am kind of stuck. I have thought about trying to make OpenGL run headless instead of Pygame, but I don't know how to do that. If someone could explain how to get OpenGL/PyOpenGL to run headless I could try that.


Solution

  • OK, I think the keyword you're looking for is "offscreen rendering".

    After a bit more digging, I think the best approach is to indeed modify PsychoPy: first, you would have to use a GLFW window instead of pygame, and you need to modify visual/backends/glfwbackend.py:

    In line 302, try to insert glfw.window_hint(glfw.VISIBLE, 0) and see if that works for your usecase.

    Leftover previous examples for PyOpenGL: https://github.com/AntonOvsyannikov/PyOpenGL_Examples_SaveToFile