Here is where I landed on this:
I did some more experimentation and found some strange behavior. If you set
the shader of an object to be 50%, the screen capture, using the snippet
(below) will have 50% alpha for that object. As you dial the transparency
towards 0% or 100%, the alpha will become increasingly white! I've logged
it as a bug and moved on.
Workaround - grab one viewport snapshot with black background and one with
white and then build the alpha from the difference. Posting it here if
someone else runs across this.
import maya.OpenMaya as api
import maya.OpenMayaUI as apiUI
# Set Background to black
self.setSolidBackground([0, 0, 0])
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([0, 0, 0])
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 [0, 0, 0, 0])
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 [
self.su.getUcharArrayItem(self.pixelPtr, position+0),
self.su.getUcharArrayItem(self.pixelPtr, position+1),
self.su.getUcharArrayItem(self.pixelPtr, position+2),
self.su.getUcharArrayItem(self.pixelPtr, position+3)]
def SetPixel(self, x, y, color):
position = self.GetLinearPixelPos(x, y)
self.su.setUcharArray(self.pixelPtr, position+0, color[0])
self.su.setUcharArray(self.pixelPtr, position+1, color[1])
self.su.setUcharArray(self.pixelPtr, position+2, color[2])
self.su.setUcharArray(self.pixelPtr, position+3, color[3])
On Tuesday, November 27, 2012 3:17:52 PM UTC-8, Python inside Maya wrote:
>
> I am using the following code to screen grab a custom viewport (thanks
> Nathan Horne)
> #Import api modules
> import maya.OpenMaya as api
> import maya.OpenMayaUI as apiUI
>
> #Grab the last active 3d viewport
> view = apiUI.M3dView.active3dView()
>
> #read the color buffer from the view, and save the MImage to disk
> image = api.MImage()
>
view.setColorMask(1, 1, 1, 1)
> view.readColorBuffer(image, True)
>
> image.writeToFile('C:/test.png', 'png')
>
> Is there any way to include alpha in the image? I just want the background
> alpha'd out.
> I don't have a lot of API experience in this area and would love any
> pointer (no pun intended) I can get.
>
> Thanks,
> /Christian
>
--
view archives: http://groups.google.com/group/python_inside_maya
change your subscription settings:
http://groups.google.com/group/python_inside_maya/subscribe