1
$\begingroup$

I'm moving my camera on a Bezier circle drawn around an object. To achieve this, I added an Empty, parented the Empty to the camera, added a Follow Path constraint to the Empty (set the target as my object), and finally added a Track To constraint to my camera. With this setup, I can move the camera on an arbitrary path by arranging the control points of the Bezier circle.

enter image description here

While doing this, I also want to get the camera location and orientation for each frame using this script:

import bpy
scene = bpy.context.scene

def handler(scene):
    print("Frame Change", scene.frame_current)
    print("Camera location", scene.camera.location)
    print("Camera rotation", scene.camera.rotation_quaternion)
    
bpy.app.handlers.frame_change_pre.append(handler)
bpy.ops.screen.animation_play()

However, I always get the same camera transform regardless of the camera position along the path:

Frame Change 1
Camera location <Vector (0.0000, 0.0000, 0.0000)>
Camera rotation <Quaternion (w=1.0000, x=0.0000, y=0.0000, z=0.0000)>
Frame Change 2
Camera location <Vector (0.0000, 0.0000, 0.0000)>
Camera rotation <Quaternion (w=1.0000, x=0.0000, y=0.0000, z=0.0000)>
...

How can I get the world coordinates of the camera at each frame?

$\endgroup$

1 Answer 1

2
$\begingroup$

use this:

import bpy

ob = bpy.context.scene.objects["Camera"]

for frame in range(0,250):
    
    bpy.context.scene.frame_set(frame)
    
    print (ob.matrix_world[0][3], ob.matrix_world[1][3], ob.matrix_world[2][3])
$\endgroup$
1
  • 1
    $\begingroup$ Thanks. Just a note: This gives the location; to get both location and rotation, we should use location, rotation = ob.matrix_world.decompose()[0:2]. $\endgroup$ Dec 3, 2021 at 13:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.