1
$\begingroup$

I would like to know the width and height of frame when rendering.

And I found that someone has ask the familiar question, "How to access Render Result pixels from python script? "(see here)

So, I added some codes in the codes answered by wsfax (see original codes) , like this script(*).

import bpy 

path_name = ''
file_name = 'test_2017'
file_path_name = path_name + file_name + '.blend'
bpy.ops.wm.open_mainfile(filepath = file_path_name)

# 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 and save image
bpy.ops.render.render()

# get width and height
width_of_render = bpy.data.images['Viewer Node'].size[0]
height_of_render = bpy.data.images['Viewer Node'].size[1]
print("width: ", width_of_render, "height: ", height_of_render)

# get viewer pixels
pixels = bpy.data.images['Viewer Node'].pixels
print(len(pixels)) # size is always width * height * 4 (rgba)

What I mainly did is using bpy.data.images['Viewer Node'].size[0] and bpy.data.images['Viewer Node'].size[1] to get the width and height.

I tested a simple blender file (test_2017.blend) only containing a cube and executed it in command line, and got 262144(pixels), width = 256(pixels), and height = 256(pixels) in command line.

However, When I opened blender file (test_2017.blend) and executed script(*)(without line 3 to line 6) in blender, I got 2073600(pixels), width = 960(pixels), and height = 540(pixels) in console window.

I have no idea why they are different?

And then I saved image when rendering, the width and the height of the image were 960(pixels) and 540(pixels).

How can I get the truly width and height? (It would be good if I can render without saving images)

$\endgroup$
1
$\begingroup$

You can get the Python expressions needed by enabling Python Tooltips in the User Preferences, "Interface" tab. Then you can just hover over the resolution settings in the render panel, and see you can use bpy.data.scene["Scene"].render.resolution_x. It's better, though, to use the currently active scene, rather than hard-coding the scene name. Also you'll want to multiply with the resolution scale percentage. This leads you to:

render = bpy.context.scene.render
scale = render.resolution_percentage / 100
rsize_x = render.resolution_x * scale
rsize_y = render.resolution_y * scale
$\endgroup$
  • 1
    $\begingroup$ Thanks a lot, that's really helpful. I wonder that blender (in GUI) will draw a picture when rendering, so the script I used can be executed correctly in blender in GUI. But it executed incorrectly in command line. All in all, you really gave me a big favor. $\endgroup$ – rita33cool1 May 26 '17 at 11:59
  • $\begingroup$ well, that would be another issue for another question, not something to be handled in comments. $\endgroup$ – dr. Sybren May 27 '17 at 9:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.