Sub topic 15
Setting and reading the view from Python
This chapter assumes that you know Blender’s basic interface, can open the Scripting workspace or Python Console, and understand the difference between an editor and an area. The examples use Blender 3.2 or later, where context.temp_override() is available.
Why control the viewport with Python?
The 3D viewport has a view state. This state includes the viewing direction, projection mode, distance from the orbit pivot, and framing. You can change these values manually, but Python makes repeatable setups possible.
For example, a script can:
- Switch a viewport to the top view.
- Change perspective to orthographic projection.
- Frame the whole scene.
- Frame the selected objects.
- Read the current view settings for debugging or automation.
A viewport is an area whose type is VIEW_3D. Its view data is stored in area.spaces.active.region_3d.
Finding a 3D viewport
The following script searches the current screen for the first 3D viewport. It then temporarily supplies the area and its window region to a viewport operator.
import bpy
for area in bpy.context.screen.areas:
if area.type == "VIEW_3D":
region = next(
region for region in area.regions
if region.type == "WINDOW"
)
with bpy.context.temp_override(area=area, region=region):
bpy.ops.view3d.view_axis(type="TOP")
breakbpy.ops.view3d.view_axis() is an operator. Operators depend on context, so Blender must know which area should receive the operation. The temporary override provides that context without permanently changing Blender’s active editor.
The script stops after finding the first matching viewport. If you need to control several viewports, remove break and apply the operation to each one.
Reading and setting view properties
Use region_3d when you need view information directly.
import bpy
for area in bpy.context.screen.areas:
if area.type == "VIEW_3D":
region_3d = area.spaces.active.region_3d
print("Projection:", region_3d.view_perspective)
print("Distance:", region_3d.view_distance)
region_3d.view_perspective = "ORTHO"
breakview_perspective can be "PERSP", "ORTHO", or "CAMERA". view_distance is the distance between the view and its orbit pivot. Changing the projection directly is useful, but use viewport operators when you need Blender to perform a complete navigation action.
Framing the scene or selection
These operators match common navigation shortcuts:
import bpy
for area in bpy.context.screen.areas:
if area.type == "VIEW_3D":
region = next(
region for region in area.regions
if region.type == "WINDOW"
)
with bpy.context.temp_override(area=area, region=region):
bpy.ops.view3d.view_all()
bpy.ops.view3d.view_selected()
breakview_all() frames the visible scene, similar to Home. view_selected() frames the current selection, similar to Numpad Period. If no object is selected, the second operation may not produce a useful view.
Exercise
Which object provides the view properties, and which object supplies the context for a viewport operator?
Show answer
area.spaces.active.region_3d provides the view properties. context.temp_override() supplies the target area and window region for the operator.