# Python rotate view on any given angle x , y , or z axis

Using bpy.context.screen.areas you can get the current view or set the view in TOP or 1 of the other pre set views like LEFT or BOTTOM

How can i change the view to any given angle in Python?

For example I would like to rotate the view 45 degrees X axis and 0 for Y and 0 for Z or i would like to see 45 degrees on all 3 axis

in API i was unable to find any that suggest user input to rotate the view

## 1 Answer

Use rotation matrix

Similarly to object rotation to rotate the view from its current rotation 45 degrees about x axis

Using code from a previous answer

import bpy
from math import radians
from mathutils import Matrix
context = bpy.context

R = Matrix.Rotation(radians(45), 4, 'X')

for area in context.screen.areas:
if area.type == 'VIEW_3D':
space = area.spaces.active
v3d = space.region_3d
v3d.view_matrix = R @ v3d.view_matrix
v3d.view_perspective = 'ORTHO'
break


To set it to this rotation instead of transforming by set the view rotation (Quaternion)

        v3d.view_rotation = R.to_quaternion()


To set it to an Euler

from mathutils import Euler
q = Euler((radians(45), radians(45), radians(45)), 'XYZ').to_quaternion()
v3d.view_rotation = q

• What do you want me to say? just what i needed (after little tweek I got the result im after) Thanks @batFINGER
– DGRL
Dec 19, 2019 at 12:58