16
$\begingroup$

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?

$\endgroup$
2
  • 1
    $\begingroup$ Why would you want to do this? $\endgroup$
    – iKlsR
    Commented Jun 6, 2013 at 2:06
  • 1
    $\begingroup$ @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. $\endgroup$
    – fouric
    Commented Jun 6, 2013 at 2:08

2 Answers 2

18
$\begingroup$

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.

See RegionView3D API docs

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

$\endgroup$
1
  • $\begingroup$ Could you show how to bring this into newer versions of blender? $\endgroup$ Commented Dec 24, 2020 at 21:37
7
$\begingroup$

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.

enter image description here

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.

$\endgroup$
0

You must log in to answer this question.

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