map degrees to 0 - 360 in python

Christoph Müller picture Christoph Müller · Aug 5, 2015 · Viewed 10.8k times · Source

is it possible to map a value of rotation to be inside the range of 0-360 degrees?

For example:

  • a angle of -10° should be 350°
  • a angle of 760° should be 40° degrees?

Is there an easy solution? Maybe in mathutils?

best, chris

Answer

juanchopanza picture juanchopanza · Aug 5, 2015

You can use the modulo operator % for this:

>>> print -10 % 360
350
>>> print 760 % 360
40