 Harry Mustoe-Playfair - 2 years ago 76
Python Question

# Blender camera rotation with python - not planar?

I'm trying to create a script that renders a 360 degree rotation of an object. I'm not familiar with blender at all, but I've been trying my best to do this.

I'm writing a script using Python that imports an STL file and creates the 360 render of it. My problem is, when moving the camera, the camera doesn't move in the right plane.

Here's an image album describing what I mean:

http://imgur.com/a/FkVLp

The function I'm using to do this is:

``````def rotateCameraAroundOrigin(rx=0):
cam = bpy.data.objects['Camera']
old_x = cam.location.x
old_y = cam.location.y
old_z = cam.location.z
radius = math.sqrt((math.pow(old_x,2) + math.pow(old_y,2)))
current_angle = math.degrees(math.atan2( old_y, old_x))
print("CUR:\t%+04d degrees" % (current_angle))
new_angle = current_angle + rx
print("FROM:\t%+04d, %+04d, %+04d" % (old_x,old_y,old_z))
moveCamTo(new_x,new_y,old_z)
print("TO:\t%+04d, %+04d, %+04d\n" % (new_x,new_y,old_z))
``````

and then:

``````for i in range(1, 10):
print("Rotation %01d" % (i))
image = 'images/' + sys.argv[-1] + str(i) + '.' + filetype
rotateCameraAroundOrigin(36)
render_thumb(image,gl=False)
``````

Which produces the output shown in the imgur album:

``````Rotation 1
CUR:    +000 degrees
FROM:   +302, +000, +000
TO:     +244, +177, +000

Rotation 2
CUR:    +035 degrees
FROM:   +244, +177, +000
TO:     +093, +287, +000

Rotation 3
CUR:    +071 degrees
FROM:   +093, +287, +000
TO:     -093, +287, +000

Rotation 4
CUR:    +107 degrees
FROM:   -093, +287, +000
TO:     -244, +177, +000

Rotation 5
CUR:    +143 degrees
FROM:   -244, +177, +000
TO:     -302, +000, +000

Rotation 6
CUR:    +179 degrees
FROM:   -302, +000, +000
TO:     -244, -177, +000

Rotation 7
CUR:    -144 degrees
FROM:   -244, -177, +000
TO:     -093, -287, +000

Rotation 8
CUR:    -108 degrees
FROM:   -093, -287, +000
TO:     +093, -287, +000

Rotation 9
CUR:    -072 degrees
FROM:   +093, -287, +000
TO:     +244, -177, +000
``````

I'm at a loss. My plane is tilted, but I don't know why. I want to rotate smoothly round the object.

Thanks for any help,

H neomonkeus

Why not create an empty object at the objects location. Parent the camera to it. Then apply a rotation matrix to the empty. The parenting will cause the camera to rotate through desired rotational arc.

This approach is a lot more simple and is used predominately in speed modelling where you want the camera to view the progress of the entire object.

``````def parent_obj_to_camera(b_obj, b_camera):
origin = (0,0,0) #can be replaced with b_obj.location
b_empty = bpy.data.objects.new("Empty", None)
b_empty.location = origin
b_camera.parent = b_empty #setup parenting

scn = bpy.context.scene
scn.objects.active = b_empty
b_empty.select = True
``````

Then in that for loop you would create a Matrix which would rotate the empty

``````import mathutils
from math import radians

b_empty = bpy.context.scene.active_object
num_steps = 9
stepsize = 360/num_steps
for i in range(0, num_steps):
mat_rot = mathutils.Matrix.Rotation(radians(step), 4, 'X')
b_empty.matrix_local *= mat_rot

print("Rotation %01d" % (radians(stepsize)))
image = 'images/' + sys.argv[-1] + str(i) + '.' + filetype
render_thumb(image,gl=False)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download