Switching OpenGL contexts in Blender (PySide2 GL contexts)

Hello all,

I am trying to get a PySide2 (Qt5) app to run inside of Blender, using bpy.app.timers to pump the event loop. So far I have had some success trying to do this, however the PySide2 app wants to freely create OpenGL contexts and this is where I get some instabilities.

From my current discoveries, out of the box the app in-question ( https://gafferhq.org/ ) does seem to launch and I can use it, but the moment I go back to Blender it segfaults. Running in a debugger I discovered that Blender is breaking in the GUI code not finding its own buffer objects, which implies that the OpenGL context might need restoring after Gaffer is done with it somehow.

I do a little more digging and I make sure the GLWidget in Gaffer to share the current context, as this is how Houdini/Maya is set-up to do when Gaffer runs inside of those host apps for Linux (whatever context is bound to when the timers call is made, as I can’t set this explicitly from bpy at all), and in this mode I am not getting crashes! However, I’ve found that both apps are quite glitchy, which could be that the OpenGL is in a different state after Gaffer is done with it.

I have some ideas which might fix this issue, but it involves exposing some calls from bpy to access either the window manager or “Ghost” directly to get/set contexts. Before I delve into this scenario and submitting a patch back which might not make it into mainline, I thought I’d ask here if there’s a better solution to this problem. Ideally sharing context might be really cool, but I would be happy to just have Gaffer manage its own context and just restore Blender’s one after the timer call.

Just for OS context, at the moment I am just trying to get Linux X11/GLX to work, as Gaffer only supports this mode for context-sharing.