Import Character Animation with Python

I’ve created animation importers for the Autodesk products, but I wanted to do the same with Blender.
This is how it works in 3ds Max:


qValue = quat -qx -qz -qy qw
            
t = getnodebyname BN  -- get bone
            
animate on (
     at time globalIdx in coordsys parent t.rotation = qValue
)

This is Maya:


qTemp = qValue.asEulerRotation()
            
rotX = math.degrees(qTemp.x)
rotY = math.degrees(qTemp.y)
rotZ = math.degrees(qTemp.z)
            
cmds.select("%s"%BN)
            
cmds.setAttr ("%s.jointOrientX"%BN, rotX, k=True)
cmds.setAttr ("%s.jointOrientY"%BN, rotY, k=True)
cmds.setAttr ("%s.jointOrientZ"%BN, rotZ, k=True)
            
cmds.setKeyframe(BN, t=idx)

And this is what I’m doing in Blender:


qValue = mu.Quaternion((qw,-qx,-qz,-qy))
                    
bpy.context.scene.frame_set(idx)
                    
# BN == bpy.data.objects["Armature"].pose.bones.data.bones[baneName]
BN.rotation_mode = 'QUATERNION'
BN.rotation_quaternion = qValue
                    
bpy.ops.anim.keyframe_insert(type='Rotation', confirm_success=True) 


I’ve looked at several scripts that import animation and tried dozens of different things, but all I end up with is a steaming pile of poo in Blender.

Have a look here:

You might need to convert the transformations to the proper space, there are several threads and questions about it here and on stackexchange.