# Python render script different outcome when run in background

When running my bpy script from command line with the argument -b (background mode) I get a different size of the image vector I want to render than without the argument. Did somebody encounter the same problem already? For better explanation here is the code:

scn.render.resolution_percentage = 100
scene.use_nodes = True
tree = scene.node_tree
links = tree.links

# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')

# create output node
v = tree.nodes.new('CompositorNodeViewer')
v.use_alpha = False

# Links
links.new(rl.outputs['Z'], v.inputs[0]) # link Z to output
scn.render.image_settings.color_mode = 'BW'
bpy.ops.render.render()

# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels


Render settings are 520x424 pixels defined in the blend file. in the command line I use the following cmd:

blender -b ...\Blender_Animation1.blend --python ...main.py

when I check the variable pixels i get different lengths depending whether I use the b or not. Without b I get 881920 values ( 1 pixel = RGBA -> 520*424*4 = 881920) which is correct. In the other case I get much less. Did somebody have the same problem already and knows what causes this error?

## 1 Answer

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.