# How do I set my camera rotation with Python?

I'm working on an automatic renderer script, and I have some trouble setting the rotation of my camera. I suspect it has something to do with the need for me to convert the rotation values I want to quaternion values. The problem is that I am absolutely hopeless in maths and not very advanced in programming, I'm still learning. I have been looking on the internet but I have found very complex things such as this :

But I don't know where to start, I don't get what the code means and I don't know how to implement that to my case, so I figured it was best to ask from scratch.

SO basically I want my camera to have the same rotation I have created manually, that is to say : x= 66.9 y= 0 z= 28.9

Here's the last thing I have tried:

import bpy
import os
import math
import shutil
import glob
import bmesh
import mathutils

#Camera position
Camera = bpy.data.objects["Camera"]

pos= [24.408, -39.401, 33.853]
rot= [66.9, -0.000115, 28.9]

Camera.location.x = pos[0]
Camera.location.y = pos[1]
Camera.location.z = pos[2]

Camera.rotation_mode = 'XYZ'
Camera.rotation_euler[0] = rot[0]
Camera.rotation_euler[1] = rot[1]
Camera.rotation_euler[2] = rot[2]

Camera.rotation_euler.to_quaternion()


And errr.... it doesn't work at all lol, it puts my camera at 3833° X, -0.00661° Y and 1656° Z...

Can someone help me ?

Thank you so much :)

• important to note blender uses radians as its internal measure of angle. When you orbit 2pi radians around a circle you have walked the perimeter. In your question code for example cam.rotation_euler = [math.radians(r) for r in rot] is another way to convert. Aug 31, 2018 at 19:03
• Thanks batFINGER, your solution works as well!
– Popi
Sep 5, 2018 at 8:25

def setupCamera(scene, c):
pi = math.pi

scene.camera.rotation_euler[0] = c[0] * (pi / 180.0)
scene.camera.rotation_euler[1] = c[1] * (pi / 180.0)
scene.camera.rotation_euler[2] = c[2] * (pi / 180.0)

scene.camera.location.x = c[3]
scene.camera.location.y = c[4]
scene.camera.location.z = c[5]

return

scene = bpy.data.scenes["Scene"]

config = list([67.1349, 0.779594, 148.858, 5.57961, 9.16202, 5.34536])

cam = bpy.data.objects['Camera']
cam.rotation_mode = 'XYZ'

scene.camera = cam

setupCamera(scene=scene, c=config)

• Hey! thank you for your answer! I'm going to try that right away. Could you explain me a few things though? 1) what does "c" mean exactly? 2) Why do you operate pi/180? 3) What are those values you put in config = list ? Thanks a million!
– Popi
Aug 31, 2018 at 14:08
• If I remember correctly it is c = config = [xrotation, yrotation, zrotation, x, y, z] (you can get the values from the camera), if something doesn't work tell me :) I think pi/180 was degree to radian or something related Aug 31, 2018 at 14:58
• Hey! Thank ou! I'll give it a try when I got a moment this week, I'll keep you posted!
– Popi
Sep 3, 2018 at 11:04