# Transform (Rotate) with 4x4 matrix via python

I have imported geometry that I am rigging for showing assembly of a product. The driving curves have been imported from rhino, from which I have built up the model further in Blender.

As part of the animation, I would like to show how all the parts fit in a box, when they are flat. However, rotating each part by eye is proving difficult as each part is quite 3 dimensional. Thus, via Rhino, I have calculated the transform matrix needed for each part in order for it to be flat on the ground.

I would like to use the data from this matrix to transform objects via python.

Update: Here is a simplified version:

, where the orange is the target.

Here is the output that I get from Rhnio python going from A to A':

R0=(-0.470458022353494,-0.807963312121372,0.354773921630525,-6631.4567921258), R1=(0.645229984870226,-0.589232780440685,-0.486295174846007,3943.31281557432), R2=(0.601953084407421,0.000129305847915359,0.798531444247745,2890.46728702281), R3=(0,0,0,1)

I make the following output into a python list and then set it to the updated matrix from above

import bpy
from mathutils import Matrix, Vector

obj = bpy.data.objects['Untitled'].pose.bones["topfrontleft"]

m =  (0.879972675860339,-0.0926548147745,0.465900391755896,-1329.45963312555), (0.443132751289856,-0.193193761313235,-0.875391075647866,1124.19004435354),(0.171118247050034,0.976775949687925,-0.128946848111055,3977.30420903923),(0,0,0,1)

m_new = Matrix(m)

obj.matrix = m_new


Here is a ling to the Blend file: http://1drv.ms/1X8vHXO

• I see you using Grasshopper?, you may be interested in Blender's equivalent Sverchok – zeffii Nov 19 '15 at 17:00
• Yes, I have dabbled with it, however in this case, I need to transform bones. To my knowledge, sverchok seems to be only for meshes. I also work with curve tools within animation Nodes. However Animation nodes is limited to a 3x3 matrix and I am not sure how to input the 4x4 data from gh into Animation Nodes – dimitarsp Nov 19 '15 at 17:15
• I found out how to perform matrix operations via pythoon with a 4x4 matrix, with the values I got from Rhino/Python. However in the blender console, it says that results may not be what is expected if the matrix is not unitized. However, I am not sure how to unitize it, in either Rhino/Python or Blender/Python – dimitarsp Nov 20 '15 at 17:10
• What line of code produces the warning message? – batFINGER Nov 21 '15 at 6:25
• Why do you think that Animation Nodes is limited to 3x3 matrices? In fact every time you use the matrix socket you will have a 4x4 matrix. – Jacques Lucke Nov 22 '15 at 7:52

I took your blend file and modified the code a little bit. At first I inserted the R0, R1, R2 and R3 values into the matrix. Then to transform a given matrix with another one you have to multiply them. The order is important.

import bpy
from mathutils import Matrix

bone = bpy.data.objects['Untitled'].pose.bones["topfrontleft"]

m = [ (-0.470458022353494, -0.807963312121372,     0.354773921630525, -6631.4567921258),
( 0.645229984870226, -0.589232780440685,    -0.486295174846007,  3943.31281557432),
( 0.601953084407421,  0.000129305847915359,  0.798531444247745,  2890.46728702281),
( 0,                  0,                     0,                  1) ]

matrix = Matrix(m)
bone.matrix = matrix * bone.matrix


After executing the code in your file the bone is 'flat', but instead of being parallel to the x axis (so that it matches the other edge) it is parallel to the y axis.. Are you sure that the matrix is correct? If yes you will need some other matrices to rotate it in the correct direction, but at least it's already flat :D

• Amazing. Thank you, Jacques! I did some plane rotation in Rhino, which is why i have the feeling that it is aligned to a different position. As this has been the first occassion working with matrices, I am still trying to wrap my head around the concept. Could you explain why the new matrix has to be multiplied with the old in order for it to work? – dimitarsp Nov 23 '15 at 15:11
• FYI: here is the correct matrix. m = (0.645229984870226,-0.589232780440685,-0.486295174846007,187.679183422907), (0.470458022353494,0.807963312121372,-0.354773921630525,2875.82315997439), (0.601953084407421,0.000129305847915359,0.798531444247745,2890.46728702281), (0,0,0,1). Also, wondering if this kind of transform orientation can be achieved solely in blender? – dimitarsp Nov 23 '15 at 15:27
• Hm explaining that in detail is a bit complicated and to be honest I also don't know too much about them.. Some time ago I created this video: youtube.com/… Maybe it helps you to understand how matrices work. Note: The combine matrices node in the Animation Nodes just multiplys the matrices. (But in reverse order so that it is easier to work with them as an artist.) – Jacques Lucke Nov 23 '15 at 15:31