Viewer node pixels do not update when running blender in background-mode #91891

Open
opened 2021-10-01 23:49:05 +02:00 by Andrea · 17 comments

System Information
Operating system: Linux-5.11.0-37-generic-x86_64-with-glibc2.31 64 Bits
Graphics card: Mesa Intel(R) UHD Graphics 630 (CFL GT2) Intel 4.6 (Core Profile) Mesa 21.0.3

Blender Version
Broken: version: 2.93.4, branch: master, commit date: 2021-08-31 09:23, hash: b7205031ce
Worked: (newest version of Blender that worked as expected)

Short description of error
I am trying to render images using blender as a python module. I compiled blender as bpy.so module following the official guide at the following link:

https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule

The installation works fine if I save renders to file on the hard disk. What I want to do is access the rendered pixels before they are saved to file because I need to process the image within python. Saving the image to file and reloading it is not feasible due to performance issues. I have been able to use this workaround using the viewer node as described at this link, although only with the blender UI open:

https://ammous88.wordpress.com/2015/01/16/blender-access-render-results-pixels-directly-from-python-2/

When running it using blender compiled as a python module, this doesn't work. Specifically, the viewer node pixels don't seem to get updated (their value is always 0 and the resolution is always default and cannot be changed). I found a similar problem when running blender in the background as described here:

https://blender.stackexchange.com/questions/69230/python-render-script-different-outcome-when-run-in-background/81240#81240

I'm encountering the same issue when running blender as a python module. I tried following the steps at the previous link to rebuild blender and enable background viewer node update but the source code seems to have changed since the time of that post (the steps are shown in the picture below). Is this still possible? I think there is a lot of potential in this feature and it seems it can be activated by simply rebuilding the module from source. If that's the case, it would be nice to know how.

Thanks.

Exact steps for others to reproduce the error
The problem can be reproduced using the file provided (it is simply the default file). The bpy module has been built using the last available source code from the blender repository at the time of writing. The python code to reproduce the problem is:

import bpy
import numpy as np

bpy.ops.wm.open_mainfile(filepath="default.blend")

bpy.context.scene.render.use_compositing = True
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links

for n in tree.nodes:
     tree.nodes.remove(n)

rl = tree.nodes.new('CompositorNodeRLayers')      

vl = tree.nodes.new('CompositorNodeViewer')   
vl.use_alpha = True
links.new(rl.outputs[0], vl.inputs[0])  # link Renger Image to Viewer Image
links.new(rl.outputs[2], vl.inputs[1])  # link Render Z to Viewer Alpha

# render
bpy.context.scene.render.resolution_percentage = 100 #make sure scene height and width are ok (edit)
bpy.ops.render.render()

# get the pixels and put them save them to a numpy array
pixels = np.array(bpy.data.images['Viewer Node'].pixels)

print(pixels.sum())

the output of the code is:

Read blend: default.blend
Fra:1 Mem:49.33M (Peak 50.00M) | Time:00:00.09 | Syncing Cube
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.11 | Syncing Light
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.11 | Syncing Camera
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.12 | Rendering 1 / 64 samples
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.54 | Rendering 26 / 64 samples
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.81 | Rendering 51 / 64 samples
Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.95 | Rendering 64 / 64 samples
Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing
Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | Determining resolution
Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | Initializing execution
Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | De-initializing execution
0.0

note that the script correctly renders the scene but the output of the print() function returns 0.0 which means that the values in the "pixels" array are all 0.

