Maya C++ API create MImage from viewport and camera


I am developing a tool for Maya with the OpenMaya API in C++.
My goal is to retrieve an image from a specific camera and specific objects, for testing purposes I would like to have a viewport image.

My first question is to know if there is a way to do this through the Maya API only ? Or should I pass by lets say OpenGL to render my own image ?

If it is preferable for me to use OpenGL, is there a way to retrieve Vertex Buffer from a specific object, so I can use this, its World Matrix and the camera’s World Matrix to generate the image ?

Thanks a lot for your help, please let me know if that’s unclear :slight_smile:

Cheers !

To begin with, you can get acquainted with the functionality of the M3dView class
This class contains functions that meet many of your needs:
OpenMayaUI.M3dView Class Reference

Tiny demo:

import maya.cmds as cmds
import maya.api.OpenMaya as om2
import maya.api.OpenMayaUI as omui2

def maya_useNewAPI():

# or:
# maya_useNewAPI = True

image_format = "jpg"
image_snapshot_01 = r'c:\temp\shapshot_01.jpg'
image_snapshot_02 = r'c:\temp\shapshot_02.jpg'

cmds.toggleAxis(origin = false)

current_view = omui2.M3dView.active3dView()
current_image = om2.MImage()
current_image.create(current_view.portWidth(), current_view.portHeight())

                  edit = True,
                  grid = False,
    useDefaultMaterial = True,
     wireframeOnShaded = True)

current_view.refresh(force = True, all = False)
current_view.readColorBuffer(current_image, True)
current_image.writeToFile(image_snapshot_01, outputFormat = image_format)

                  edit = True,
    useDefaultMaterial = False,
     wireframeOnShaded = False)

current_view.refresh(force = True, all = False)
current_view.readColorBuffer(current_image, True)
current_image.writeToFile(image_snapshot_02, outputFormat = image_format)

cmds.modelEditor('modelPanel4', edit = True, grid = True)
current_view.refresh(force = True, all = False)

Depending on the goals and conditions (Viewport 2.0. / OpenGL\DX / legacy default viewport), for effective processing you will have to use both OpenMayaUI, and OpenMayaRender, and OpenGL.GL :slight_smile:

Here are links to great examples from the documentation for maya.api.OpenMaya.*:








Almost all examples (directly or indirectly) use the Viewport 2.0 API.
A good starting point is this documentation:

Viewport 2.0 API

Harmony and good luck!

1 Like

Thanks a lot for your answer.

I have tried to use this method with C++ and the M3dView.readColorBuffer is deprecated since Maya 2019. I am supposed to be using MHWRender::MRenderTargetManager::acquireRenderTarget instead, but I am having a really hard time to set this up.

At the moment I have something like this, but I am probably missing something because it fails to create the MRenderOverride and it displays the error “Failed to find viewport render override.” :

void captureViewportImage(const MString& filePath) {
	MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
	if (!renderer) {
		MGlobal::displayError("Viewport 2.0 renderer not available.");

	const MString& overrideName = renderer->activeRenderOverride();
	const MHWRender::MRenderOverride* override = renderer->findRenderOverride(overrideName);
	if (!override) {
		MGlobal::displayError("Failed to find viewport render override.");

	const MHWRender::MRenderTargetManager* targetManager = renderer->getRenderTargetManager();
	MHWRender::MRenderTargetDescription targetDesc;
	MHWRender::MRenderTarget* renderTarget = targetManager->acquireRenderTarget(targetDesc);
	if (!renderTarget) {
		MGlobal::displayError("Failed to acquire render target.");

	MHWRender::MDrawContext* context = MHWRender::MRenderUtilities::acquireSwatchDrawContext(renderTarget);
	MHWRender::MTextureManager* textureManager = renderer->getTextureManager();
	MHWRender::MTexture* colorTexture = context->copyCurrentColorRenderTargetToTexture();
	textureManager->saveTexture(colorTexture, filePath);


I am new to all this viewport stuff so I am probably missing something, and not understanding how it actually works.

Who would have thought… (s)
Yes, this is true for OpenMaya-1/C++.
But in maya.api.OpenMaya (OpenMaya-2) this works correctly with Viewport 2, the function is automatically completed correctly.
Here’s a recommendation from the Maya developers themselves:
Get image from M3dView::readColorBuffer in Viewport 2

import maya.OpenMaya as openmaya
import maya.OpenMayaUI as openmayaUI

view = openmayaUI.M3dView.active3dView()
img = openmaya.MImage()
if view.getRendererName() == view.kViewport2Renderer:       
    img .create(view.portWidth(), view.portHeight(), 4, openmaya.MImage.kFloat)

Despite the fact that the entire M3dView class (in the latest Viewport 2.0 API documentation) is positioned as “deprecated”, in the examples (the latest Viewport 2.0 API documentation) this class is used very actively. :slight_smile:
Because for simple needs, this method is simpler and just as effective.

To my great regret, I cannot help you with checking the C++ code :frowning:
Maybe something like this:

1 Like