More Quaternions: An Unintended Inversion

I recently started a thread about reversing the direction of rotation when lerping between quaternions, as a result of my lerp strangely rotating in the direction opposite to that which I expected.

I think that I’ve discovered why that was happening: one of my changes in orientation in building up the orientations between which I was lerping was resulting in an orientation that, while visually correct, had the signs of all elements of the quaternion reversed.

Here’s an example:

# In my initialisation:
        self.shoulder = root.attachNewNode(PandaNode("shoulder"))
        self.upperArm = self.shoulder.attachNewNode(PandaNode("upper arm"))
        # "root" should initially be "render", but this is later changed to be
        # my character model, at which point "self.shoulder" is reparented and
        # its position and HPR set to (0, 0, 0)

# The code in question:
        shoulderParent = self.shoulder.getParent()

        # ... Other code ...

        print self.upperArm.getQuat(shoulderParent)
        self.upperArm.setQuat(shoulderParent, self.upperArm.getQuat(shoulderParent))
        print self.upperArm.getQuat(shoulderParent)

Example printout:

0.155711 + 0.0802894i + 0.948326j + -0.264547k
-0.155711 + -0.0802892i + -0.948326j + 0.264547k

Note that, despite the fact that I’m setting the object’s quaterion to what should be the same quaternion, I end up with one that has its elements inverted.

(I’m only setting the quaternion to itself in the example above; my intended code uses that to apply a change to the orientation relative to “shoulderParent”, like so: “self.upperArm.setQuat(shoulderParent, self.upperArm.getQuat(shoulderParent)*offsetQuat)”.)

Does anyone know why this is, and what I might do about it? While I had managed to get around the issue described in the previous thread, it seems to be causing problems in other parts of the code.