Skip to content

Instantly share code, notes, and snippets.

@hmasato
Last active August 17, 2018 01:36
Show Gist options
  • Select an option

  • Save hmasato/b72a95fbadf1c63b56ec to your computer and use it in GitHub Desktop.

Select an option

Save hmasato/b72a95fbadf1c63b56ec to your computer and use it in GitHub Desktop.
[Maya, python, PyQt, PySide] grabFrameBufferToQImage (MImage and QPixmap) #screenshot
#hmasato
#https://gist.github.com/hmasato/b72a95fbadf1c63b56ec
#------------------------------
#grab frame buffer to QImage
#------------------------------
import ctypes
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
from PyQt4.QtGui import QImage
#from PySide.QtGui import QImage
view = omui.M3dView.active3dView()
width = view.portWidth()
height = view.portHeight()
img = om.MImage()
#view.readColorBuffer(img, True)
view.readColorBuffer(img, False)
ptr = ctypes.cast(img.pixels().__long__(), ctypes.POINTER(ctypes.c_char))
ptrAsStr = ctypes.string_at(ptr, width * height * 4)
qimg = QImage(ptrAsStr, width, height, QImage.Format_ARGB32)
#qimg = qimg.rgbSwapped().mirrored(horizontal=False, vertical=True)
qimg = qimg.mirrored(horizontal=False, vertical=True)
qimg.save('C:/screenshot.png')
'''
#convert to QPixmap
from PyQt4.QtGui import QPixmap
#from PySide.QtGui import QPixmap
#qpix = QPixmap().convertFromImage(qimg)
qpix = QPixmap().fromImage(qimg)
qpix.save('C:/screenshot.png', 'png')
'''
'''
#------------------------------
#grab frame buffer to MImage
#------------------------------
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
view = omui.M3dView.active3dView()
img = om.MImage()
view.readColorBuffer(img, True)
img.writeToFile('C:/screenshot.png', 'png')
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment