[Bf-extensions-cvs] [bd4f0a0c] master: sun_position: fix rotation order
Damien Picard
noreply at git.blender.org
Wed Dec 4 01:42:33 CET 2019
Commit: bd4f0a0c1f3094e0ea6ba4577059bc03288cae7a
Author: Damien Picard
Date: Tue Dec 3 12:42:04 2019 +0100
Branches: master
https://developer.blender.org/rBACbd4f0a0c1f3094e0ea6ba4577059bc03288cae7a
sun_position: fix rotation order
The sun object is now correctly rotated even when selecting
quaternion, axis angle, or euler other than XYZ as rotation mode.
===================================================================
M sun_position/sun_calc.py
===================================================================
diff --git a/sun_position/sun_calc.py b/sun_position/sun_calc.py
index 8f217b89..a933c33a 100644
--- a/sun_position/sun_calc.py
+++ b/sun_position/sun_calc.py
@@ -18,6 +18,7 @@
import bpy
from bpy.app.handlers import persistent
+from mathutils import Euler
import math
from math import degrees, radians, pi
import datetime
@@ -99,7 +100,8 @@ def sun_handler(scene):
############################################################################
#
# move_sun() will cycle through all the selected objects
-# and call set_sun_position to place them in the sky.
+# and call set_sun_position and set_sun_rotations
+# to place them in the sky.
#
############################################################################
@@ -127,12 +129,12 @@ def move_sun(context):
sun.theta = math.pi / 2 - sun_props.hdr_elevation
sun.phi = -sun_props.hdr_azimuth
- locX = math.sin(sun.phi) * math.sin(-sun.theta) * sun_props.sun_distance
- locY = math.sin(sun.theta) * math.cos(sun.phi) * sun_props.sun_distance
- locZ = math.cos(sun.theta) * sun_props.sun_distance
- sun_props.sun_object.location = locX, locY, locZ
- sun_props.sun_object.rotation_euler = (sun_props.hdr_elevation - pi/2,
- 0, -sun_props.hdr_azimuth)
+ obj = sun_props.sun_object
+ set_sun_position(obj, sun_props.sun_distance)
+ rotation_euler = Euler((sun_props.hdr_elevation - pi/2,
+ 0, -sun_props.hdr_azimuth))
+
+ set_sun_rotations(obj, rotation_euler)
return
local_time = sun_props.time
@@ -170,10 +172,9 @@ def move_sun(context):
and sun_props.sun_object.name in context.view_layer.objects):
obj = sun_props.sun_object
set_sun_position(obj, sun_props.sun_distance)
- if obj.type == 'LIGHT':
- obj.rotation_euler = (
- (math.radians(sun.elevation - 90), 0,
- math.radians(-sun.az_north)))
+ rotation_euler = Euler((math.radians(sun.elevation - 90), 0,
+ math.radians(-sun.az_north)))
+ set_sun_rotations(obj, rotation_euler)
# Sun collection
if (addon_prefs.show_object_collection
@@ -413,6 +414,20 @@ def set_sun_position(obj, distance):
obj.location = locX, locY, locZ
+def set_sun_rotations(obj, rotation_euler):
+ rotation_quaternion = rotation_euler.to_quaternion()
+ obj.rotation_quaternion = rotation_quaternion
+
+ if obj.rotation_mode in {'XZY', 'YXZ', 'YZX', 'ZXY','ZYX'}:
+ obj.rotation_euler = rotation_quaternion.to_euler(obj.rotation_mode)
+ else:
+ obj.rotation_euler = rotation_euler
+
+ rotation_axis_angle = obj.rotation_quaternion.to_axis_angle()
+ obj.rotation_axis_angle = (rotation_axis_angle[1],
+ *rotation_axis_angle[0])
+
+
def calc_sunrise_set_UTC(rise, jd, latitude, longitude):
t = calc_time_julian_cent(jd)
eq_time = calc_equation_of_time(t)
More information about the Bf-extensions-cvs
mailing list