problem with sharp transitions between rotation keyframes

Maybe this question was asked in some other post, and if so feel free to direct me to it. But, help please :( .

I have an animation project consisting of an object which is orbited (in a curvaceous way) by a first camera on a track. the track goes all around the object, as seen below (though it looks like a closed loop, it's actually a bezier curve that has a tiny opening). The first camera has a damped track constraint to an empty in the object. It also has a locked track constraint to an empty on another, nearly identical track above this track. The empty on the above track has almost the exact same movement and speed as the first camera. It is meant to keep the first camera pointing...I guess, up. (If the two tracks are identically aligned, it can be problematic at a couple points in the animation, triggering the camera to "snap rotate", but I'll treat this as another matter.)

Another camera copies the transforms of the first camera on the first track, and this "copy camera" is used by a Python script to insert VisualLocRot keyframes for the duration of the animation.

The script that uses the copy camera, before it finishes inserting each keyframe, does a "camera to view selected" on the subject being orbited, which fits the entire object to the vertical or horizontal edges of the camera view.

Problem is, I'm getting what I call "rotation artifacts", pictured below, in the F-curves. Just by looking at the selected keyframes, I can kind of tell what's happening, but I don't understand why Blender is doing it that way. (Why can't the curves keep going up or down, without breaking like that?) I don't know how to fix.

I hope to soon grasp all the algebra and trig I need to understand this problem better. Are quaternions my magical unicorn solution? Please let me know if I've done a bad job of explaining this, or if you need more info.

• Sorry about the title. I forgot to fix that. – nameless_nameseeker Jan 25 '16 at 5:34
• you can edit your question any time (or any post for that matter), just use the "edit" link below the question. – David Jan 25 '16 at 5:59
• You might have tripped on "Gimbal Lock" problem with Euler. And yes Quaternions method let you avoid the "Gimbal Lock" issue. There is a very clear video about gimbal lock issue here ... youtube.com/… – hawkenfox Jan 25 '16 at 8:41

The Gimbal Lock ... maybe?

You might have tripped on "Gimbal Lock" problem with Euler. And yes Quaternions method let you avoid the "Gimbal Lock" issue. There is a very clear video about gimbal lock issue here ...

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.