Search code examples
pyqt4screen-capturemayaqpixmap

How can I capture maya 3d view with QPixmap?


I make a screen capture script for maya. So, I deal the job with QPixmap class.

I write the script like that.

from PyQt4 import QtCore, QtGui
import sip
import maya.cmds as cmds
import maya.OpenMayaUI as mui

def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QtCore.QObject)

pm = QtGui.QPixmap.grabWindow(getMayaWindow().winId())
pm.save('c:/test.png')

but, it can't grab the 3Dview, so it can't work.


Solution

  • Had the same issue. There is the solution by Nathan Horne which works fine. However, if you want to get the QWidget object that a certain M3dView uses, follow these steps:

    import maya.OpenMayaUI as apiUI
    from PyQt4 import QtGui, QtCore
    import sip
    
    view = apiUI.M3dView.active3dView()
    widget_ptr = view.widget()
    widget = sip.wrapinstance(long(widget_ptr), QtCore.QObject)
    pixmap = QtGui.QPixmap.grabWidget(widget)
    

    As you know, you can then save the pixmap to a file or use it directly for another QWidget. Strangely enough, the result seems a mess (or at least less predictable) and I recommend using Nathan Horne's solution.