How to calculate the median of a rotation?

Hello there.

I’m writing a script that gets the 5 last orientations of object X and applies the median of it on object Y (one axis only).

So, basically it sums the 5 last Y orientations and divide it by 5 and apply on object Y, it works very well, however, the problems begin when the X object orientation goes from (Euler) -180 to +180 or vice-versa.

The Y object gets the median of these two values and it rotates rapidly on the wrong direction. Instead of rotating a little smoothly.

The problem is… MathUtils doesn’t seems to calculate correctly convert to Matrix values that are out of the default range (Euler +/-180, Quat +/-1.0).

Thanks.