# LScript versions of EulerToQuaternion and QuaternionToEuler

#### vncnt

##### Well-known member
My LScript version of these functions seem to work fine (until now) in Legato.
I needed them in the Lock-2-World variation of the HoldPose function in Legato.

Procedure: select a frame range or a frame, use Lock-2-World and presto: the item will stop moving/rotating in that frame range or until after the current frame.
A Lock-2-Item is on its way: temporary parenting an item to another item without rebuilding the rig!

You might need these functions if you want to add/subtract rotations on multiple axis at once without gimbal lock.

Code:
``````EulerToQuaternion: heading,pitch,bank
{
// SOURCE: http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToQuaternion/index.htm

c2 = cos(pitch/2);
s2 = sin(pitch/2);
c3 = cos(bank/2);
s3 = sin(bank/2);

c1c2 = c1*c2;
s1s2 = s1*s2;

Qw = c1c2*c3 - s1s2*s3;
Qx = c1c2*s3 + s1s2*c3;
Qy = s1*c2*c3 + c1*s2*s3;
Qz = c1*s2*c3 - s1*c2*s3;

return(Qw,Qx,Qy,Qz);
}

QuaternionToEuler: Qw,Qx,Qy,Qz
{
// SOURCE: http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm

test = Qx*Qy + Qz*Qw;

if(test > 0.499) {

// FOUND SINGULARITY AT NORTH POLE
pitch = PI/2;
bank = 0;
}

if(test < -0.499) {

// FOUND SINGULARITY AT SOUTH POLE
pitch = - PI/2;
bank = 0;