2
$\begingroup$

Not Camera

I found two ways:

https://docs.blender.org/api/current

/bpy.ops.view3d.html#bpy.ops.view3d.view_orbit
bpy.ops.view3d.view_orbit(angle=0.0, type='ORBITLEFT')

https://docs.blender.org/api/current

/bpy.ops.view3d.html#bpy.ops.view3d.view_roll
bpy.ops.view3d.view_roll(angle=0.0, type='ANGLE')

Relative to screen

  • view_orbit can rotate up and down —Rotate around the x-axis of the screen
  • view_roll can rotate left and right (clockwise and counterclockwise) — Rotate around the z-axis of the screen

How to rotate around the screen y axis?

enter image description here

$\endgroup$

1 Answer 1

3
$\begingroup$

Access region_3d.view_rotation

https://docs.blender.org/api/2.92/bpy.types.RegionView3D.html?highlight=view_rotation#bpy.types.RegionView3D.view_rotation

Rotate Y axis 10 degrees of first viewport

import bpy
from mathutils import Euler
from math import radians

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

area = find_area()

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

    # rotate Y 10 degrees
    q = Euler((0.0, radians(10.0), 0.0), 'XYZ').to_quaternion()
    rot[:] = q @ rot

If you have a 3D mouse

bpy.ops.view3d.ndof_all()
# Pan and rotate the view with the 3D mouse

bpy.ops.view3d.ndof_orbit()
# Orbit the view using the 3D mouse

bpy.ops.view3d.ndof_orbit_zoom()
# Orbit and zoom the view using the 3D mouse

bpy.ops.view3d.ndof_pan()
# Pan the view with the 3D mouse

Rotate from global axis

import bpy
from mathutils import Euler, Vector
from math import radians

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

area = find_area()

r3d = area.spaces[0].region_3d
r3d.update() # update view_matrix
rot = r3d.view_rotation

x = Vector((1,0,0))
y = Vector((0,1,0))
z = Vector((0,0,1))

mat = r3d.view_matrix.Rotation(radians(10), 4, rot @ z)
# mat = r3d.view_matrix.Rotation(radians(10), 4, rot @ y)
# mat = r3d.view_matrix.Rotation(radians(10), 4, rot @ x)
l, r, s = mat.decompose()

rot[:] = r @ rot
$\endgroup$
2
  • $\begingroup$ Thanks for the answer, how should I rotate relative to the screen? I added a picture to describe $\endgroup$ Commented Feb 14, 2023 at 15:57
  • $\begingroup$ I added a method to rotate the global axis. z-axis faces the screen. $\endgroup$
    – X Y
    Commented Feb 15, 2023 at 11:42

You must log in to answer this question.

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