matrices in phyton

so hy there again!!
just a simple question
i have no practice with using matrices in phyton
Now I want to create a matrix with n raws and colums where i can play around :slight_smile:

I use embedded lists for that
so for a two-dimensional 10 * 10 matrix it’s


matrix = []
for i in range(10):
  embeddedList = []
  for ii in range(10)
    embeddedList.append("someValue")
  matrix.append(embeddedList)

to get the value at row 5 column 2 just use

value = matrix[5][2]

Maybe there’s a better way, this one works fine for me

yeah jonasbalmer
its much comfortabler then this

##DUNGEON GEN

“”"
Thomas Brunner
“”"

import Mathutils

array = [
[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,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,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],
[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,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,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],
[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,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,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],
[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,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,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],
[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,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,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],
[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,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,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]
]

start = int(10 * Mathutils.Rand(0,2.3))
#end = int(10 * Mathutils.Rand(0,2.4))

LEGENDE##########

0… Leer

1… Weg

x… Hindernis

s… Start

z… Ziel

####################

row = 0
col = int(10 * Mathutils.Rand(0,2.4)) #start

array [0] [start] =1
for i in range (0,100):
dir = int(10 * Mathutils.Rand(0,0.4))
if dir == 0:
row = row +1
if row > 23:
row = 23
elif dir == 1:
col = col -1
if col < 0:
col =0
elif dir == 2:
col = col +1
if col > 23:
col = 23
array[row][col]=1

print “start”
print array
print “end”

All these posts seem quite confusing to me, Id just convert the matrix to mathutils then its all very simple, only remember you must transpose the matrix before and after using it.

eg…
from Mathutils import *
mat = Matrix(*ob.orientation).transpose()
mat = mat * RotationMatrix(angle, 3, ‘z’)
ob.orientation = mat.transpose()

Note, in blender 2.5, all Matrix’s are mathutils without having to convert or transpose

Here is a great resource for those looking to learn how matrices work in python. Have a look:
http://socialstorage.googlepages.com/orientationmatrix-basics

He explains everything pretty well, even how to avoid strange deformations when manipulating the orientation matrix.

Definitely worth a read. = )

bump

I’m trying to get orientation matrix, then transpose it and define new orientation matrix with rotation matrix, transpose it back and apply to object orientation

my script is here:

import Mathutils 
from Mathutils import Matrix 
 
cont = GameLogic.getCurrentController() 
 
own = cont.owner 
 
mat = Matrix(own.localOrientation).transpose() 

rot = 90.0

turn_left = cont.sensors["Qkey"]

if turn_left.positive: 
    ori = mat.RotationMatrix(-rot,3,"z").transpose()

own.localOrientation = ori

and that’s error I get… what’s wrong? docu is saying ‘localOrientation’ get orientation in 3x3 matrix, but error is saying I haven’t matrix with rows and columns between 2 and 4 :spin:

Python script error from controller "cont#CONTR#1":
Traceback (most recent call last):
  File "tilemove_alpha01", line 8, in &lt;module&gt;
RuntimeError: matrix(): row and column sizes must be between 2 and 4

thx

Matrix() receives 3 parameters one with each line, ex: Matrix(line1, line2, line3). But .localOrientation returns only 1 item which is a list with all 3 lines. So there’s an imcompatiblity, you can’t just say Matrix(own.localOrientation) because you’re passing only 1 value instead of three.

I fix this, personally do:
Matrix(own.localOrientation[0], own.localOrientation[1], own.localOrientation[2])

This will make it work. But it looks a bit bad, if anyone knows a better way, please let me know.

great, thx! it’s working now…
but have another error with rotationMatrix

import Mathutils
from Mathutils import Matrix 

cont = GameLogic.getCurrentController() 

own = cont.owner 

ori = own.localOrientation 

mat = Matrix(ori[0],ori[1],ori[2]).transpose() 

turn_left = cont.sensors["Qkey"]

rot = 90.0

if turn_left.positive: 
       mat = mat.RotationMatrix(-rot,3,"z")

own.localOrientation = mat.transpose()

I’m getting error “AttributeError: RotationMatrix”

mat is a Matrix object and you’re calling mat.RotationMatrix, but RotationMatrix isn’t a method of the Matrix class so mat doesn’t have any attribute with that name. It’s a function in the Mathutils module. You can call it with Mathutils.RotationMatrix().

Notice in my example I did…
mat = Matrix(*ob.orientation).transpose()
Which is like…
mat = Matrix(ob.orientation[0], ob.orientation[1], ob.orientation[2]).transpose()

This is a feature of the python language

That is cool, I didn’t know that. It’s gonna make my scripts look much cleaner :slight_smile:

Well this is all almost enlightening, but in the end still quite confusing. Coming from Java where a 2 dimensional array was just one declared array inside of another (both fixed length from the beginning), this is weird. The first proposal (using matrix[]/embeddedList[]) made most sense, but then moving to the built in matrices it would be helpful if somebody could offer a more basic explanation/differentiation. From what I can see I’m assuing that the mathutils matrices are only really useful for working with orientation. Am I wrong? If I just wanted to declare a 16 x 16 matrix and populate it with values (either at the time of its creation or by calling it array style (matrix[1][16] = x)) could I? What would that code look like? If anybody could shed some more general light it would be useful.
Thanks,
conartist6