-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
reload.py
305 lines (202 loc) · 7.2 KB
/
reload.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import bpy
import os
import re
from bpy.app.handlers import persistent
from .addon_prefs import get_addon_preferences
# TODO Reload sounds waveform
# TODO Cache files sequence ?
# TODO Add reload for images in sequence strip
object_types = [
"images",
"movieclips",
"sounds",
"libraries",
"cache_files",
]
def get_filesequence_from_file(filepath):
seq_files = []
folderpath, filename_ext = os.path.split(filepath)
filename, ext = os.path.splitext(filename_ext)
str_nb = re.search(r'\d+$', filename)
if str_nb is None:
return seq_files
str_nb = str_nb.group()
pattern = filename.replace(str_nb, "")
for file in os.listdir(folderpath):
if pattern in file and ext in file:
seq_files.append(os.path.join(folderpath, file))
if get_addon_preferences().debug:
print(
"AUTORELOAD --- File sequence found for "
f"{filepath} : {len(seq_files)} files"
)
return seq_files
def get_image_size(image, filepath):
new_size = 0
# UDIM
if image.source == "TILED":
tile_list = []
for tile in image.tiles:
tile_list.append(
filepath.replace(
"<UDIM>",
str(tile.number),
)
)
new_size = get_file_list_size(tile_list)
# Sequence
elif image.source == "SEQUENCE":
new_size = get_file_list_size(
get_filesequence_from_file(filepath)
)
# Single file
else:
# Invalid filepath
if not os.path.isfile(filepath):
return new_size
new_size = os.path.getsize(filepath)
return new_size
def get_file_size(object, filepath):
# Invalid filepath
if not os.path.isfile(filepath):
return 0
# Valid filepath
else:
return os.path.getsize(filepath)
def reload_file_size(new_size, object):
# Save new file_size
if new_size != object.file_size:
# Find if images previously been checked
checked = True
if object.file_size == 0:
checked = False
object.file_size = new_size
if get_addon_preferences().debug:
print(
f"AUTORELOAD --- {object.name} size - "
f"Old:{object.file_size}, New:{new_size}"
)
return checked
return False
def get_file_list_size(file_list):
size = 0
for filepath in file_list:
if os.path.isfile:
size += os.path.getsize(filepath)
return size
def get_files_size(obj_type):
obj_to_reload = []
for obj in getattr(bpy.data, obj_type):
# Avoid builtin
if obj.filepath in ["<builtin>", ""]:
continue
path = bpy.path.abspath(obj.filepath)
# Get new size
if obj_type == "images":
new_size = get_image_size(obj, path)
else:
new_size = get_file_size(obj, path)
# Reload file size
if reload_file_size(
new_size,
obj,
):
obj_to_reload.append(obj)
return obj_to_reload
@persistent
def startup_refresh_file_size(scene):
print("AUTORELOAD --- Setting autoreload categories")
props = bpy.context.window_manager.autoreload_properties
prefs = get_addon_preferences()
for obj_type in object_types:
setattr(
props,
f"autoreload_{obj_type}",
getattr(prefs, f"startup_{obj_type}"),
)
props.autoreload_run = prefs.startup_run
print("AUTORELOAD --- Reloading external files")
reload_modified_objects()
def update_3d_viewers():
if get_addon_preferences().debug:
print("AUTORELOAD --- Updating 3D Viewports")
# Check if render is compatible with live refresh
engine = bpy.context.scene.render.engine
if engine in ['BLENDER_EEVEE','BLENDER_WORKBENCH']:
return
# Iterate through viewport to update them
wm = bpy.data.window_managers['WinMan']
for window in wm.windows :
for area in window.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'
def update_sound_waveform(sound):
for scn in bpy.data.scenes:
for strip in scn.sequence_editor.sequences_all:
if strip.type == "SOUND"\
and strip.show_waveform\
and strip.sound == sound:
strip.show_waveform = True
def reload_images(image_list):
for img in image_list:
# Hack to prevent pink sequence
if img.source == "SEQUENCE":
img.source = "FILE"
img.reload()
img.source = "SEQUENCE"
else:
img.reload()
if image_list:
update_3d_viewers()
def reload_movieclips(mov_list):
for mov in mov_list:
mov.filepath = mov.filepath
def reload_sounds(sound_list):
for sound in sound_list:
sound.filepath = sound.filepath
# TODO update_sound_waveform(sound)
def reload_libraries(library_list):
for lib in library_list:
lib.reload()
def reload_cache_files(cache_list):
for cache in cache_list:
cache.filepath = cache.filepath
def reload_modified_objects():
props = bpy.context.window_manager.autoreload_properties
obj_to_reload = {}
# Get objects to reload
for obj_type in object_types:
obj_to_reload[obj_type] = []
# Check if obj type autoreloaded from property
if getattr(props, f"autoreload_{obj_type}"):
obj_to_reload[obj_type] = get_files_size(obj_type)
# Reload objects
if get_addon_preferences().debug:
print("AUTORELOAD --- Objects to reload :")
print(obj_to_reload)
reload_images(obj_to_reload["images"])
reload_movieclips(obj_to_reload["movieclips"])
reload_sounds(obj_to_reload["sounds"])
reload_libraries(obj_to_reload["libraries"])
reload_cache_files(obj_to_reload["cache_files"])
def timer_reload_files():
interval = get_addon_preferences().timer_frequency
props = bpy.context.window_manager.autoreload_properties
# Pause
if not props.autoreload_run:
return interval
if get_addon_preferences().debug:
print("AUTORELOAD --- Timer")
reload_modified_objects()
return interval
### REGISTER ---
def register():
bpy.app.handlers.load_post.append(startup_refresh_file_size)
bpy.app.timers.register(timer_reload_files, persistent=True)
def unregister():
bpy.app.handlers.load_post.remove(startup_refresh_file_size)
bpy.app.timers.unregister(timer_reload_files)