mathutil matrix how to

is there a way to access a column with mathutil and how ?

like i can print a row like this


 
newMat = mathutils.Matrix(((1, 2, 3, 4),
 (0,-3,4, 5),
 (0,-2, -1, 3) ,
 (0,-6.5, -2, 1)))
 
print ('Matrix newMat= ',newMat)
 
print ()
print ('Matrix newMat[0]= ',newMat[0])
print ()
 
 

any good tut on the latest in 2.5 for mathutil?

thanks
happy 2.5

I don’t know if there is a built in method, but here is something banged together…


import bpy
import mathutils

newMat = mathutils.Matrix(((1, 2, 3, 4),
 (0,-3,4, 5),
 (0,-2, -1, 3) ,
 (0,-6.5, -2, 1)))
 
print ('Matrix newMat= ',newMat)
 
print ()
print ('Matrix newMat[0]= ',newMat[0])
print ()

def matcol(m,n):
    for r in m:
        print(r[n])
    return

matcol(newMat,1)

you could get it to return the result as a vector even…

ok printing the column values!

now i tried a lot of the other functions pre defined

but on many of these i get the address instead of the values!

like this one

print ()
deter=newMat.determinant
print ('Determinant = ',deter)

Identity Matrix = <built-in method identity of mathutils.Matrix object at 0x0CC
A7AB0>

i mean how do i get the matrix values from this address?

thanks


deter=newMat.determinant()
print ('Determinant = ',deter)

work for the determinant

but not the other ones
well it gives none

Identity Matrix = None
Invert Matrix

Invert Matrix = None
transpose Matrix

transpose Matrix = None
transposed Matrix = None

is that normal in this case ?

also at the top of the page for mathutil there are some functions in grey
with no doc or example

any ideas what to do with these?

like
classmethod OrthoProjection(axis, size)
Create a matrix to represent an orthographic projection.

Parameters:

  • axis (string or Vector) – Can be any of the following: [‘X’, ‘Y’, ‘XY’, ‘XZ’, ‘YZ’],where a single axis is for a 2D matrix. Or a vector for an arbitrary axis
  • size (int) – The size of the projection matrix to construct [2, 4].
    Returns:A new projection matrix.
    Return type:Matrix

but in any case it seems that mathutil cannot be use with matrix greater the 4X4
so will have to find another way for the bigger matrix N X N!

thanks
happy 2.5

see here:
you must be aware that some functions need an argument (matrix) which is then ‘changed’ see example:
>>> m
Matrix(((1.0, 0.0, 0.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0)))

>>> Matrix.zero(m)
>>> m
Matrix(((0.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 0.0, 0.0)))

>>> Matrix.identity(m)
>>> m
Matrix(((1.0, 0.0, 0.0, 0.0),
(0.0, 1.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, 0.0, 0.0, 1.0)))

ok thanks i see how to get it done now
there should a little bit more examples in the wiki pages for this
but i guess doc is not completed yet!

i got this old script tryng to make it work

so woudl like this old script to work again!

blmat[0]= [0.05, 0.0, 0.0]
Vec= Vector((1.399999976158142, 1.600000023841858, 1.7000000476837158))

Vec = mathutils.Vector(vec)

Blmat = mathutils.Matrix(blmat[0],blmat[1],blmat[2],blmat[3])

last line does not work anymore?

seems that vector are using list
but now matrix are using some sort of tuples instead!

is there a way to make this last line work again in 2.5 ?
like a way to translate list into tuple to make a matrix ?

thanks
happy 2.5

seem that these functions will modify the original matrix instead of creating a new one!

so here is a script which makes a copy for a new matrix and keep the original data matrix


 
import bpy
import mathutils
import mathutils.geometry
from mathutils.geometry import *
from math import *
from bpy.props import *
import random
 
 
newMat = mathutils.Matrix(((1, 2, 3, 4),
 (0,-3,4, 5),
 (0,-2, -1, 3) ,
 (0,-6.5, -2, 1)))
 
print ('Matrix newMat= ',newMat)
 
 
print ()
print ('  Determinant of A Matrix  ')
print ()
 
 
print ()
deter=newMat.determinant()
print ('Determinant = ',deter)
 
print ()
print (' Matrix  zero')
print ()
 
idmat=newMat.copy()
print (' idmat=',idmat)
print ()
#m0=newMat.zero(newMat)
m0=mathutils.Matrix.zero(idmat)
print ('m0 = ',m0)
print ('Matrix.zero  idmat = ',mathutils.Matrix.zero(idmat))
print (' idmat=',idmat)
print ()
print ('  Identity Matrix  ')
print ()
 
print ()
idmat=newMat.copy()
print (' idmat=',idmat)
ident=mathutils.Matrix.identity(idmat)
print ('Identity Matrix = ',ident)
print ('Identity Matrix = ',idmat)
 
 
 
print ()
print ('  Invert Matrix  ')
print ()
idmat=newMat.copy()
print (' idmat=',idmat)
print ()
inverse=mathutils.Matrix.invert(idmat)
print ('Invert Matrix = ',inverse)
print ('Invert Matrix = ',idmat)
 
print ()
print ('  transpose Matrix  ')
print ()
idmat=newMat.copy()
print (' idmat=',idmat)
print ()
transpos=idmat.transpose()
print (' idmat=',idmat)
print ()
print ()
print ('transpose Matrix = ',transpos)
 
 
print ()
idmat=newMat.copy()
print (' idmat=',idmat)
print ()
transpos=idmat.transposed()
print ('transposed Matrix = ',transpos)
 
 
 

thanks
happy 2.5


import bpy
import mathutils

