Blender multiple rendering

Pretty often, when we create a 3D model, for example using Blender, we would like to automate the rendering of the images from different camera angles and perspectives. See for example, this pre-processing part of an image processing project. The blender file containing the 3D model can be found here.

After creating the model, use the following codes to run auto_capture.py for rendering. The codes are to be input into python console in Blender (to access it, press shift+f4).

import os, sys, importlib
current_dir = "path\\to\\current\\directory"
sys.path.append(current_dir) # add current path
import auto_capture # importlib.reload(auto_capture) # reload to rerun the script after first time import

auto_capture.py

import os, sys, importlib
current_dir = "path\\to\\current\\directory"
sys.path.append(current_dir) # add current path
# import auto_capture
# importlib.reload(auto_capture)

print("Hello!")

import bpy
obj = bpy.data.objects['Animal.001']
obj_cam = bpy.data.objects['Camera']

# x, y, z, euler_x, euler_y, euler_z
pos = [
[0, -3, 5, 30, 0, 0],
[0, -3.5, 5, 40, 0, 10],
[0, -4, 5, 45, 0, -5],
[-2, -3, 3, 50, 0, -20],
]

count = 1
def convert_deg_to_rad(x):
return x*3.142/180
for x in pos:
obj_cam.location = x[0:3]
obj_cam.rotation_euler = [convert_deg_to_rad(y) for y in x[3:]]
print(obj_cam.location)
print(obj_cam.rotation_euler )

bpy.data.scenes['Scene'].render.filepath = current_dir + '\\a' + str(count) + '.jpg'
bpy.ops.render.render(write_still=True)
count = count + 1

We use pos to specify the position of the camera x, y, z and then the extrinsic Euler angles (in degree) of the camera w.r.t the absolute x, y, z axes. We have specified 4 positions, and of course you can automate by creating a longer list. The output is a set of 4 images, a0.jpg, a1.jpg, a2.jpg and a3.jpg as shown in figure (D) below.

process

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s