Commit 7b0831a7 authored by meta-androcto's avatar meta-androcto
Browse files

sun position: base update

parent e99bfab9
......@@ -32,19 +32,17 @@
# <pep8 compliant>
bl_info = {
"name": "Sun Position",
"author": "Michael Martin",
"name": "Sun Position 2.8",
"author": "Michael Martin, Kevan Cress",
"version": (3, 0, 1),
"blender": (2, 65, 0),
"api": 53207,
"blender": (2, 80, 0),
"location": "World > Sun Position",
"description": "Show sun position with objects and/or sky texture",
"warning": "",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
"Scripts/3D_interaction/Sun_Position",
"tracker_url": "https://projects.blender.org/tracker/"
"index.php?func=detail&aid=29714",
"category": "Lighting"}
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/" \
"Scripts/3D_interaction/Sun_Position",
"tracker_url": "https://projects.blender.org/tracker/" \
"index.php?func=detail&aid=29714",
"category": "3D View"} # "Lighting"} ?
import bpy
from . properties import *
......@@ -54,48 +52,42 @@ from . hdr import SunPos_HdrHelp
############################################################################
classes = (
SunPos_OT_Controller,
SunPos_OT_Preferences,
SunPos_OT_PreferencesDone,
SunPos_OT_DayRange,
SunPos_OT_SetObjectGroup,
SunPos_OT_ClearObjectGroup,
SunPos_OT_TimePlace,
SunPos_OT_Map,
SunPos_OT_Hdr,
SPOS_PT_Panel,
SunPos_OT_MapChoice,
SunPos_Help,
SunPos_HdrHelp,
)
def register():
bpy.utils.register_class(SunPosSettings)
bpy.types.Scene.SunPos_property = (
bpy.props.PointerProperty(type=SunPosSettings,
name="Sun Position",
description="Sun Position Settings"))
name="Sun Position",
description="Sun Position Settings"))
bpy.utils.register_class(SunPosPreferences)
bpy.types.Scene.SunPos_pref_property = (
bpy.props.PointerProperty(type=SunPosPreferences,
name="Sun Position Preferences",
description="SP Preferences"))
name="Sun Position Preferences",
description="SP Preferences"))
bpy.utils.register_class(SunPos_OT_Controller)
bpy.utils.register_class(SunPos_OT_Preferences)
bpy.utils.register_class(SunPos_OT_PreferencesDone)
bpy.utils.register_class(SunPos_OT_DayRange)
bpy.utils.register_class(SunPos_OT_SetObjectGroup)
bpy.utils.register_class(SunPos_OT_ClearObjectGroup)
bpy.utils.register_class(SunPos_OT_TimePlace)
bpy.utils.register_class(SunPos_OT_Map)
bpy.utils.register_class(SunPos_OT_Hdr)
bpy.utils.register_class(SunPos_Panel)
bpy.utils.register_class(SunPos_OT_MapChoice)
bpy.utils.register_class(SunPos_Help)
bpy.utils.register_class(SunPos_HdrHelp)
for c in classes:
bpy.utils.register_class(c)
def unregister():
bpy.utils.unregister_class(SunPos_HdrHelp)
bpy.utils.unregister_class(SunPos_Help)
bpy.utils.unregister_class(SunPos_OT_MapChoice)
bpy.utils.unregister_class(SunPos_Panel)
bpy.utils.unregister_class(SunPos_OT_Hdr)
bpy.utils.unregister_class(SunPos_OT_Map)
bpy.utils.unregister_class(SunPos_OT_TimePlace)
bpy.utils.unregister_class(SunPos_OT_ClearObjectGroup)
bpy.utils.unregister_class(SunPos_OT_SetObjectGroup)
bpy.utils.unregister_class(SunPos_OT_DayRange)
bpy.utils.unregister_class(SunPos_OT_PreferencesDone)
bpy.utils.unregister_class(SunPos_OT_Preferences)
bpy.utils.unregister_class(SunPos_OT_Controller)
for c in reversed(classes):
bpy.utils.unregister_class(c)
del bpy.types.Scene.SunPos_pref_property
bpy.utils.unregister_class(SunPosPreferences)
del bpy.types.Scene.SunPos_property
......
......@@ -192,8 +192,8 @@ class HdrClass:
self.set_view3d_area(self.view3d_area)
self.start = True
self.handler1 = bpy.types.SpaceView3D.draw_handler_add(
Hdr_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
Hdr_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
self.isActive = True
return True
else:
......@@ -201,7 +201,7 @@ class HdrClass:
def activateBGLcallback(self, context):
self.handler2 = bpy.types.SpaceView3D.draw_handler_add(
Draw_hdr_callback, (self, context), 'WINDOW', 'POST_PIXEL')
Draw_hdr_callback, (self, context), 'WINDOW', 'POST_PIXEL')
self.view3d_area = context.area
self.set_view3d_area(self.view3d_area)
bpy.ops.sunpos.hdr('INVOKE_DEFAULT')
......@@ -222,9 +222,9 @@ class HdrClass:
self.glImage = None
self.image.bindcode = 0
self.isActive = False
if Sun.SP:
Sun.SP.ShowHdr = False
Sun.SP.BindToSun = True
#if Sun.SP: # why removed?
Sun.SP.ShowHdr = False # indent?
Sun.SP.BindToSun = True # indent?
Sun.BindToSun = False
Display.refresh()
......@@ -465,11 +465,11 @@ def hdr_G(event):
############################################################################
Key_function = dict([('LEFT_CTRL', key_Ctrl), ('LEFT_ALT', key_Alt),
('RIGHT_CTRL', key_Ctrl), ('RIGHT_ALT', key_Alt),
('MIDDLEMOUSE', key_MiddleMouse),
('LEFTMOUSE', key_LeftMouse),
('RIGHTMOUSE', key_Esc), ('ESC', key_Esc),
('G', key_G), ('H', key_H), ('F1', key_H)])
('RIGHT_CTRL', key_Ctrl), ('RIGHT_ALT', key_Alt),
('MIDDLEMOUSE', key_MiddleMouse),
('LEFTMOUSE', key_LeftMouse),
('RIGHTMOUSE', key_Esc), ('ESC', key_Esc),
('G', key_G), ('H', key_H), ('F1', key_H)])
# ---------------------------------------------------------------------------
......@@ -918,11 +918,11 @@ class SunPos_HdrHelp(bpy.types.Operator):
colL = split.column()
colR = split.column()
colL.label(text="Esc or Right Mouse ")
colR.label("Close map or text.")
colR.label(text="Close map or text.")
colL.label(text="Left Mouse")
colR.label(text="Move crosshair.")
colL.label(text="G or MiddleMouse")
colR.label("Pan mode. Grab and move map or text.")
colR.label(text="Pan mode. Grab and move map or text.")
colL.label(text="Ctrl Middlemouse")
colR.label(text="Mouse zoom to point.")
self.layout.label("--- The following are changed by moving " +
......
......@@ -199,12 +199,12 @@ class MapClass:
self.start = True
if self.mapLocation == 'PANEL':
self.handler1 = bpy.types.SpaceProperties.draw_handler_add(
Map_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
Map_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
else:
self.handler1 = bpy.types.SpaceView3D.draw_handler_add(
Map_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
Map_load_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
self.isActive = True
return True
else:
......@@ -213,12 +213,12 @@ class MapClass:
def activateBGLcallback(self, context):
if self.mapLocation == 'PANEL':
self.handler2 = bpy.types.SpaceProperties.draw_handler_add(
Draw_map_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
Draw_map_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
else:
self.handler2 = bpy.types.SpaceView3D.draw_handler_add(
Draw_map_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
Draw_map_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
self.view3d_area = context.area
self.set_view3d_area(self.view3d_area)
bpy.ops.sunpos.map('INVOKE_DEFAULT')
......@@ -239,8 +239,8 @@ class MapClass:
self.glImage = None
self.image.bindcode = 0
self.isActive = False
if Sun.SP:
Sun.SP.ShowMap = False
#if Sun.SP: # why removed?
Sun.SP.ShowMap = False # indent?
def load_blender_image(self, file_name):
if file_name == "None":
......@@ -249,23 +249,23 @@ class MapClass:
return True
else:
self.textureless = False
# S.L. fix to use any relative path
dir_path = os.path.dirname(os.path.realpath(__file__))
self.image.name = dir_path + os.path.sep + file_name
if os.path.exists(self.image.name):
try:
self.glImage = bpy.data.images.load(self.image.name)
if self.glImage is not None:
self.image.loaded = True
self.glImage.user_clear()
self.object[0].heightFactor = \
self.glImage.size[1] / self.glImage.size[0]
return True
else:
return False
except:
pass
for path in sys.path:
if path.endswith("addons"):
fn = path + "\\sun_position\\" + file_name
self.image.name = fn.replace("\\", "/")
if os.path.exists(self.image.name):
try:
self.glImage = bpy.data.images.load(self.image.name)
if self.glImage is not None:
self.image.loaded = True
self.glImage.user_clear()
self.object[0].heightFactor = \
self.glImage.size[1] / self.glImage.size[0]
return True
else:
return False
except:
pass
return False
def load_gl_image(self):
......@@ -365,7 +365,7 @@ class MapClass:
if val:
return val
elif event.type in ('RIGHTMOUSE', 'ESC',
'T', 'D', 'X', 'Y', 'R', 'S', 'E', 'A', 'O', 'C', 'H', 'F1'):
'T', 'D', 'X', 'Y', 'R', 'S', 'E', 'A', 'O', 'C', 'H', 'F1'):
Display.refresh()
return Key_function[event.type](event)
......@@ -680,15 +680,15 @@ def map_Y(event):
############################################################################
Key_function = dict([('LEFT_CTRL', key_Ctrl), ('LEFT_ALT', key_Alt),
('RIGHT_CTRL', key_Ctrl), ('RIGHT_ALT', key_Alt),
('MIDDLEMOUSE', key_MiddleMouse),
('LEFTMOUSE', key_LeftMouse),
('RIGHTMOUSE', key_Esc), ('ESC', key_Esc),
('A', key_A), ('E', key_E), ('G', key_G), ('C', key_C),
('H', key_H), ('F1', key_H),
('R', key_R), ('S', key_S),
('T', key_TDOXY), ('D', key_TDOXY), ('O', key_TDOXY),
('X', key_TDOXY), ('Y', key_TDOXY)])
('RIGHT_CTRL', key_Ctrl), ('RIGHT_ALT', key_Alt),
('MIDDLEMOUSE', key_MiddleMouse),
('LEFTMOUSE', key_LeftMouse),
('RIGHTMOUSE', key_Esc), ('ESC', key_Esc),
('A', key_A), ('E', key_E), ('G', key_G), ('C', key_C),
('H', key_H), ('F1', key_H),
('R', key_R), ('S', key_S),
('T', key_TDOXY), ('D', key_TDOXY), ('O', key_TDOXY),
('X', key_TDOXY), ('Y', key_TDOXY)])
# ---------------------------------------------------------------------------
......@@ -808,7 +808,7 @@ def day_change_bounds(wf):
else:
Sun.SP.Day_of_year += wf
dt = (datetime.date(Sun.SP.Year, 1, 1) +
datetime.timedelta(Sun.SP.Day_of_year - 1))
datetime.timedelta(Sun.SP.Day_of_year - 1))
Sun.SP.Day = dt.day
Sun.SP.Month = dt.month
Display.refresh()
......@@ -1248,16 +1248,16 @@ class SunPos_Help(bpy.types.Operator):
colL.label(text="Left Mouse")
colR.label(text="Move crosshair.")
colL.label(text="G or MiddleMouse")
colR.label("Pan mode. Grab and move map or text.")
colR.label(text="Pan mode. Grab and move map or text.")
colL.label(text="Ctrl Middlemouse")
colR.label(text="Mouse zoom to point.")
colL.label(text="C")
colR.label("Invert text color.")
colR.label(text="Invert text color.")
colL.label(text="R")
colR.label(text="Toggle through thickness of the radiating rise/set lines.")
colL.label(text="S")
colR.label(text="Show statistics data. Move with pan command.")
self.layout.label("----- The following are changed by moving " +
self.layout.label(text="----- The following are changed by moving " +
"the mouse or using the scroll wheel.")
self.layout.label(text="----- Use Ctrl for coarse increments or Alt for fine.")
row = self.layout.row()
......
import bpy
import bgl
import math
import gpu
from gpu_extras.batch import batch_for_shader
from mathutils import Vector
from .shader import Dashed_Shader_3D
from . properties import Sun
dashedLineShader = gpu.types.GPUShader(Dashed_Shader_3D.vertex_shader, Dashed_Shader_3D.fragment_shader)
class NorthClass:
......@@ -12,14 +16,16 @@ class NorthClass:
self.isActive = False
def refresh_screen(self):
bpy.context.scene.cursor.location.x += 0.0
#bpy.context.scene.cursor.location.x += 0.0
bpy.context.area.tag_redraw()
def activate(self, context):
if context.area.type == 'PROPERTIES':
self.handler = bpy.types.SpaceView3D.draw_handler_add(
DrawNorth_callback,
(self, context), 'WINDOW', 'POST_PIXEL')
DrawNorth_callback,
#(self, context), 'WINDOW', 'POST_PIXEL') # why changed?
(self, context), 'WINDOW', 'POST_VIEW')
self.isActive = True
self.refresh_screen()
return True
......@@ -31,8 +37,8 @@ class NorthClass:
self.handler = None
self.isActive = False
self.refresh_screen()
if Sun.SP:
Sun.SP.ShowNorth = False
#if Sun.SP: # why removed?
Sun.SP.ShowNorth = False # indent?
Sun.ShowNorth = False
......@@ -50,91 +56,32 @@ def DrawNorth_callback(self, context):
# less 90 degrees. This forces the unit circle to begin at the
# 12 O'clock instead of 3 O'clock position.
# ------------------------------------------------------------------
color = (0.2, 0.6, 1.0, 0.7)
#color = (0.2, 0.6, 1.0, 0.7)
color = (0.2, 0.6, 1.0, 1)
radius = 100
angle = -(Sun.NorthOffset - math.pi / 2)
x = math.cos(angle) * radius
y = math.sin(angle) * radius
p1, p2 = (0, 0, 0), (x, y, 0) # Start & end of needle
#p1, p2 = (0, 0, 0), (x, y, 0) # Start & end of needle
# much removed / changed from original below
bgl.glEnable(bgl.GL_MULTISAMPLE)
bgl.glEnable(bgl.GL_LINE_SMOOTH)
bgl.glEnable(bgl.GL_BLEND)
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# Thanks to Buerbaum Martin for the following which draws openGL
# lines. ( From his script space_view3d_panel_measure.py )
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
# ------------------------------------------------------------------
# Convert the Perspective Matrix of the current view/region.
# ------------------------------------------------------------------
view3d = bpy.context
region = view3d.region_data
perspMatrix = region.perspective_matrix
tempMat = [perspMatrix[j][i] for i in range(4) for j in range(4)]
perspBuff = bgl.Buffer(bgl.GL_FLOAT, 16, tempMat)
# ---------------------------------------------------------
# Store previous OpenGL settings.
# ---------------------------------------------------------
MatrixMode_prev = bgl.Buffer(bgl.GL_INT, [1])
bgl.glGetIntegerv(bgl.GL_MATRIX_MODE, MatrixMode_prev)
MatrixMode_prev = MatrixMode_prev[0]
# Store projection matrix
ProjMatrix_prev = bgl.Buffer(bgl.GL_DOUBLE, [16])
bgl.glGetFloatv(bgl.GL_PROJECTION_MATRIX, ProjMatrix_prev)
# Store Line width
lineWidth_prev = bgl.Buffer(bgl.GL_FLOAT, [1])
bgl.glGetFloatv(bgl.GL_LINE_WIDTH, lineWidth_prev)
lineWidth_prev = lineWidth_prev[0]
# Store GL_BLEND
blend_prev = bgl.Buffer(bgl.GL_BYTE, [1])
bgl.glGetFloatv(bgl.GL_BLEND, blend_prev)
blend_prev = blend_prev[0]
line_stipple_prev = bgl.Buffer(bgl.GL_BYTE, [1])
bgl.glGetFloatv(bgl.GL_LINE_STIPPLE, line_stipple_prev)
line_stipple_prev = line_stipple_prev[0]
# Store glColor4f
color_prev = bgl.Buffer(bgl.GL_FLOAT, [4])
bgl.glGetFloatv(bgl.GL_COLOR, color_prev)
# ---------------------------------------------------------
# Prepare for 3D drawing
# ---------------------------------------------------------
bgl.glLoadIdentity()
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glLoadMatrixf(perspBuff)
bgl.glEnable(bgl.GL_DEPTH_TEST)
bgl.glDepthMask(False)
bgl.glEnable(bgl.GL_BLEND)
bgl.glEnable(bgl.GL_LINE_STIPPLE)
# ------------------
# and draw the line
# ------------------
width = 2
bgl.glLineWidth(width)
bgl.glColor4f(color[0], color[1], color[2], color[3])
bgl.glBegin(bgl.GL_LINE_STRIP)
bgl.glVertex3f(p1[0], p1[1], p1[2])
bgl.glVertex3f(p2[0], p2[1], p2[2])
bgl.glEnd()
# ---------------------------------------------------------
# Restore previous OpenGL settings
# ---------------------------------------------------------
bgl.glLoadIdentity()
bgl.glMatrixMode(MatrixMode_prev)
bgl.glLoadMatrixf(ProjMatrix_prev)
bgl.glLineWidth(lineWidth_prev)
if not blend_prev:
bgl.glDisable(bgl.GL_BLEND)
if not line_stipple_prev:
bgl.glDisable(bgl.GL_LINE_STIPPLE)
bgl.glColor4f(color_prev[0],
color_prev[1],
color_prev[2],
color_prev[3])
bgl.glLineWidth(2)
p1 = (0, 0, 0)
p2 = (x/20 , y/20, 0)
coords = [p1, p2] # Start & end of needle
arclengths = [0,(Vector(p1)-Vector(p2)).length]
batch = batch_for_shader(dashedLineShader, 'LINES', {"pos": coords,"arcLength":arclengths})
dashedLineShader.bind()
dashedLineShader.uniform_float("finalColor", color)
dashedLineShader.uniform_float("u_Scale", 10)
batch.draw(dashedLineShader)
......@@ -42,7 +42,7 @@ class ControlClass:
def add_handler(self, context):
self.handler = bpy.types.SpaceView3D.draw_handler_add(self.callback,
(self, context), 'WINDOW', 'POST_PIXEL')
(self, context), 'WINDOW', 'POST_PIXEL')
def remove_handler(self):
if self.handler:
......@@ -55,15 +55,14 @@ class ControlClass:
if not Sun.UseDayMonth and sp.Day_of_year != Sun.Day_of_year:
dt = (datetime.date(sp.Year, 1, 1) +
datetime.timedelta(sp.Day_of_year - 1))
datetime.timedelta(sp.Day_of_year - 1))
Sun.Day = dt.day
Sun.Month = dt.month
Sun.Day_of_year = sp.Day_of_year
sp.Day = dt.day
sp.Month = dt.month
rv = True
elif (sp.Day != Sun.Day or
sp.Month != Sun.Month):
elif (sp.Day != Sun.Day or sp.Month != Sun.Month):
try:
dt = datetime.date(sp.Year, sp.Month, sp.Day)
sp.Day_of_year = dt.timetuple().tm_yday
......@@ -84,7 +83,7 @@ class ControlClass:
if envTex.type == "TEX_ENVIRONMENT":
Sun.Bind.tex_location = envTex.texture_mapping.rotation
Sun.Bind.azStart = sp.HDR_azimuth
obj = bpy.context.scene.objects.get(Sun.SunObject)
obj = bpy.context.view_layer.objects.get(Sun.SunObject)
Sun.HDR_texture = sp.HDR_texture
Sun.Elevation = sp.HDR_elevation
Sun.Azimuth = sp.HDR_azimuth
......@@ -158,10 +157,10 @@ class SunPos_OT_Controller(bpy.types.Operator):
Stop_all_handlers()
Controller.remove_handler()
Display.setAction('ENABLE')
try:
Sun.SP.IsActive = False
except:
pass
#try:
Sun.SP.IsActive = False # indent?
#except:
# pass
def modal(self, context, event):
......@@ -272,16 +271,16 @@ def Cleanup_callback(self, context):
def Cleanup_objects():
try:
if Sun.SP:
Sun.SP.UseObjectGroup = False
#if Sun.SP: # why removed?
Sun.SP.UseObjectGroup = False # indent?
Sun.UseObjectGroup = False
except:
pass
del Sun.Selected_objects[:]
del Sun.Selected_names[:]
Display.setAction('ENABLE')
if Sun.SP:
Sun.SP.IsActive = False
#if Sun.SP: # why removed?
Sun.SP.IsActive = False # indent?
def Stop_all_handlers():
......
......@@ -30,7 +30,8 @@ class DisplayAction:
def refresh(self):
# Touching the cursor forces a screen refresh
bpy.context.scene.cursor.location.x += 0.0
#bpy.context.scene.cursor.location.x += 0.0
bpy.context.area.tag_redraw()
Display = DisplayAction()
......@@ -65,7 +66,7 @@ class SunClass:
BindToSun = False
PlaceLabel = "Time & Place"
PanelLabel = "Panel Location"
MapName = "World.jpg"
MapName = "WorldMapLR.jpg"
MapLocation = 'VIEWPORT'
Latitude = 0.0
......@@ -226,7 +227,7 @@ class SunPosSettings(PropertyGroup):
soft_min=1, soft_max=3000.00, step=10.00, default=50.00)
UseSunObject: BoolProperty(
description="Enable sun positioning of named lamp or mesh",
description="Enable sun positioning of named light or mesh",
default=False)
SunObject: StringProperty(
......
......@@ -59,7 +59,7 @@ def format_lat_long(latLong, isLatitude):
############################################################################
#
# PlaceSun() will cycle through all the selected objects of type LAMP or
# PlaceSun() will cycle through all the selected objects of type LIGHT or
# MESH and call setSunPosition to place them in the sky.
#
############################################################################
......@@ -89,14 +89,16 @@ def Move_sun():
envTex.texture_mapping.rotation.z += taz
Sun.Bind.azStart = az
obj = bpy.context.scene.objects.get(Sun.SunObject)
string = Sun.SunObject
newString = string[3:1000]
obj = bpy.context.view_layer.objects.get(newString)
try:
obj.location = locX, locY, locZ
except:
pass
if obj.type == 'LAMP':
if obj.type == 'LIGHT':
obj.rotation_euler = (
(math.radians(Sun.Elevation - 90), 0, -Sun.Azimuth))
except:
......@@ -106,7 +108,7 @@ def Move_sun():
totalObjects = len(Sun.Selected_objects)
localTime = Sun.Time
if Sun.Longitude > 0:
if Sun.Longitude > 0.0:
zone = Sun.UTCzone * -1
else:
zone = Sun.UTCzone
......@@ -120,8 +122,8 @@ def Move_sun():