The position of the point from which we look at the scene in the 3D viewport area can be controlled through the Blender Python API. We can read the coordinates of this point and the direction of view from the viewport, and also set the values we need for them.
First, let’s get a pointer to the 3D viewport area:
1 2 3 |
area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D') # <bpy_struct, Area at 0x0000016F1C7E3DC8> |
Now we can get the coordinates of the origin of the viewport:
1 2 3 |
area.spaces[0].region_3d.view_location # <Vector (-0.0958, -0.1309, -0.1229)> |
We got a vector from the 3D scene center, to the point from which we are looking at the scene.
We can also get the direction of the “look” from the viewport:
1 2 3 |
area.spaces[0].region_3d.view_rotation # <Quaternion (w=0.7144, x=0.5854, y=0.2429, z=0.2964)> |
We got a quaternion from the center of the scene in the direction of view from the 3D viewport.
We can get the offset value of the viewport along its view direction:
1 2 3 |
area.spaces[0].region_3d.view_distance # 3.4859158992767334 |
And finally, we can set the values for all these parameters ourselves to place the viewport in the position we need.
1 2 3 |
area.spaces[0].region_3d.view_location = (-3.8, -3.6, 0.9) area.spaces[0].region_3d.view_rotation = (0.71, 0.59, 0.24, 0.3) area.spaces[0].region_3d.view_distance = 7.5 |