2
$\begingroup$

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 :

https://blenderartists.org/t/questions-about-python-controlled-camera-rotation-and-location-in-blender-2-59/520639

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 :)

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – batFINGER
    Commented Aug 31, 2018 at 19:03
  • $\begingroup$ Thanks batFINGER, your solution works as well! $\endgroup$
    – Popi
    Commented Sep 5, 2018 at 8:25

1 Answer 1

5
$\begingroup$
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])

bpy.ops.object.camera_add()
cam = bpy.data.objects['Camera']
cam.rotation_mode = 'XYZ'

scene.camera = cam

setupCamera(scene=scene, c=config)
$\endgroup$
4
  • $\begingroup$ 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! $\endgroup$
    – Popi
    Commented Aug 31, 2018 at 14:08
  • $\begingroup$ 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 $\endgroup$
    – WhatAMesh
    Commented Aug 31, 2018 at 14:58
  • 1
    $\begingroup$ Hey! Thank ou! I'll give it a try when I got a moment this week, I'll keep you posted! $\endgroup$
    – Popi
    Commented Sep 3, 2018 at 11:04
  • $\begingroup$ Instead of multiplying by pi / 180 to get radians, just use the math.radians function on the c[i] terms directly: docs.python.org/3/library/math.html#math.radians $\endgroup$
    – Raleigh L.
    Commented Oct 10, 2023 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .