mathutils.Quaternion.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import mathutils
  2. import math
  3. # a new rotation 90 degrees about the Y axis
  4. quat_a = mathutils.Quaternion((0.7071068, 0.0, 0.7071068, 0.0))
  5. # passing values to Quaternion's directly can be confusing so axis, angle
  6. # is supported for initializing too
  7. quat_b = mathutils.Quaternion((0.0, 1.0, 0.0), math.radians(90.0))
  8. print("Check quaternions match", quat_a == quat_b)
  9. # like matrices, quaternions can be multiplied to accumulate rotational values
  10. quat_a = mathutils.Quaternion((0.0, 1.0, 0.0), math.radians(90.0))
  11. quat_b = mathutils.Quaternion((0.0, 0.0, 1.0), math.radians(45.0))
  12. quat_out = quat_a @ quat_b
  13. # print the quat, euler degrees for mere mortals and (axis, angle)
  14. print("Final Rotation:")
  15. print(quat_out)
  16. print("%.2f, %.2f, %.2f" % tuple(math.degrees(a) for a in quat_out.to_euler()))
  17. print("(%.2f, %.2f, %.2f), %.2f" % (quat_out.axis[:] +
  18. (math.degrees(quat_out.angle), )))
  19. # multiple rotations can be interpolated using the exponential map
  20. quat_c = mathutils.Quaternion((1.0, 0.0, 0.0), math.radians(15.0))
  21. exp_avg = (quat_a.to_exponential_map() +
  22. quat_b.to_exponential_map() +
  23. quat_c.to_exponential_map()) / 3.0
  24. quat_avg = mathutils.Quaternion(exp_avg)
  25. print("Average rotation:")
  26. print(quat_avg)