# Automate the rotation of camera around an object with python

I have a 3D model of a liver and I wanted to get the rendered images of the model from various angles like the ones shown below. The path the camera would traverse would be roughly circular/elliptical with a fixed height from the X-Y plane. The problem is in every iteration I'm also doing some other computations in python with a script. So I wanted to add the automation of rotation along with my script in Python rather than doing it one at a time but am not able to figure out how to do so.

Try using Python Scripting. The following script rotates the camera from initial position

X: 7m
Y: 1m
Z: 1m
Rotation_Z = 90 degrees (a.k.a cam.rotation_euler[2])


upto 90 degrees and captures num_steps frames. Change it to 360 for a complete view. R describes the distance from the target object (The Default Cube in my case). The radius_range contains all the radii to rotate in.

import bpy
import os
from math import *
from mathutils import *

target = bpy.data.objects['Cube']
cam = bpy.data.objects['Camera']
t_loc_x = target.location.x
t_loc_y = target.location.y
cam_loc_x = cam.location.x
cam_loc_y = cam.location.y

init_angle  = (1-2*bool((cam_loc_y-t_loc_y)<0))*acos((cam_loc_x-t_loc_x)/R)-2*pi*bool((cam_loc_y-t_loc_y)<0) # 8.13 degrees
target_angle = (pi/2 - init_angle) # Go 90-8 deg more
num_steps = 10 #how many rotation steps

for x in range(num_steps):
alpha = init_angle + (x)*target_angle/num_steps
cam.rotation_euler[2] = pi/2 + alpha #
cam.location.x = t_loc_x+cos(alpha)*r
cam.location.y = t_loc_y+sin(alpha)*r

# Define SAVEPATH and output filename
file = os.path.join('renders/', str(x)+'_'+ str(r)+'_'+str(round(alpha,2))+'_'+str(round(cam.location.x, 2))+'_'+str(round(cam.location.y, 2)))

# Render
bpy.context.scene.render.filepath = file
bpy.ops.render.render(write_still=True)



In my, case, all cameras positions produced by this code look like

I hope it helps :)