I'm trying to append an object so that it is placed at the 3D cursor and rotated in the Z axis so that it is facing the viewport. I can set the location easily like this:

object.location = context.scene.cursor.location

But how do I get the Z rotation of the viewport camera relative to the 3D cursor?


1 Answer 1


Get z rotation of the viewport

import bpy, math

def find_area():
        for a in bpy.data.window_managers[0].windows[0].screen.areas:
            if a.type == "VIEW_3D":
                return a
        return None
        return None

area = find_area()

if area is None:
    print("area not find")
    r3d = area.spaces[0].region_3d
    z = r3d.view_rotation.to_euler().z

    print(" Z rotation of the viewport is: ")
    print("     radians: ", z)
    print("     degrees: ", math.degrees(z))

You must log in to answer this question.

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