Created
August 10, 2018 22:22
-
-
Save timlehr/42239bfe288b5a77d98663f5ae0eb153 to your computer and use it in GitHub Desktop.
Capture Maya viewport to QImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import maya.OpenMaya as om | |
import maya.OpenMayaUI as omui | |
from PySide2 import QtCore, QtGui, QtWidgets | |
# Capture viewport to MImage | |
view = omui.M3dView.active3dView() | |
mIm = om.MImage() | |
if view.getRendererName() == view.kViewport2Renderer: | |
mIm.create(view.portWidth(), view.portHeight(), 4, om.MImage.kFloat) | |
view.readColorBuffer(mIm) | |
mIm.convertPixelFormat(om.MImage.kByte) | |
else: | |
view.readColorBuffer(mIm) | |
# Prep MImage | |
mIm.verticalFlip() | |
# Get the width and height | |
wUtil = om.MScriptUtil() | |
wUtil.createFromInt(0) | |
wPtr = wUtil.asUintPtr() | |
hUtil = om.MScriptUtil() | |
hUtil.createFromInt(0) | |
hPtr = hUtil.asUintPtr() | |
mIm.getSize(wPtr, hPtr) | |
width = wUtil.getUint(wPtr) | |
height = hUtil.getUint(hPtr) | |
# byte size | |
imSize = width * height * 4 | |
# pass pointer to QImage | |
buf = ctypes.c_ubyte * imSize | |
buf = buf.from_address(long(mIm.pixels())) | |
qIm = QtGui.QImage(buf, width, height, QtGui.QImage.Format_RGB32).rgbSwapped() | |
# Test | |
pix = QtGui.QPixmap.fromImage(qIm) | |
l = QtWidgets.QLabel() | |
l.setPixmap(pix) | |
l.setScaledContents(True) | |
l.resize(640,480) | |
l.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment