Rendering with inverse z buffer

Hello,

I am a new user, and quite new in Blender. I have a specific problem that should not be too difficult to be solved, but I still miss the experience in the Python-Blender environment.

What I want to do is to render a 3D model using the Z buffer in the wrong way, so that all parts of the model that would be occluded are visible and the parts of the model that are visible will be occluded.
Let’s say is like seeing the 3D model from the other direction, but mantaining the Z values (because I need the depth).
Many possibilities are available:

  • using a kind of raytracing approach and selecting the maximum value (so the value further away from the camera, so the occluded)
  • rendering every object at a different time in the image, before the objects that are further away, then the objects close to the camera (that would appear in the back, thus occluded, in the final 2D image)

I am open to new ideas also, suggestions, or whatever.
I am currently working on this and I will post some code as soon as I get something working.

Thank you very much,
Luca