# Applying Rotation Matrix To Bone

I’m working with SimpleOpenNI to capture Kinect data. They have a method getJointPositionSkeleton() which returns what appears to be a rotation matrix (I’m working under this assumption) example:

``````( 0.2973, 0.9137, -0.2772, 0.0000)
(-0.9531, 0.3014, -0.0288, 0.0000)
( 0.0573, 0.2727,  0.9604, 0.0000)
( 0.0000, 0.0000,  0.0000, 1.0000)
``````

I’ve been trying to figure out how to apply these rotations to my bones in Blender, currently, I’m constructing the Matrix within python and setting the matrix of the pose bone to the product of rotation matrix and the local matrix of the bone:

`bpy.data.objects[0].pose.bones[bone].matrix = location @ bpy.data.objects[0].data.bones[bone].matrix_local`

Honestly I’m out of my league math wise here and I’m looking for any push in the right direction, or someone to tell me if I’ve just completely goofed this up. In addition I’m able to grab the coordinates of the joints themselves but I am unaware of a way that this could be utilized to set the rotation of the bones, however I figured I’d add that piece of information just in case that could provide another path forward.

Thanks!

``````    >>> mat = Matrix((( 0.2973, 0.9137, -0.2772, 0.0000), (-0.9531, 0.3014, -0.0288, 0.0000), ( 0.0573, 0.2727,  0.9604, 0.0000), ( 0.0000, 0.0000,  0.0000, 1.0000)))
>>> mat.determinant()
1.0000813007354736

>>> mat.decompose()
(Vector((0.0, 0.0, 0.0)), Quaternion((0.7998514175415039, 0.09423322975635529, -0.10454785078763962, -0.5834618210792542)), Vector((1.000035047531128, 1.0000274181365967, 1.0000187158584595)))
``````

Yup, that looks pretty much like a rotation matrix.

Here’s some code I wrote a while back that applies a rotation to a pose bone; note how it uses the bone origin as the rotation origin:

``````    parent_bone = context.selected_pose_bones[0]
# parent_origin = parent_bone.matrix * parent_bone.bone.head
# seems like “head” property is actually tail of parent bone,
# real head of a bone is always at (0, 0, 0) in its own coord system
parent_origin = parent_bone.matrix * Vector((0, 0, 0))
parent_matrix = parent_bone.matrix.copy()
parent_bone.matrix = \
(
Matrix.Translation(parent_origin)
*
Matrix.Rotation(- self.rotation_angle, 4, parent_axis)
*
Matrix.Translation(parent_origin).inverted()
*
parent_matrix
)
``````

Hopefully that helps.

1 Like

I’m going to go ahead and provide the full code of my plugin and an example capture file, because I seem to keep arriving at the same place no matter what I do (even trying to adapt the code that ldo so graciously provided. Attached find my current code (for Blender 2.8), the test blend, and a gif of the results I’m seeing with the current code and a text file containing output from the SimpleOpenNI capture system I implemented. kinect2blend.py (6.7 KB) test.blend (769.1 KB) test_capture.txt (88 KB)