**System Information** Operating system: Linux-5.11.0-37-generic-x86_64-with-glibc2.31 64 Bits Graphics card: Mesa Intel(R) UHD Graphics 630 (CFL GT2) Intel 4.6 (Core Profile) Mesa 21.0.3 **Blender Version** Broken: version: 2.93.4, branch: master, commit date: 2021-08-31 09:23, hash: `b7205031ce` Worked: (newest version of Blender that worked as expected) **Short description of error** I am trying to render images using blender as a python module. I compiled blender as bpy.so module following the official guide at the following link: https://wiki.blender.org/wiki/Building_Blender/Other/BlenderAsPyModule The installation works fine if I save renders to file on the hard disk. What I want to do is access the rendered pixels before they are saved to file because I need to process the image within python. Saving the image to file and reloading it is not feasible due to performance issues. I have been able to use this workaround using the viewer node as described at this link, although only with the blender UI open: https://ammous88.wordpress.com/2015/01/16/blender-access-render-results-pixels-directly-from-python-2/ When running it using blender compiled as a python module, this doesn't work. Specifically, the viewer node pixels don't seem to get updated (their value is always 0 and the resolution is always default and cannot be changed). I found a similar problem when running blender in the background as described here: https://blender.stackexchange.com/questions/69230/python-render-script-different-outcome-when-run-in-background/81240#81240 I'm encountering the same issue when running blender as a python module. I tried following the steps at the previous link to rebuild blender and enable background viewer node update but the source code seems to have changed since the time of that post (the steps are shown in the picture below). Is this still possible? I think there is a lot of potential in this feature and it seems it can be activated by simply rebuilding the module from source. If that's the case, it would be nice to know how. Thanks. **Exact steps for others to reproduce the error** The problem can be reproduced using the file provided (it is simply the default file). The bpy module has been built using the last available source code from the blender repository at the time of writing. The python code to reproduce the problem is: ``` import bpy import numpy as np bpy.ops.wm.open_mainfile(filepath="default.blend") bpy.context.scene.render.use_compositing = True bpy.context.scene.use_nodes = True tree = bpy.context.scene.node_tree links = tree.links for n in tree.nodes: tree.nodes.remove(n) rl = tree.nodes.new('CompositorNodeRLayers') vl = tree.nodes.new('CompositorNodeViewer') vl.use_alpha = True links.new(rl.outputs[0], vl.inputs[0]) # link Renger Image to Viewer Image links.new(rl.outputs[2], vl.inputs[1]) # link Render Z to Viewer Alpha # render bpy.context.scene.render.resolution_percentage = 100 #make sure scene height and width are ok (edit) bpy.ops.render.render() # get the pixels and put them save them to a numpy array pixels = np.array(bpy.data.images['Viewer Node'].pixels) print(pixels.sum()) ``` the output of the code is: ``` Read blend: default.blend Fra:1 Mem:49.33M (Peak 50.00M) | Time:00:00.09 | Syncing Cube Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.11 | Syncing Light Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.11 | Syncing Camera Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.12 | Rendering 1 / 64 samples Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.54 | Rendering 26 / 64 samples Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.81 | Rendering 51 / 64 samples Fra:1 Mem:49.70M (Peak 50.04M) | Time:00:00.95 | Rendering 64 / 64 samples Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | Determining resolution Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | Initializing execution Fra:1 Mem:49.22M (Peak 89.25M) | Time:00:01.00 | Compositing | De-initializing execution 0.0 ``` note that the script correctly renders the scene but the output of the print() function returns 0.0 which means that the values in the "pixels" array are all 0.
Author

Added subscriber: @Andrea-3

Added subscriber: @Andrea-3
Author

default.blend

this is the file used to reproduce the results.

