# Depth data works, but not in Background mode

As the titel says, I found a workaround for obtaining a depth image of the camera's FOV by using this workaround. It works just perfectly by accessing the pixel data of the image with python:

 pixels = bpy.data.images['Viewer Node'].pixels


BUT: This method works only when the GUI is used as well. In Background mode (using the attribute -b in cmd line) this does not work. The farest I got was to check if the image "has" some data with:

 pixels = bpy.data.images['Viewer Node'].has_data


which gave me a clear false. Therefore my question is, if there is a way how to enable this data also in Background mode?

I googled a lot and the only other option I found to access depth data, is to use the Camera Data Node's depth output. However, I do not know how to access this data via python script.

• Could you resolve this issue? Did the answer Sebastian Koch posted help you? I am somehow trying to literally do what you are trying to do here. Could you take a look at my question here and see if you can post a solution? The best think that came to my mind was to simulate mouse clicking, but it seems that it is not possible to do that either. – Amir Mar 14 '18 at 0:38

Rendering of viewer nodes and previews in background mode was disabled in 2013 for speed optimization (see here). However, you can easily enable it again by modifying 2 files of the blender sources and compiling blender yourself (as described here).

In the file source/blender/compositor/operations/COM_ViewerOperation.h, line ~58:

bool isOutputOperation(bool /*rendering*/) const { if (G.background) return false; return isActiveViewerOutput();


should be changed to

bool isOutputOperation(bool /*rendering*/) const {return isActiveViewerOutput(); }


and in file source/blender/compositor/operations/COM_PreviewOperation.h, line ~48:

bool isOutputOperation(bool /*rendering*/) const { return !G.background; }


should be changed to

bool isOutputOperation(bool /*rendering*/) const { return true; }


After these changes, the pixels array gets properly updated in background mode.

• I am somehow trying to literally do what the question asker here is trying to do here. Could you take a look at my question here and see if you can post a solution? The best think that came to my mind was to simulate mouse clicking, but it seems that it is not possible to do that either. So I guess I need to somehow enable backdrop in the background mode instead. Do you think this would work? – Amir Mar 14 '18 at 0:40
• I made the changes you mentioned and compiled Blender. Numpy can access the data but I cannot update "Viewer Node" image content, as mentioned in my previous post earlier. I need to somehow activate my Viewer nodes to be able to update the contents of the image block. Do you have any ideas on how I can do that? – Amir Mar 14 '18 at 5:19
• I would also go with @Scott Millner 's answer here to add a Switch Node that can be toggled. This is easy to implement and shouldn't add overhead as you mentioned (not sure about this, though). As you want to render millions of images, you probably want to parallelize the rendering anyways and run multiple blender instances in background mode. – Sebastian Koch Mar 15 '18 at 9:02
• I have already paralellized the rendering pipeline but I would like to store the rendering results in "Viewer Node" image block and use Numpy to read them. Unfortunately it seems that what Scott recommended does not work either and I cannot update the contents of the image block when running Blender in the background :( – Amir Mar 15 '18 at 16:51