Export corresponding X,Y,Z and RGB values of rendered image as text file

Hello all,
I’m new here.

I’d like to use python to do the following:
after I render scene in blender as an image (render button), then use a python script that exports the following of each pixel in the rendered image:
R,G,B values and corresponding X,Y,Z coordinates in the 3D model and put it into a text file.
I’m only interested in the visible pixels (therefore I assume that starting from the rendered image makes sense.

Ideally the data should have rendered image width x rendered image height data points and be layed outs just like the rendered image.
like this pseudo code, assuming the rendered image has size 800x600

for( y = 0 ; y < 600 ; y++) {
for(x = 0 ; x < 800 ; x++) {
RGBColor = getPixelColorInRenderedImage(x,y);
VectorXYZ = getVectorXYZInRenderedImage(x,y);
WriteToTextFile(RGBColor, VectorXYZ);
}
}

Does blender have a way to do the above things?
Has such a script already available?
If not I could code it by myself, but I’d need some help from you guys to figure out which python functions I need to use to access the 3D data of blender.

Thanks in advance for your help and pointers.
Martin

Hi,

Interesting question you have. the tricky part is getting the XYZ for each pixel. I’d say you can do that by creating a cycles node setup that uses Geometry.Position as input, and then converts that to the 0…1 range using some math. Then your script should do that conversion in the opposite direction to get the original color back.

You should make two renders. One with an emission-material showing the xyz colors (as described above) and one with an emission-material showing RGB colors

Then a python script that goes through these renders, reads the values per pixel, and writes them to a text file