forked from samytichadou/Auto_Reload_Blender_addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.py
108 lines (98 loc) · 3.58 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import bpy
import os
import time
from bpy.app.handlers import persistent
from .global_variables import handler
from .addon_prefs import get_addon_preferences
# absolute path
def absolute_path(relpath):
return os.path.abspath(bpy.path.abspath(relpath))
# get addon path
def get_my_dir():
""" get current file parent directory absolute path """
script = os.path.realpath(__file__)
return os.path.dirname(script)
# check libraries
def checkLibraries():
modified = []
missing = []
for item in bpy.data.libraries:
path = absolute_path(item.filepath)
try:
if item.modification_time != str(os.path.getmtime(path)):
item.modification_time = str(os.path.getmtime(path))
item.to_reload=True
modified.append(item.name)
else:
item.to_reload=False
except FileNotFoundError:
item.modification_time = "missing"
item.to_reload = True
missing.append(item.name)
return modified, missing
# reload library
def reloadLibrary(name):
lib = bpy.data.libraries[name]
lib.reload()
lib.to_reload=False
lib.modification_time = str(os.path.getmtime(absolute_path(lib.filepath)))
# reload modified datas
def reloadModifiedImages():
modified = []
missing = []
for item in bpy.data.images:
if not item.library and not item.packed_file:
path = absolute_path(item.filepath)
try:
if item.modification_time!=str(os.path.getmtime(path)):
item.reload()
item.modification_time=str(os.path.getmtime(path))
modified.append(item.name)
except FileNotFoundError:
item.modification_time="missing"
missing.append(item.name)
return modified, missing
# update 3d view if in rendered mode and not EEVEE or WORKBENCH
def update_viewers(context):
if context.scene.render.engine not in ['BLENDER_EEVEE','BLENDER_WORKBENCH']:
wman = bpy.data.window_managers['WinMan']
for win in wman.windows :
for area in win.screen.areas :
if area.type=='VIEW_3D' :
for space in area.spaces :
if space.type == 'VIEW_3D' and space.shading.type == 'RENDERED' :
space.shading.type = 'SOLID'
space.shading.type = 'RENDERED'
# check all images at startup
def checkImagesStartup():
is_missing = False
for item in bpy.data.images:
if not item.library and not item.packed_file:
try:
path=absolute_path(item.filepath)
item.modification_time=str(os.path.getmtime(path))
except FileNotFoundError:
item.modification_time="missing"
is_missing = True
return is_missing
# check all libraries at startup
def checkLibrariesStartup():
is_missing = False
for item in bpy.data.libraries:
try:
path=absolute_path(item.filepath)
item.modification_time=str(os.path.getmtime(path))
item.to_reload = False
except FileNotFoundError:
item.modification_time="missing"
is_missing = True
return is_missing
# handler
@persistent
def reload_startup(scene):
wm = bpy.data.window_managers['WinMan']
if checkImagesStartup(): wm.autoreloadMissingImages = True
else: wm.autoreloadMissingImages = False
if checkLibrariesStartup(): wm.autoreloadMissingLibraries = True
else: wm.autoreloadMissingLibraries = False
print(handler)