Issues with quaternion to Euler conversion

I’m trying to re-invent the wheel here a little bit and write a program that converts quaternions into Euler angles, partially as an attempt to better understand quaternions but for other various reasons as well.

However, I’ve run into a problem. In Blender, no matter what axis I use, if I input values beyond a certain point the Euler angles Blender produces differ from my program, the results from the equation on Wikipedia and, as well as MMD(for what it’s worth).

Basically, the issue I have in more concrete form is this:
An angle of 135 in Blender will produce some variant of 0,0,(±)135(any order) for every axis.
An angle of 135 in any other converter will produce some variant of (-+)180,(±)180,(±45) for one axis, and some variant of 0,0,135 for the other two.

It’s not just this one angle, either. The problem seems to bleed over into other angles as well. It appears that the angle produced is always(or generally, I haven’t tested that much) a rotational equivalent, but if the numbers I produce in my program don’t match Blender’s, then when it comes to doing operations like multiplication and division on the angles this difference will become a problem.

I’ve been at this for the past day or two but I just can’t figure out what’s causing this discrepancy. I cannot produce a quaternion converter that always produces a variant of 0,0,135 when rotated 135 degrees from neutral; one axis always results in 180,180,45. I’m not saying Blender is wrong or anything like that, but I suspect there is something going on here that I’m not going to be able to find the answer for short of possibly looking at the source code for the quaternion object in Blender.

Can anyone give me an idea of what’s going on or help? I honestly don’t understand the math that well, so I’m about at the end of my rope as far as debugging this.

The resources I’ve used to develop my code are mostly:

I’d post the source code but it’s a little jumbled right now and all I’ve done is taken the equations on and plugged them into Python with some swizzling in attempt to account for the equations being designed for a different rotation order(I would suspect this is the source of my problem but my converter seems to match some programs, just not Blender).

Hi, maybe I understand badly, but in your message, it seems that you implicitly assume that there is a perfect correspondance between quaternion, rotations and Euler Angles. Such a correspondance does not exist. There are two possible quaternions for each rotation. In particular, if you start with an axis and angle and take a quaternion to represent it, there are two choices, and it can be proven that it not possible to take a uniform continuous formula. There are some points where you jump from one formula to an other.

In contrast, there is a nice formula from quaternions to (matrix of) rotations.

I am less familiar with Euler Angles, but I know there are some issues too due to maps which are not local diffeomorphisms. Thus these are still other problems.

It’s difficult to say more from what you wrote because this is confusing to me. You speak about input values beyond a certain limit. But input values according to the beginning of your message should be quaternions representing rotations, thus vectors of norm 1 with 4 coordinates. I don’t understand how a vector of norm 1 could go beyond a limit…

Blender is using a certain angle/axis sequence. I believe you can set the sequence in blender. Then use the same sequence in your code. There are twelve possible sequences and this could cause your mismatch.

In addition to your problems, Euler angles are not unique. A given rotation in 3D space has a number of Euler rotation that could result in that position. You have to use the same assumptions that Blender is using if you want the same results. Your best bet for that is to look at the Blender source code.

As an alternative, you can write some code to see if two Euler Rotations are equivalent then test to see if your results are equivalent to Blender’s results.