## Getting quaternion from matrix

Clarisse Scripting related topics

### Getting quaternion from matrix

Hi,

I'm trying to get the quaternion of an object from its global matrix.
When I run this code:
Code: Select all
`rotMatrix = ix.api.GMathMatrix4x4d()matrix.extract_rotation(rotMatrix)rotQuat = ix.api.GMathQuat()rotQuat.set_rotation(rotMatrix)`

I'm getting the following error:
Code: Select all
`TypeError: Wrong number or type of arguments for overloaded function 'GMathQuat_set_rotation'.  Possible C/C++ prototypes are:    GMathQuaternion< double >::set_rotation(double const &,GMathVec3d const &)    GMathQuaternion< double >::set_rotation(GMathVec3d const &,GMathVec3d const &`

While in the documentation it says that we should be able to do it from a rotation matrix if I'm not wrong:
Code: Select all
`set_rotation (const U &m)U   The type of the input matrix. It's automatically deduced, and works with 3x3 and 4x4 matrices.`

I also tried to set the rotation for each angle and rotation axis, but then I'm not quite sure how I could combine them to get the global quaternion of the object:
Code: Select all
`rotate = ix.api.GMathVec3d()matrix.compute_euler_angles(rotate)xRot = rotateyRot = rotatezRot = rotatexRotAxis = ix.api.GMathVec3d(1,0,0)yRotAxis = ix.api.GMathVec3d(0,1,0)zRotAxis = ix.api.GMathVec3d(0,0,1)rotQuat = ix.api.GMathQuat()rotQuat.set_rotation(xRot, xRotAxis)roQquat.set_rotation(yRot, yRotAxis)roQuat.set_rotation(zRot, zRotAxis)`

So i was curious to check on this to see if you would know how to do it.

Thanks,
Jeremy
jboissinot

Posts: 108
Joined: Tue Jan 29, 2019 10:36 pm

### Re: Getting quaternion from matrix

Hi Jeremy,

There are missing API bindings in Python in 5.0 SP10b, so the C++ SDK documentation is not reliable for GMathQuaternion in Python here.
It seems to be a regression from previous SPs. Sorry about that.
What version are you using?

In the meantime, here is a workaround.

#### python code

```rotMat = ix.api.GMathMatrix4x4d() # for the sake of the example# Extract Euler angles, default rotation order: ZXYeulerAngles = ix.api.GMathVec3d()rotMat.compute_euler_angles(eulerAngles)# Rotation axesxRotAxis = ix.api.GMathVec3d(1,0,0)yRotAxis = ix.api.GMathVec3d(0,1,0)zRotAxis = ix.api.GMathVec3d(0,0,1)# Individual rotation quaternions per axisxQuat = ix.api.GMathQuatd()xQuat.set_rotation(eulerAngles, xRotAxis)yQuat = ix.api.GMathQuatd()yQuat.set_rotation(eulerAngles, yRotAxis)zQuat = ix.api.GMathQuatd()zQuat.set_rotation(eulerAngles, zRotAxis)# Compute the final quaternion by *multiplying* in the same rotation orderrotQuat = zQuat * xQuat * yQuat
```

We'll fix the bindings.
Anthony Nemoff
Isotropix
R&D Engineer anemoff

Posts: 500
Joined: Wed Jan 13, 2016 10:10 am

### Re: Getting quaternion from matrix

Thank you for getting back on this Anthony. When I looked into the definition of the set_rotation() method, it seems to only support 3x3 matrices, so I actually tried to create a 3x3 rotation matrix from the 4x4 extracted one as I thought that it would be worth giving it a try but didn't work either after all.

Thanks for the info about the missing bindings in the Python API and for the workaround solution which I ended using, good to know that we can do it this way too as it does work fine.

I'm using Clarisse 5 SP10 by the way.

Thanks,
Jeremy
jboissinot

Posts: 108
Joined: Tue Jan 29, 2019 10:36 pm

### Re: Getting quaternion from matrix

By the way, the APIs that you tried to use in your original post are now available in 5.0 SP11.
Cheers!
Anthony Nemoff
Isotropix
R&D Engineer anemoff

Posts: 500
Joined: Wed Jan 13, 2016 10:10 am

### Re: Getting quaternion from matrix

Thanks for the heads up regarding this Anthony. I will definitely give it a try when we update it and let you know how it goes.

Thanks,
Jeremy
jboissinot

Posts: 108
Joined: Tue Jan 29, 2019 10:36 pm