[default.blend](https://archive.blender.org/developer/F10723630/default.blend) this is the file used to reproduce the results.
Author

Removed subscriber: @Andrea-3

Removed subscriber: @Andrea-3
Author

Added subscriber: @Andrea-3

Added subscriber: @Andrea-3

Added subscriber: @Gadgeteering

Added subscriber: @Gadgeteering

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'

I am having the same problem, it seems that the source code for a couple of the header files need to be updated to solve this issue.

Solution

I am having the same problem, it seems that the source code for a couple of the header files need to be updated to solve this issue. [Solution](https://blender.stackexchange.com/questions/69230/python-render-script-different-outcome-when-run-in-background?answertab=votes#tab-top)
Member

Changed status from 'Confirmed' to: 'Needs Triage'

Changed status from 'Confirmed' to: 'Needs Triage'

Added subscribers: @lichtwerk, @ideasman42, @mano-wii

Added subscribers: @lichtwerk, @ideasman42, @mano-wii

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'

Changed status from 'Needs Triage' to: 'Needs Developer To Reproduce'

Blender as a python module is not something the #triaging team is used to checking.

I'm not sure if this is a feature tracked by this Bug Tracker (I didn't even find a tag for it).

@ideasman42, if I'm not mistaken, you're maintaining the module. How would you like these issues to be handled?


(@lichtwerk, I'm tagging you in case I'm missing something).

Blender as a python module is not something the #triaging team is used to checking. I'm not sure if this is a feature tracked by this Bug Tracker (I didn't even find a tag for it). @ideasman42, if I'm not mistaken, you're maintaining the module. How would you like these issues to be handled? --- (@lichtwerk, I'm tagging you in case I'm missing something).
Member

If we move this out of the triaging queue (by changing status label to Needs Information from Developers), this needs to be the responsibility of a module (so a Module label must be added)

If we move this out of the triaging queue (by changing status label to `Needs Information from Developers`), this needs to be the responsibility of a module (so a `Module` label must be added)
Philipp Oeser added
Status
Needs Triage
and removed
Status
Needs Info from Developers
labels 2023-02-14 12:32:09 +01:00
Member

if I'm not mistaken, you're maintaining the module. How would you like these issues to be handled?

@ideasman42 ^

> if I'm not mistaken, you're maintaining the module. How would you like these issues to be handled? @ideasman42 ^

This isn't related to running blender as a Python module, the same problem happens in background mode.


Save as test_render.py

import bpy
import numpy as np

bpy.context.scene.render.use_compositing = True
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links

for n in tree.nodes:
     tree.nodes.remove(n)

rl = tree.nodes.new('CompositorNodeRLayers')      

vl = tree.nodes.new('CompositorNodeViewer')   
vl.use_alpha = True
links.new(rl.outputs[0], vl.inputs[0])  # link Renger Image to Viewer Image
links.new(rl.outputs[2], vl.inputs[1])  # link Render Z to Viewer Alpha

# render
bpy.context.scene.render.resolution_percentage = 100 #make sure scene height and width are ok (edit)
bpy.ops.render.render()

# get the pixels and put them save them to a numpy array
pixels = np.array(bpy.data.images['Viewer Node'].pixels)

print(pixels.sum())

import sys
sys.exit(0)

Run:

blender --factory-startup --python test_render.py

Now run:

blender --factory-startup --background --python test_render.py

When in background mode, the printed result is 0.0

This isn't related to running blender as a Python module, the same problem happens in background mode. ---- Save as `test_render.py` ```py import bpy import numpy as np bpy.context.scene.render.use_compositing = True bpy.context.scene.use_nodes = True tree = bpy.context.scene.node_tree links = tree.links for n in tree.nodes: tree.nodes.remove(n) rl = tree.nodes.new('CompositorNodeRLayers') vl = tree.nodes.new('CompositorNodeViewer') vl.use_alpha = True links.new(rl.outputs[0], vl.inputs[0]) # link Renger Image to Viewer Image links.new(rl.outputs[2], vl.inputs[1]) # link Render Z to Viewer Alpha # render bpy.context.scene.render.resolution_percentage = 100 #make sure scene height and width are ok (edit) bpy.ops.render.render() # get the pixels and put them save them to a numpy array pixels = np.array(bpy.data.images['Viewer Node'].pixels) print(pixels.sum()) import sys sys.exit(0) ``` Run: `blender --factory-startup --python test_render.py` Now run: `blender --factory-startup --background --python test_render.py` When in background mode, the printed result is `0.0`
Campbell Barton changed title from Viewer node pixels do not update when running blender as python module to Viewer node pixels do not update when running blender in background-mode 2023-03-23 07:54:16 +01:00

As far as I know, Eevee already supports background rendering, so this must be a Render Pipeline issue.

As far as I know, Eevee already supports background rendering, so this must be a `Render Pipeline` issue.

This is likely working as intended since using the viewer-node in background mode is unnecessary overhead in most cases (on render-farms for e.g.).

This could be supported but don't think it's a bug.

This is likely working as intended since using the viewer-node in background mode is unnecessary overhead in most cases (on render-farms for e.g.). This could be supported but don't think it's a bug.
Member

For why this is requested, see also #54314

For why this is requested, see also #54314
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
6 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#91891
No description provided.