Donnerstag, 6. Februar 2020

How to set the active camera ... when Blender renders in the background



Background rendering, i.e. rendering without the UI and based on command line parameters alone, is usually the domain of render farms.

But there are situations where this functionality is useful for normal users as well: scripts.

In my case the blend file contains a complex architectural model and various cameras (which you can conveniently set-up and switch between with the “Stored Views” add-on).

Some of those cameras are (virtual) panoramic cameras.  Their output will be fed into a generator script to calculate multi-res images for use with the web panorama viewer pannellum (https://pannellum.org) to create a virtual tour.

Wouldn’t it be nice to have a script producing the rendered pictures for all those cameras; it might even call the pannellum generator script afterwards to calculate the multi-res images needed for the virtual tour...

When Blender is started in background mode, various parameters can be set from the command line - among others the frame and scene to be rendered, the render engine and the output path.

Missing however are some settings I would like for my workflow: the active camera and the dimension percentage (Output Properties > Dimensions > Resolution > %). Those values are stored in the blend file and used in the render process.

If you want rendered images from multiple cameras you usually open the blend file, switch the camera, save the blend file and re-render…

As for the other setting: I sometimes forget to switch the resolution from my usual 50% to 100% before starting to render. While rendering without the UI you don’t notice this right away.  Something that sets this value to 100% would be nice.

The solution

Blender’s command line option --python-expr lets you enter a short Python snippet which gives you access to all settings in your scene.

To switch the active camera in the current screen use

import bpy
bpy.context.scene.camera = bpy.context.scene.objects.get('XXXXXX')


where XXXXXX is the name of the camera.

The command for setting the resolution percentage value is

bpy.context.scene.render.resolution_percentage = 100

You can test these commands in Blender’s Python console.

Note: It does matter where you put the --python-expr command on the command line. If used at the end of the command it will be ineffective because it changes the parameter after the render process.

My typical Blender background render command looks like this:

blender -b myblend.blend --python-expr "import bpy; bpy.context.scene.camera = bpy.context.scene.objects.get('Camera XXXX'); bpy.context.scene.render.resolution_percentage = 100" -o /tmp/pic_#### -F PNG -x 1 -f 1 -E CYCLES

  • -b: background render of myblend.blend.
  • --python-expr: use camera ‘Camera XXXX’ and render at 100%
  • -o: store under /tmp/pic_####
  • -F: store as PNG
  • -x: add the PNG extension
  • -f: render frame 1
  • -E: use cycles render engine

You might use this technique to change other values on the fly without them being saved back to disk.

Keine Kommentare: