Is there a way to programmatically achieve the same thing as pressing the
0 key the numpad?
The operator for switching the 3D view which is associated with numpad keys is
view3d.viewnumpad(). You have to be in the right context (3d_view) to use it and select which view you want to set:
import bpy for area in bpy.context.screen.areas: if area.type == 'VIEW_3D': override = bpy.context.copy() override['area'] = area bpy.ops.view3d.viewnumpad(override, type = 'CAMERA') break
Another way, without the operator would be:
import bpy for area in bpy.context.screen.areas: if area.type == 'VIEW_3D': area.spaces.region_3d.view_perspective = 'CAMERA' break
Which is equivalent to:
import bpy area = next(area for area in bpy.context.screen.areas if area.type == 'VIEW_3D') area.spaces.region_3d.view_perspective = 'CAMERA'
Other options are
[‘PERSP’, ‘ORTHO’, ‘CAMERA’]