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,


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

enter image description here

After running script.

What am I missing here?

  • $\begingroup$ Try using import mathutils and mathutils.Matrix() to declare your matrix. $\endgroup$ – Robert Gützkow Dec 18 '19 at 19:41
  • $\begingroup$ i already have mathutils imported but how do i use the mathutils.Matrix() it gives the error mathutils not defined $\endgroup$ – DGRL Dec 18 '19 at 19:53
  • $\begingroup$ 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. $\endgroup$ – Robert Gützkow Dec 18 '19 at 19:57
  • $\begingroup$ I uploaded the code i use (complete) $\endgroup$ – DGRL Dec 18 '19 at 20:09

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'
| improve this answer | |
  • $\begingroup$ 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 $\endgroup$ – DGRL Dec 18 '19 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.