View Full Version : Screen Grab in maya via python

03 March 2008, 07:38 PM
Hi, trying to set up a screenGrab function inside of maya using python, I'mm using the python imaging library to do it, the following code works fine as a standalone python script:

import PIL
from PIL import ImageGrab
screen = ImageGrab.grab()

in maya however i get this error: (the module imports go with no error, and the .grab function seems to work)

# Error: (0, 'Error')
# Traceback (most recent call last):
# File "<maya console>", line 1, in <module>
# File "C:\Python25\Lib\site-packages\PIL\", line 1405, in save
# save_handler(self, fp, filename)
# File "c:\python25\lib\site-packages\PIL\", line 409, in _save
# ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
# File "C:\Python25\Lib\site-packages\PIL\", line 491, in _save
# s = e.encode_to_file(fh, bufsize)
# IOError: (0, 'Error') #

Not sure why this doesn't work? Possibly something to do installing the module in my maya site-packages folder? I just copied and pasted it from my python25 directory- any help appreciated!

03 March 2008, 09:01 AM
While I can't give you an absolute solution, I can point you in the right direction. There's a way to save image files from the viewport. Check the help for "saveImage" and other similar commands.

There was a script one of our animators showed me a while ago that used this technique to create an onionskin/ghosting effect directly in the viewport by creating jpegs on the fly, and projecting them onto a look-at plane.

03 March 2008, 12:15 PM
Hey thanks for the tip, can't get saveImage to do anything useful though, documentation is pretty skimpy, and the example code doesn't seem to do anything except give me a window with a big black square in it- could be something with my machine? I'll try it at work on monday and see if the result is any different-

if any python people out there know possibly how to get pil working inside maya that would be a better alternative as pil has some really nice methods for handling images-

03 March 2008, 09:02 PM
if the package/modules comes with a script for installing it, you can easily control which installation it will be installed to by which python binary you use to call it.

this will install to whatever the system python is:
python install

this will install it to maya2008
C:\Program Files\Autodesk\Maya2008\bin\mayapy install

try using this to do an actual install of the package and see if it makes a difference.


03 March 2008, 10:22 PM
Hey chad thanks for the tip, sadly the pil module didn't seem to come with a file? it was a windows installer- also, anyone know what version of python ships with maya 2008?

saveImage errors out on my machine:
// Error: Image open failed: //
probably some type of graphics card thing- forgot to try it at work, have to try and remember to give it a go..

my other thought for a screen grab type behavior was to build an ortho cam on the fly, and then use a call to the hardware render buffer (not exaclty a screen grab but it will do) -glRender is one piece of crap command though... none of the flags seem to actually work- though I can spawn a glRenderEditor and get it to 'test render' got only knows where or if it's actually writing an image to disk though...

only other things I can think of is to use os.system to make an external call to my python25 install to run the PIL imagegrab method...or just give up doing it via python and force a modelPanel to my desired size and then do a single frame playblast from that-

10 October 2008, 10:04 PM
hey greatPumpkin, the version you are looking for is 2.4. I am trying to get PIL to load correctly but am having no luck. I downloaded the installer and installed PIL, but how do i get it to work with Maya?

10 October 2008, 12:40 AM
I haven't been able to get it to work unfortunately, the module appears to import, but the commands don't seem to run properly. As a workaround I was making a os.system call to it. Haven't been playing with that stuff for the last little while unfortunately.

10 October 2008, 04:49 PM
Success!!! I think I figured it out last night. I dont know what commands you were using but the commands I tried worked, and I got it to save out an image using Python, so yay! If you are still interested I could tell you how I got it to work?

P.S. - The information I provided was wrong, and I think that was the major problem that I was running into. I read that it was version 2.4, but its not! Its 2.5.

01 January 2009, 11:36 AM
I would be very interested on how you reached a solution. Also, you mentioned Python 2.5 being the version used by Maya 2008? How did you find out that it's not 2.4.1 like the docs mention?

01 January 2009, 06:01 PM
To be honest Andrei2k, I dont remember, since its been a while, but I believe that I tried installing 2.4, and PIL did not work for me, but 2.5 worked fine. Maybe its anything after 2.4, i.e. 2.4.1+

01 January 2009, 07:54 PM
i haven't gone back to this in awhile but i believe i may have been using maya 8.5 at the time so it's definitely worht another try with 2.5- the code from my first post works fine outside of maya so it would be a good starting point once you get PIL installed-

CGTalk Moderation
01 January 2009, 07:54 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.