# Python, blender and weird rotate behaviour after restoring matrix

I am trying to save user matrix and set it back later when needed But after restoring the matrix the rotate function is behaving weird As if it is 4D instead of 3D. And t seemt the wrong view. What i did was open a default file and zoomed in. took these matrix values and placed them in VM. After restoring the view looks different. I tried to search for similar posts but could not find any answer Code i use is,

EDIT THE CODE ADDED

this is the code

import bpy
from mathutils import matrix

vm = ((0.9077774286270142, -0.4194521903991699, -6.8628960292471675e-09, 1.434309720993042),
(0.3097465932369232, 0.6703528165817261, 0.6743027567863464, 0.9151758551597595),
(-0.28283774852752686, -0.6121168732643127, 0.738455057144165, -0.902187168598175),
(0.0, 0.0, 0.0, 1.0))

for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
v3d = area.spaces[0].region_3d
if v3d :
v3d.view_matrix = vm
v3d.view_perspective = 'ORTHO'


Before running script

After running script.

What am I missing here?

• Try using import mathutils and mathutils.Matrix() to declare your matrix. – Robert Gützkow Dec 18 '19 at 19:41
• i already have mathutils imported but how do i use the mathutils.Matrix() it gives the error mathutils not defined – DGRL Dec 18 '19 at 19:53
• That depends on how you've imported mathutils. If you're using the Python console, instead of running a script/add-on, then it does from mathutils import * for you. Therefore you don't prefix with mathutils and write Matrix() directly. – Robert Gützkow Dec 18 '19 at 19:57
• I uploaded the code i use (complete) – DGRL Dec 18 '19 at 20:09

## 1 Answer

In order to use the Matrix class you can either use import mathutils and mathutils.Matrix() to initialize an instance or from mathutils import Matrix and then Matrix().

import bpy
import mathutils

view_matrix = mathutils.Matrix(([0.9077774286270142, -0.4194521903991699, -6.8628960292471675e-09, 1.434309720993042],
[0.3097465932369232, 0.6703528165817261, 0.6743027567863464, 0.9151758551597595],
[-0.28283774852752686, -0.6121168732643127, 0.738455057144165, -0.902187168598175],
[0.0, 0.0, 0.0, 1.0]))

for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for space in area.spaces:
if space.type == 'VIEW_3D':
v3d = space.region_3d
v3d.view_matrix = view_matrix
v3d.view_perspective = 'ORTHO'
break

• This is what i needed. Made a small adjustment to set the coords based on current view (so its not hardcoded) and works like a charm I realized that i still needed to learn a lot Thanks @Robert for your lesson and time Appriciate it – DGRL Dec 18 '19 at 20:30