I suspect you are having the same problem I had in How do I ensure a sequence of quaternions from matrix.decompose() is continuous?How do I ensure a sequence of quaternions from matrix.decompose() is continuous? .
It boils down to "q and -q both represent the same orientation". Since you mention a "script", maybe you can modify your script to use the same logic as my QuaternionStabilizer
class to figure out whether to keyframe q
or -q
.