I think this is very good question.I will answer them for those who will be looking for answers in the future.
- If you are using IMU or AHRS modeule it intrinsic rotation in ZYX (Yaw, Pitch, Roll). It mean that at first you rotate Z axis (local/global), then Y (local) and eventually X (local).
- The angles in the blender refer to extrinsic rotation.
for example in XYZ convention at first you rotate Z axis (global), then Y (Global) and eventually X (Global).
To get real Yaw Pitch Roll you should use convention from intrinsic rotation to extrinsic rotation https://en.wikipedia.org/wiki/Euler_angles#Definition_by_extrinsic_rotations.
You can see that example rotation A) and B) gives the same result
- intrinsic rotation,local axis (IMU Yaw Pitch Roll)
Local Z(90) ->Local Y(45)->Local X(30)
- extrinsic_rotations, global axis
Global X(30) ->Global Y(45) ->Global Z(90)
Real answer is
import numpy as np
matrix=bpy.data.objects['Test orientation'].matrix_world
e=np.degrees(np.array(matrix.to_euler('XYZ')[0:3]))
print('Yaw %.2f Pitch %.2f Roll %.2f'%(e[2],e[1],e[0]))
You should notice the e[2],e[1] reverse order.