How to read a bone's quaternion rotation and convert from local bone to world space I am writing a custom exporter to export from blender to my opengl program.
To read the roatation, as well as convert from blender's cordinate system to my game's system, i use this code:

e = poseBone.rotation_quaternion.to_euler()
quat = Euler((e[1], e[2], e[0])).to_quaternion() #write this quat to file


Bones in blender, as demonstrated by this image, have their own local space:

I believe this difference in local bone space vs world space is the last thing causing incorrect rotations in my opengl program. How would convert bone.rotation_quaternion from local bone to world space prior to export? I have seen several similar questions, but none that helped me.
Based on some simple experiments it seems that

debone = Matrix([[1,0,0,0],[0,0,-1,0], [0,1,0,0],[0,0,0,1]]) #because bones are wacky
m = armature_object.matrix_world * poseBone.matrix * debone
o2.rotation_mode = 'QUATERNION'
o2.location, o2.rotation_quaternion, o2.scale = m.decompose()


will align o2 with poseBone (unless o2 has a parent causing its matrix_local to not be identical to matrix_world)