Last time I asked for a script, I was suprised at how quickly I got a working response (hat's off to 7feet for his Maya camera scripts, nice job!). So, being encouraged by people giving me what I asked for

Would it be possible to create a script that could render out a grayscale image representing z-depth. Basically, many 3d renderers allow you to render out an image that contains RGBA and Z, where z is a value between 1 and 0, 1 being closest to camera, 0 being furthest away.
Here's one possible application of it, animatable depth of field:
http://www.cgarchitect.com/vb/showthread.php?t=3062
Now I imagine it wouldn't be possible to create a renderer that deals with z-depth on a pixel by pixel level, but would it be possible for it to deal with objects or layers? Would it be possible to change the colors of an entire layer based on depth? Also, would it be possible to animate those colors?
I'm guessing that some things won't work very well with it, like layers that extend through z (floors and walls, etc.). I see it as you save out an alternate version of your file, and run the lua script, changing your beautiful animation into a grayscale ghost show. Render and you've got your z-depth. The lua script, of course, being the tricky part of all this.
Anyway, just thought I'd ask.
Thanks for listening.
Kurt