1
$\begingroup$

I know how to render one image via Python and get an output with Blender's compositor.

What if I need it to render 100 images for me with numerical names e.g: img_1, img_2, img_3 etc.?

Give me please an example of a working script via Ubuntu terminal and Python sys.argv?

Can I for example setup compositor nodes, and then simply render scene and get composited render as a result render?

Python code for rendering 1 image:

import bpy
import sys

argv = sys.argv
argv = argv[argv.index("--") + 1:]

current_frame_path = argv[0]
print(argv)
ob = bpy.context.active_object
scene_rd = bpy.context.scene.render

scene_rd.filepath = current_frame_path
bpy.ops.render.render(write_still=True)

Shell script for rendering n images

#!/bin/bash

imgs_cnt=n

output_frame_path='RenderedImages/img'


for (( i=1; i<=imgs_cnt; i++ )); do
  echo "Rendering frame ${i}/${imgs_cnt}..."

  (
    cd /path/to/model/
blender --background Model.blend --python Render.py-- "${output_img_path}/img_${i}.png"
  )

  echo "Image ${i}/${imgs_cnt} has been rendered"
done

Here's an example for what I'm trying to do:

This is an automatically generated scene with plants, and I render it via executing shell script in terminal. It use 3 python scripts for blender. 1st script is for generating the scene, it places objects on the scene with different location and rotation settings and check if objects are intersected (to avoid clipping). 2nd script is for rendering scene. 3rd script isn't yet ready but it will be generating a mask for generated scene via cryptomatte. As a final result I want blender to output me 2 images into 2 different directories. 1st for the original images and 2nd for mask annotations. enter image description here enter image description here

$\endgroup$
  • 3
    $\begingroup$ Hi. I suggest showing the relevant code you have for rendering 1 image. The last sentence comes across a little demanding and probably won't go down well with other users. $\endgroup$ – Ray Mairlot Feb 17 at 15:47
  • $\begingroup$ @RayMairlot here you go $\endgroup$ – cxnt Feb 18 at 6:35
  • $\begingroup$ No extra args needed here blender -b -f ${i} -o "${output_img_path}/img_###.png" When useing -- make sure it is clearly separated by spaces. See blender.stackexchange.com/questions/6817/… Would be a lot quicker to copy images from one render than to render each. $\endgroup$ – batFINGER Feb 18 at 6:49
  • $\begingroup$ @batFINGER I use args because I render an original image, then a grayscale mask for it, and output them to different directories. My question is how to do the same, but when using compositor? I need an original scene render and cryptomatte image outputs $\endgroup$ – cxnt Feb 18 at 8:00
  • $\begingroup$ @batFINGER Can I for example setup a compositor nodes, and then simply render scene and get compositorED render as a result render? $\endgroup$ – cxnt Feb 18 at 8:26
2
$\begingroup$

You can add File Output nodes to the compositor to get multiple files saved from any point of your compositing without the need for any Python:

enter image description here

You can also use Python to add more nodes:

import bpy

bpy.context.scene.use_nodes = True
tree = bpy.context.scene.node_tree


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

RenderLayers_node = tree.nodes.new('CompositorNodeRLayers')   
RenderLayers_node.location = 0,0

comp_node = tree.nodes.new('CompositorNodeComposite')   
comp_node.location = 400,0

links = tree.links
link = links.new(RenderLayers_node.outputs[0], comp_node.inputs[0])

for i in range(0,100):
    output_node = tree.nodes.new(type='CompositorNodeOutputFile')
    output_node.name = "Output node number "+str(i+1)
    output_node.location = (400,(-140-i*110)) 
    link = links.new(RenderLayers_node.outputs[0], output_node.inputs[0])

See the documentation for more info.

| improve this answer | |
$\endgroup$
  • $\begingroup$ That's now gonna work. What if I need to render 1000 images? I need to script it anyway $\endgroup$ – cxnt Feb 18 at 9:39
  • $\begingroup$ Fix typo: NOT* gonna work $\endgroup$ – cxnt Feb 18 at 10:07
  • $\begingroup$ Sorry, my bad, did not read the question carefully enough. See the update. $\endgroup$ – Martynas Žiemys Feb 18 at 11:24
  • $\begingroup$ Thanks for the effort, appreciated your time. I updated the question with more detailed info of what I'm trying to achieve. Your answer kinda answers the question "How to render multiple images via compositor?" but that's not in the way I meant it $\endgroup$ – cxnt Feb 18 at 11:51
  • 1
    $\begingroup$ You might not be using Cryptomatte the way it is intended to be used if you are outputting the colourful image. Generally you should not see it ever. You should save Cryptomatte masks using EXR format with the rendered image or as separate greyscale masks using Cryptomatte nodes to generate them. exr-io.com might also be of interest... $\endgroup$ – Martynas Žiemys Feb 18 at 12:00

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.