Changing the viewing angle in a blender using Python

I am trying to figure out if there is a way to change the viewing angle in a blender using Python. I would like to get the result, as you could get by pressing 1, 3 or 7 on num. pads.

Thanks for any help

+6
source share
2 answers

First of all, please note that you can immediately open several 3D views, and each of them can have its own viewing angle, perspective / spelling settings, etc. So your script will have to look for all the 3D views that can (which could be nobody) and decide which one will affect.

Start with the bpy.data object that has window_managers . This collection always has only one item. However, there may be one or more open windows . Each window has a screen that is divided into one or more areas . Therefore, you need to search all areas for one with the type space of "VIEW_3D". Then go through the spaces of this area for one (s) of type "VIEW_3D". Such a space will have a subclass of SpaceView3D . This will be the region_3d attribute of type RegionView3D . And finally, this object, in turn, has a view_matrix attribute, which takes a value of type Matrix , which you can get or set.

Got it all? :)

+6
source

Once you post the right “view”, you can change:

view.spaces[0].region_3d.view_matrix view.spaces[0].region_3d.view_rotation 

Note that region_3d.view_location is the target of 'look_at', not the location of the camera; you need to change view_matrix directly if you want to move the camera position (as far as I know), but you can easily adjust the rotation using view_rotation quite easily. You will probably need to read this to generate a valid quaternion: http://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

Perhaps something like this might be useful:

 class Utils(object): def __init__(self, context): self.context = context @property def views(self): """ Returns the set of 3D views. """ rtn = [] for a in self.context.window.screen.areas: if a.type == 'VIEW_3D': rtn.append(a) return rtn def camera(self, view): """ Return position, rotation data about a given view for the first space attached to it """ look_at = view.spaces[0].region_3d.view_location matrix = view.spaces[0].region_3d.view_matrix camera_pos = self.camera_position(matrix) rotation = view.spaces[0].region_3d.view_rotation return look_at, camera_pos, rotation def camera_position(self, matrix): """ From 4x4 matrix, calculate camera location """ t = (matrix[0][3], matrix[1][3], matrix[2][3]) r = ( (matrix[0][0], matrix[0][1], matrix[0][2]), (matrix[1][0], matrix[1][1], matrix[1][2]), (matrix[2][0], matrix[2][1], matrix[2][2]) ) rp = ( (-r[0][0], -r[1][0], -r[2][0]), (-r[0][1], -r[1][1], -r[2][1]), (-r[0][2], -r[1][2], -r[2][2]) ) output = ( rp[0][0] * t[0] + rp[0][1] * t[1] + rp[0][2] * t[2], rp[1][0] * t[0] + rp[1][1] * t[1] + rp[1][2] * t[2], rp[2][0] * t[0] + rp[2][1] * t[1] + rp[2][2] * t[2], ) return output 
+4
source

Source: https://habr.com/ru/post/906994/


All Articles