# How can I navigate the viewport using Python?

Normally, with most actions that you want to script with Python there is a pop-up tooltip that shows you the function that you want to use. However, for viewport navigation there are no pop-ups, you use the mouse directly for everything (scrolling to zoom, middle-click-and-drag to pan). How can I navigate the viewport with Python?

• Why would you want to do this? – iKlsR Jun 6 '13 at 2:06
• @iKlsR, I am working on developing a small addon that allows you to use basic Blender functions through a gamepad or joystick. Plus, this could be useful to others in the future, as the method of accessing the viewport through Python is not readily apparent. – fouric Jun 6 '13 at 2:08

I would suggest not to call operators at all, these are rather cumbersome way to interact with the view, Instead manipulate the view directly.

If you write an operator you can access the viewport like this...

v3d = context.space_data
rv3d = v3d.region_3d
rv3d.view_location.x += 1.0


You may also want to treat the view as a 4x4 matrix since in some cases this is more convenient since you can multiply it with other matrices to combine transformations, accessible from the view_matrix.

To get started you may want to try things out in the python console...

For this you need to specify the absolute path to the 3D region.

In the Scripting screen layout you can run this from the console or editor...

rv3d = bpy.context.screen.areas[2].spaces[0].region_3d
rv3d.view_rotation.rotate(Euler((0, 0, 0.1)))
rv3d.view_location.x += 1.0
rv3d.view_distance -= 1.0


We have a template of an operator that edits the view in the text editor Operator Modal View3D

• Could you show how to bring this into newer versions of blender? – Troy Carpenter Dec 24 '20 at 21:37

Hover over the menu items, the options/commands are there, Go to the 3d view header then ViewNavigation. You can hover over a menu item and use Ctrl + C to copy the command to your clipboard.

For example, to pan to the right, you can use bpy.ops.view3d.view_pan(type='PANRIGHT') and to zoom out you can use bpy.ops.view3d.zoom(delta=-1).

I don't think you will be able to move very naturally however, not quite in the way you want to at least.