If I’m not mistaken you can save the z-buffer to an image, using the nodes editor. After that it’s a simple matter of reading the values and writing them to a text file.
Alternatively you could use the z-buffer of the 3d-view. Here’s some code to do that:
from Blender import *
from Blender.BGL import *
windows = Window.GetScreenInfo()
for w in windows:
if w['type'] == Window.Types.VIEW3D:
xmin, ymin, xmax, ymax = w['vertices']
width = xmax-xmin
height = ymax-ymin
zbuf = Buffer(GL_FLOAT, [width*height])
glReadPixels(xmin, ymin, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, zbuf)
strbuf = 
for i in range(height):
file = open("C:/test.txt", "w")
for i in strbuf:
This doesn’t work in wireframe and bounding box mode (viewport shading methods).
It’s best to have only one 3d-view when running the script.
It could be that the z-depth of the grid is also output, so you might want to disable it.