# Subtract quaternions in python?

how do you subtract two quaternions, in blender scripting?

I've tried allot of things and none of them work, quaternion1 * quaternion2.inverted() does not work inverted gives some random answer, instead of the reciprocal as I had expected

On a similar note, how do you get the inverse translation by a matrix? so Vector / quaterion @ matrix would translate it by the matrix, but again Vector @ matrix.inverted() does not give the right answer

i've been trying to get this to work for two days and i'm really frustrated by blender python right now

Rotation Difference.

Believe what you are looking for is rotation_difference ie the minimum rotation required to rotate from one quaternion to another

Python console code

Define a couple of angles using Eulers,

>>> r1 = Euler((radians(30), radians(60), radians(45)))


As quaternions

>>> q1 = r1.to_quaternion()
>>> q2 = r2.to_quaternion()


The difference

>>> qd = q1.rotation_difference(q2)

>>> list(map(degrees, qd.to_euler()))
[-30.000000834826057, 2.481181138556882e-06, -3.316504514323493e-06]


Or other way

>>> qd = q2.rotation_difference(q1)
>>> list(map(degrees, qd.to_euler()))
[30.00000424992064, -3.4388197927083468e-06, 1.2883001108333913e-06]


And using this result to get back to q1 from q2

>>> qr = q2 @ qd
>>> qr
Quaternion((0.8223630785942078, 0.022260043770074844, 0.5319756269454956, 0.20056211948394775))

>>> q1
Quaternion((0.8223631381988525, 0.022260019555687904, 0.5319756865501404, 0.20056211948394775))


difference (within precision) is pretty much Identity Quat (1, 0, 0, 0)

>>> qr.rotation_difference(q1)
Quaternion((1.0, -1.4901161193847656e-08, 3.4458935260772705e-08, -2.8870999813079834e-08))