3
$\begingroup$

Currently I have add-on that gets rotations from an object, converts angles into a matrix and saves it in a particular format into a text file.

This is what code for that looks like:

rot = tuple(chain(*obj.matrix_world.to_3x3().normalized().transposed()))

dump += ['"rotation" "{}"'.format(" ".join(str(r) for r in rot)),]

self.write(self.filepath, "\n".join(dump))

I also need to have Euler angles written after "rotation" in the following format:

"angles" "Y Z X"

Where Y, Z and X are angles in degrees.

How would I do that, following analogy with rotation matrix ? Thanks.

$\endgroup$

2 Answers 2

6
$\begingroup$

Should be as simple as:

from math import degrees

"{:f} {:f} {:f}".format(*map(degrees, obj.matrix_world.to_euler()))
# '-90.224203 -10.379210 -11.433244'

to_euler() converts a matrix to an Euler object, with order XYZ (regardless of what is set for rotation_mode). The angles are given in radians, but they can be simply converted to degrees using math.degrees().

$\endgroup$
2
  • $\begingroup$ Thanks! I got this code writing angles now: '"angles" "{:f} {:f} {:f}"'.format(*map(degrees, obj.matrix_world.to_euler())), however, the order is wrong. How do I re-arrange X Y Z to be Y Z X ? $\endgroup$
    – motorsep
    Aug 2, 2015 at 16:45
  • $\begingroup$ e = tuple(map(degrees, obj.matrix_world.to_euler())) and then "{:f} {:f} {:f}".format(e[1], e[2], e[0]) $\endgroup$
    – CodeManX
    Aug 2, 2015 at 19:11
3
$\begingroup$

So this is what worked for me (for test cases I tried anyway; with help from some folks on IRC):

rot_angles = obj.matrix_world.to_euler()
rot_angles_X = math.degrees(rot_angles[0])
rot_angles_Y = math.degrees(rot_angles[1])
rot_angles_Z = math.degrees(rot_angles[2])

'"angles" "{}"'.format(" ".join( [ str(rot_angles_Y), str(rot_angles_Z), str(rot_angles_X) ] ))

Thanks!

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .