xm_euleroperation

Übersicht: Python & Blender


To work with angles in Blender the unit is *Radians* is used. Those who want to brush up his knowledge again, can read on Wikipedia: Radian: https://en.wikipedia.org/wiki/Radian For short the most important facts to solve our problems.


Conversion

After the formula the amount can be calculated for a degree:

1° = 2 * pi / 360

The good news are, there is no need to calculate the values like this:

>>> 2 * math.pi / 360
0.017453292519943295

>>> 10 * 2 * math.pi/360
0.17453292519943295

Use the the following formulas where you need a rotation. For your orientation some special formulas for quarter, half, three quarter and full turn.

45° pi * 1/4
90° pi * 1/2
180° pi * 1
270° pi * 3/2
360° pi * 2

Now it should no problem to rotate the cube or other objects.

Sample video and how it could look like...

Euler rotation

Because the rotation in 3 axes is a so fundamentate operation for a 3D world, there are also useful functions. One of them is the Euler object and the Vector object for managing coordinates.

A small command line protocol is showing the possibilities:

Step I: import the helper, two mathe modules
>>> import mathutils
>>> import math
Step II: create an Euler and a Vector object
>>> eul = mathutils.Euler()
>>> print(eul)
>>> vec = Vector()
>>> print(vec)

Regarding the coordinates, both objects are identical, only the Euler object manages even the order of the axes, but Standard is the order that applies to the Vector object first X, then Y and finally Z.

Step III: change the values
>>> eul[:] = 1,2,3
>>> vec = (3,4,5)
>>> print(vec, eul)
>>> print(vec.x, eul.x)
>>> print(vec.y, eul.y)
>>> print(vec.z, eul.z)
>>> x,y,z = vec
>>> print(x,y,z)
>>> x,y,z = eul
>>> print(x,y,z)
Step IV: create a monkey and rotate the object
>>> bpy.ops.mesh.primitive_monkey_add(location=(2, 2, 2))
>>> obj = bpy.context.object
>>> obj.rotation_euler = (eul.x,eul.y, eul.z)
>>> obj.rotation_euler = (vec.x, vec.y, vec.z)
>>> eul.zero() # back to the beginning
>>> obj.rotation_euler = (eul.x,eul.y, eul.z)
Step V:

degrees imstead of radians

For the rotation parts of PI or 2 * PI must be used and our test values 1,2,3,4,5,6 were arbitrary selected. For the rotation in 90 ° increments helps us the table above, intermediate values are more difficult to handle, but help we get from two functions:

rad = math.radians(180)
print(rad)
grad = math.degrees(rad)
print(grad)
Step VI:

application for rotation

Let's Suzanne look slightly upwards, 45° looking up would be too much (have a look at the table), so we want rotate the x-axis by 20° (clockwise).

rotation = Euler((math.radians(-20),0,0))
obj.rotation_euler = (rotation.x, rotation.y, rotation.z)

Any questions?

Commentaar