Python API - M3dView - Screenshot alpha

Python API - M3dView - Screenshot alpha

Anonymous
Not applicable
2,364 Views
1 Reply
Message 1 of 2

Python API - M3dView - Screenshot alpha

Anonymous
Not applicable
I am using the following code to screen grab a viewport.

#Import api modules
import maya.OpenMaya as api
import maya.OpenMayaUI as apiUI

#Grab the last active 3d viewport
view = apiUI.M3dView.active3dView()
# Enable Alpha
view.setColorMask(1, 1, 1, 1)

#read the color buffer from the view, and save the MImage to disk
image = api.MImage()
view.readColorBuffer(image, True)
image.writeToFile('C:/test.png', 'png')


Is there any way to exclude the background in the alpha?
Seems silly that the background should act as a solid object to begin with no?
Even the view cube acts like an alpha object…

We need to screen grab (as opposed to render).

Thanks,
/Christian
0 Likes
2,365 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Seems to be a bug with this and I logged it. This is a workaround:
Set background to black, grab an instance of view port. Set the background to white and grab another instance. Then compare the pixels and create the alpha. Posting if someone else runs across this.


import maya.OpenMaya as api
import maya.OpenMayaUI as apiUI


# Set Background to black
self.setSolidBackground()
viewBlack = apiUI.M3dView.active3dView()
viewBlack.refresh()
imageBlack = api.MImage()

viewBlack.setColorMask(1, 1, 1, 1)
viewBlack.readColorBuffer(imageBlack, True)

# Set Background to white
self.setSolidBackground()
viewWhite = apiUI.M3dView.active3dView()
viewWhite.refresh()
imageWhite = api.MImage()
viewWhite.setColorMask(1, 1, 1, 1)
viewWhite.readColorBuffer(imageWhite, True)

# Set up combined image
imageCombined = api.MImage()
imageCombined.create(viewWhite.portWidth(), viewWhite.portHeight(), 4)

srcWhite = MyImg(imageWhite, viewWhite.portWidth(), viewWhite.portHeight())
srcBlack = MyImg(imageBlack, viewWhite.portWidth(), viewWhite.portHeight())
destImage = MyImg(imageCombined, viewWhite.portWidth(), viewWhite.portHeight())

for y in range(viewWhite.portHeight()):
for x in range(viewWhite.portWidth()):
color = srcWhite.GetPixel(x, y)
colorB = srcBlack.GetPixel(x, y)
destImage.SetPixel(x, y, color if color == colorB else )
ext = 'png'
tName = '%s.%s' % (fNameBase, ext)
imageCombined.writeToFile(tName, ext)
displayRGBColor(rs=1)

class MyImg(object):
def __init__(self, mimage, width, height):
self.pixelPtr = mimage.pixels()
self.width = width
self.height = height
self.su = api.MScriptUtil()

def GetLinearPixelPos(self, x, y):
return ( self.width * y + x) * 4

def GetPixel(self, x, y):
position = self.GetLinearPixelPos(x, y)
return

def SetPixel(self, x, y, color):
position = self.GetLinearPixelPos(x, y)
self.su.setUcharArray(self.pixelPtr, position+0, color)
self.su.setUcharArray(self.pixelPtr, position+1, color)
self.su.setUcharArray(self.pixelPtr, position+2, color)
self.su.setUcharArray(self.pixelPtr, position+3, color)


/Christian
0 Likes