# How to calculate the direction and up vector of a camera?

I'm trying to implement a prototype for a custom render engine using blender as content creation tool. One remaining questions is: How can we calculate the direction and up vector of the camera by its actual position and rotation properly?

2.8x

import bpy
from mathutils import Vector

cam = bpy.data.objects['Camera']
up = cam.matrix_world.to_quaternion() @ Vector((0.0, 1.0, 0.0))
cam_direction = cam.matrix_world.to_quaternion() @ Vector((0.0, 0.0, -1.0))


2.7x

import bpy
from mathutils import Vector

cam = bpy.data.objects['Camera']
up = cam.matrix_world.to_quaternion() * Vector((0.0, 1.0, 0.0))
cam_direction = cam.matrix_world.to_quaternion() * Vector((0.0, 0.0, -1.0)) • Nice. Thank you pink vertex! I've also added a cam_direction.normalize() and up.normalize(). Works like a charm. – p2or Jul 3 '14 at 18:47
• Since it uses .to_quaternion, calling normalize should be unnecessary because a pure rotation preserves the length of a vector, which equals 1.0 in both of these cases. – pink vertex Jul 4 '14 at 15:32

Alternatively

Can get the global directions of local axes from the columns of the matrix world's rotation matrix.

>>> cam = C.scene.camera
>>> cam
bpy.data.objects['Camera']

>>> right, up, back = cam.matrix_world.to_3x3().transposed()
>>> cam_direction = -back

• Yeah, good idea. – p2or Dec 14 '19 at 12:29

As of Blender 2.8x, you have to use the @ operator to multiply matrices:

up = cam.matrix_world.to_quaternion() @ Vector((0.0, 1.0, 0.0))
cam_direction = cam.matrix_world.to_quaternion() @ Vector((0.0, 0.0, -1.0))


Matrix multiplication previously used *, scripts should now use @ for multiplication (per PEP 465).