blmat = mathutils.Matrix()
blmat.identity()
blmat[0]= [0.0, 0.05, 0.0, 0.0]

Blmat = mathutils.Matrix((blmat[0], blmat[1], blmat[2], blmat[3]))

Blmat.invert()

invert does not work

here is the whole little script
i added your definitions which should be taken out i guess
but you defined blmat as a matrix

which is not defined like that in the script but as a list!


 
 
 
print ()
print ('  Vector  X  TIMES A Matrix  ')
print ()
 
import time
 
 
def multiply(vec, mat):
 
 x = vec[0]*mat[0][0] + vec[1]*mat[0][1] + vec[2]*mat[0][2]
 y = vec[0]*mat[1][0] + vec[1]*mat[1][1] + vec[2]*mat[1][2]
 z = vec[0]*mat[2][0] + vec[1]*mat[2][1] + vec[2]*mat[2][2] 
 
 return x,y,z
 
 
 
blmat = [[0.05, 0.0, 0.0],[0.0, 0.0, 0.05],[0.0, -0.05, 0.0],[0.0, 0.0, 0.0]]
vec = [1.4,1.6,1.7]
 
 
 
ts =time.time()
#nr = 1000000
nr = 1000
 
 
 
for i in range(nr):
 
 x,y,z = multiply(vec,blmat)
 
 
 
te = time.time()-ts    
print("multipy ",nr, " Times    te ", te)
 
print ()
print("blmat[0]=",blmat[0])
 
print ()
 
""" 
blmat = mathutils.Matrix()
blmat.identity()
blmat[0]= [0.0, 0.05, 0.0, 0.0]
Blmat = mathutils.Matrix((blmat[0], blmat[1], blmat[2], blmat[3]))
print("Blmat=",Blmat)
Blmat.invert()
 
 """
 
 
Vec = mathutils.Vector(vec)
print("Vec=",Vec)
Blmat = mathutils.Matrix(blmat[0],blmat[1],blmat[2],blmat[3])
 
print ()
ts =time.time()
 
 
 
 
for i in range(nr):
 
 x,y,z = Vec*Blmat
 
 
 
te = time.time()-ts    
 
print("multipy ",nr, " Times    te ", te)
 
 
 

and your sort of saying that it cannot work with list only real matrix

thanks
happy 2.5

anyone has a sample file for using the functions from geometry may be?

thanks

happy 2.5

how do you convert this to 2.5 latest version ?

mat_rot = mathutils.RotationMatrix(radians(90), 4, ‘X’)

thanks
happy 2.5

@RickyBlender
open a python window in blender:
from mathutils import Matrix
help(Matrix)
and you can see how to build your rotation matrix!
(for the lazy ones :wink: )


import bpy
from mathutils import Matrix
from math import radians
rot = Matrix.Rotation(radians(90),4,'Z')
print(rot)

here example for 2.5


 Rotation transformation.= Matrix(((1.0, 0.0, 0.0, 0.0),
        (0.0, 7.549790126404332e-08, 1.0, 0.0),
        (0.0, -1.0, 7.549790126404332e-08, 0.0),
        (0.0, 0.0, 0.0, 1.0)))
Translation transformation.= Matrix(((1.0, 0.0, 0.0, 0.0),
        (0.0, 1.0, 0.0, 0.0),
        (0.0, 0.0, 1.0, 0.0),
        (1.0, 2.0, 3.0, 1.0)))

 

thanks
happy 2.5

You should edit your code, it does not work at all!

i copied the output from the console not the editor !

here is the code

vec = Vector((1.0, 2.0, 3.0))

mat_rot = mathutils.Matrix.Rotation(radians(90), 4, ‘X’)

print (’ Rotation transformation.=’,mat_rot)

print (’ vec =’,vec)
mat_trans = mathutils.Matrix.Translation(vec)
print ()
print (‘Translation transformation.=’,mat_trans)

happy 2.5

hmm Ricky, for output you do not need code for real code yes please (and vec before its use) …

Not sure if it was in this thread or via msg, but I saw this function (matMult) and thought it’d be quite simple to adapt to your needs of multiplying arbitrary sized matrices?

http://sites.google.com/site/socialstorage/orientationmatrix-basics

P.S. The example file uses old API…

Here’s an updated ‘Test_2’


from bge import logic
from bge import events
from math import sin, cos, pi

cont = logic.getCurrentController()
own = cont.owner

ad = 45 ## Angle in degrees

ar = ad/(180/pi) ## Convert to angle in radians

rot_matrix = [
              [cos(ar),-sin(ar), 0],
              [sin(ar), cos(ar), 0],
              [0      ,0       , 1]
             ]

key = logic.keyboard.events
k_space=key[events.SPACEKEY]

if k_space == 1:
    own.orientation=rot_matrix

i change the code example with vec before the codes
so it will work

but it’s not really important what vec value you use !

the commands are the important ones which works under 2.5

now for this BGE example seen to be done manually
why not use mathutil functions ?

not cetain are you saying you cannot use rotation and translation matrix when doing some BGE python code and have to do it manually ?

and this rotation matrix is valid only for 2 Dimensions not 3 if i remember well!
see


seems that in Blender rotation is complicated and can be done 3 Ways
Euler angles ,Axis angles or Quaternion/matrix

i’d like to see a more complete representation on how to use all these
as examples!
i’ll see what i can find

happy 2.5

That example was just to show how to update the examples in the dl file from that link, to help you with matMult() function contained within.

Yeah they are 2d (one each for x, y, z) then you multiply them together, see e.g. here even…
http://www.blender.org/documentation/blender_python_api_2_57_release/mathutils.html?