How to get world coordinates of render pixels?

Using a Blender python script I want to get the world coordinate of a specific render pixel (or all).

Assume a camera has been defined (focal length, fov/angle, sensor size,etc.), and orientated/positioned as desired. Also, a large surface is defined, so the camera rays would actually “hit” objects/mesh in the scene (& not cast into infinity).

For example, given 4 corners pixels of camera, where do they in fall in world coordinates (XYZ)?

Alternatively, a script could export the world coordinates (XYZ) for all pixels, then a “camera ray hit map” could be generated. There’s probably a better name for this.

From the very similar question on stack exchange “Get location in scene for pixel?”, there are recommendations to use: bpy_extras.view3d_utils
bpy.types.Scene.ray_cast

However I’m new to blender scripting, & camera projection/ray-tracing in general, and it is not clear to me how to proceed with these functions. Any help would be appreciated.

Note: I also posed the same question on Blender’s stackexchange.

You could create a Cycles material that does this. Start with a Geometry node, plug the Position into the Color of an Emission node. Let the strength be 1.0 and plug the result in the material output node.

Now in the RenderLayers tab, you can override all materials with the one you just created.

The resulting rendered image is what you want, except for a little bit of softening/anti aliasing at the edges.

If you want to do it more interactively it would involve some basic raytracing code which would be pretty slow to do in Python.

I followed your instructions and produced the following below of the default cube + plane & it looks like I got get X,Y,Z world/scene coordinates from the render result R,G,B values respectively. Very cool, Thanks!

If you or anyone can steer me towards basic ray tracing resources that would be very helpful too – preferably useable via the blender python console. I realize it would be slow to get all pixels via python, I’d only be interested in the 4 corner & center world/scene coordinates of the render.


There’s lots of online sources, for instance: http://scratchapixel.com/lessons/3d-basic-lessons/lesson-1-writing-a-simple-raytracer/how-does-it-work/

Check out the parts on intersections, that’s what you want to do, you don’t care about colors and reflections etc. You need to know a bit of linear algebra do go through it though.