1
$\begingroup$

I'm new to working with both Blender and bpy, and I'm trying to render directly into Python. Best case, I could render directly from bpy 2.82 into a byte stream or numpy matrix.

I've been trying to follow the instructions here https://ammous88.wordpress.com/2015/01/16/blender-access-render-results-pixels-directly-from-python-2/, using bpy.data.images["Viewer Node"].pixels, but it's returning something of size 262,144 which doesn't make any sense. The link was written for Blender version 2.7, so I assume the API changed somehow between the two.

I have bpy.context.scene.render.resolution_x and bpy.context.scene.render.resolution_y set to 320 and 480 respectively, so I would assume there would be 320x480x4=614,400 total pixels.

Is there any way to do what I'm trying to do in Blender version 2.82?

Here's the code from the link above:

import bpy
import numpy as np
 
# switch on nodes
bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree
links = tree.links
  
# clear default nodes
for n in tree.nodes:
    tree.nodes.remove(n)
  
# create input render layer node
rl = tree.nodes.new('CompositorNodeRLayers')      
rl.location = 185,285
 
# create output node
v = tree.nodes.new('CompositorNodeViewer')   
v.location = 750,210
v.use_alpha = False
 
# Links
links.new(rl.outputs[0], v.inputs[0])  # link Image output to Viewer input
 
# render
bpy.ops.render.render()
 
# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels
print(len(pixels)) # size is always width * height * 4 (rgba)
 
# copy buffer to numpy array for faster manipulation
arr = np.array(pixels[:])
```
$\endgroup$
5
  • $\begingroup$ You may need to check your render resolution setting to be at 100%. $\endgroup$ Commented May 5, 2022 at 20:17
  • $\begingroup$ check out answers to this question, especially blender.stackexchange.com/a/3054/111042 $\endgroup$ Commented May 5, 2022 at 21:04
  • $\begingroup$ 262,144 = 256*256*4. 256 is the default size of the viewer node image $\endgroup$ Commented May 5, 2022 at 21:10
  • $\begingroup$ blender.stackexchange.com/questions/195289/… $\endgroup$ Commented May 5, 2022 at 21:12
  • $\begingroup$ render_res = bpy.data.images["Render Result"].pixels returns None actually $\endgroup$ Commented May 12, 2023 at 11:23

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.