Sharing OpenGL resources in QGLWidgets with Viewport 2.0

You can directly access a QGLWidget whose context is shared with other Viewport 2.0 resources. This is useful in the case when your plug-in creates OpenGL resources for it's own QGLWidget and you want it to share the plug-in's OpenGL resources with Viewport 2.0.

Using Python

import PySide2
import PySide2.QtOpenGL
import PySide2.QtWidgets
import shiboken2
#Get the property from the application (as a QWidget)
widget = PySide2.QtWidgets.QApplication.instance().property("mayaSharedQGLWidget")
#Get the raw C++ pointer
ptr =shiboken2.getCppPointer(widget)[0]
#Downcast to QGLWidget
glWidget = shiboken2.wrapInstance(ptr, PySide2.QtOpenGL.QGLWidget)
#Print the format of the current rendering context
print (glWidget.format())

Using C++

QVariant v = qApp->property("mayaSharedQGLWidget");
if (v.isValid())
{
    QGLWidget glWidget = dynamic_cast<QGLWidget*>(v.value<QWidget*>());
}