3
$\begingroup$

Please forgive if this is something obvious (I've only started looking at bpy today), but I don't seem to get a valid quaternion from objects, it's always coming out as (1.0, 0.0, 0.0, 0.0)

>> bpy.data.objects['Cube'].rotation_euler = (22.5,22.5,22.5)
>>> bpy.data.objects['Cube'].rotation_euler
Euler((22.5, 22.5, 22.5), 'XYZ')

>>> bpy.data.objects['Cube'].rotation_quaternion
Quaternion((1.0, 0.0, 0.0, 0.0))

Also

>> Euler( bpy.data.objects['Cube'].rotation_euler ).to_quaternion()
Quaternion((-0.8905556797981262, -0.29705408215522766, 0.17443729937076569, -0.29705408215522766))

>>> Euler( bpy.data.objects['Cube'].rotation_euler ).to_quaternion().to_euler()
Euler((0.5088514089584351, -0.5088514685630798, 0.5088514685630798), 'XYZ')

I'm missing something pretty basic?

Cheers.

$\endgroup$
4
$\begingroup$

There are two problems with what you are doing:

  1. Rotations are ambiguous:
    • For euler rotations there are the problems of gimbal lock and the order in which the rotations are applied.
    • Quaternions on the other hand will give the rotation which their corresponding normalized Quaternion would produce. This means that all scaled versions of a quaternion will result in the same rotation.
      The Blender Wiki gives a good overview about the meaning of the different rotation modes.
  2. The values of the other rotation modes are only set when you switch to them. The value you are reading from rotation_quaternion is a default value.
    To switch to another rotation mode in python set bpy.data.objects['Cube'].rotation_mode to one of the following values:
    'QUATERNION', 'XYZ', 'XZY', 'YXZ', 'YZX', 'ZXY', 'ZYX' or 'AXIS_ANGLE'

You might also see different rotation values than which you set using python when you look at the properties of the object. The values in the image below are displayed after executing bpy.data.objects['Cube'].rotation_euler = (22.5,22.5,22.5).
enter image description here

$\endgroup$
  • $\begingroup$ Thanks, looks like it's the rotation mode I was missing. I know gimbal lock, that's why I want quaternions :-) $\endgroup$ – Andy J Buchanan May 19 '14 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.