# How create a valid matrix?

I’m trying to orient objects with a script (not in the Game Engine, in Blender, <alt>P), and I think I should be able to use setMatrix to do this, but I can’t seem to correctly create the matrix. When I use getMatrix, I get a 4x4 result, but when I try to emulate that and say:

``````
newMat = [[1, 0,  0, 0],[0,-0,1, 0],[0,-1, -0, 0] ,[0,-6.5, -0, 1]]

``````

it’s not accepted by setMatrix (says it needs a matrix object).

My intention here is simply to set the camera in the grid plane, pointed at the default cube, and once I can do that, use the same method to orient other objects.

What I did was manually position the camera approx. in the grid plane, pointed at the cube, and then made a script with “getMatrix”; then I rounded the resultant numbers to closest integer (the last set of 4 is presumably the location of the camera, and I’m not sure what the first three are).

So whether I have the right values at this time in my attempt or not, clearly I’m doing something wrong in how I’m trying to make the matrix.

Can anyone try to clue me in?

``````
&gt;&gt;&gt; newMatrix = Matrix()
&gt;&gt;&gt; newMatrix
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)))

``````

so

``````
&gt;&gt;&gt; newMatrix = Matrix(((1, 0,  0, 0),(0,-0,1, 0),(0,-1, -0, 0) ,(0,-6.5, -0, 1)))
&gt;&gt;&gt; newMatrix
Matrix(((1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, -6.5, 0.0, 1.0)))

``````

thanks zeffii, I finally tried something which may be functionally identical to your explanation and sample:

``````
newMat = Mathutils.Matrix([1, 0, 0, 0],
[0,-0,1, 0],
[0,-1, -0, 0] ,
[0,-6.5, -0, 1])

``````

and it seems to work fine!

But I do notice that the values in the bottom two rows, third from left, start out as negative values, but when I use that matrix in setMatrix, the result of that application, while otherwise identical to the matrix, show in those positions as positives, and I don’t know if negative zeros vs positive zeros means anything special to Blender or not.

(edit) and now I notice the same thing showing in your example, too, so I presume that Blender knows there’s no such thing as a negative zero and simply fixes it.

technically in finite precision computing i think there is such a thing as negative 0, but it’s more the result of a rounding; like -0.000000003, rather than a useful mathematical construct. An interesting thread over at stackoverflow.com/negative-zero-in-python

and Mathutils in blender 2.5x is fully lowercase ‘mathutils’ (for anyone else using 2.5)

``````
&gt;&gt;&gt; newMat = mathutils.Matrix(([1, 0, 0, 0],[0,-0,1, 0],[0,-1, -0, 0],[0,-6.5, -0, 1]))
&gt;&gt;&gt; newMat
Matrix(((1.0, 0.0, 0.0, 0.0),
(0.0, 0.0, 1.0, 0.0),
(0.0, -1.0, 0.0, 0.0),
(0.0, -6.5, 0.0, 1.0)))

``````

using lists or tuples, is a personal choice:

• you can edit lists, in place.
• you need to overwrite an entire tuple, if you want to edit one value

ok, and one more question: what exactly dothe values in the rows and columns mean?

I know the last row are the xyz coords of the object (in this case the camera), and I thought I understood what the others were, but when I try to change the values in a different matrix (a text font mesh) to make it be perpendicular to the camera, it doesn’t work right; it changes the scale, which I pretty much expected since that info, contained in the matrix, was changed, but it also changed the orientation in an unexpected direction.

Try to get and use the Api advisor, activated by Ctrl F (if the addon is ON) and then type eg. mathutils
and you see what it can do, then click on Matrix (you see it at once!) there you find Rotation, Scale, Translation which are all
in the 4x4 matrix (hidden)! try and experiment ;-)! in the python console of any object
getting a matrix with a scaled tranlated rotated object via: >>> C.active_object.matrix_world
or >>> testM = C.active_object.matrix_world
to check testM

how do you activate this addon ?

and is there a thread or site showing how to use this script ?

i found it it is called API Navigator

to activate it Ctrl-F
but you need to click on the little triangle in sub panel to see it appear

but then how do you use it ?

thanks
happy 2.5

Just as I said (green where you start typing, e.g. mathutils or bpy or …)

very cool script

thanks