-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
489 lines (396 loc) · 19.1 KB
/
main.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# MIT License
#
# Copyright (c) [2024] [Xinyu Zhu]
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import atexit
import os
import re
import shutil
import subprocess
import threading
import time
import ast
from datetime import datetime
from pathlib import Path
from common.class_loader.module_installer import install_if_missing, install_fake_bpy, default_blender_addon_path
ACTIVE_ADDON = "lol_blender"
# The path of the blender executable. Blender2.93 is the minimum version required
BLENDER_PATH = os.environ.get("BLENDER_PATH", "C:/Program Files/Blender Foundation/Blender 4.1/blender.exe")
# The files to be ignored when release the addon
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# The default release dir. Must not within the current workspace
DEFAULT_RELEASE_DIR = os.path.join(PROJECT_ROOT, "dist/")
# The default test release dir. Must not within the current workspace
TEST_RELEASE_DIR = os.path.join(PROJECT_ROOT, ".lolblender/")
addon_namespace_pattern = re.compile("^[a-zA-Z]+[a-zA-Z0-9_]*$")
# The framework use this pattern to find the import module within the workspace
import_module_pattern = re.compile("from ([a-zA-Z_][a-zA-Z0-9_.]*) import (.+)")
__addon_md5__signature__ = "addon.txt"
_ADDON_TEMPLATE = "sample_addon"
_ADDONS_FOLDER = "addons"
ADDON_ROOT = os.path.join(PROJECT_ROOT, _ADDONS_FOLDER)
install_if_missing("watchdog")
install_fake_bpy(BLENDER_PATH)
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from common.io.FileManagerClient import read_utf8, write_utf8, get_md5_folder, is_subdirectory
from common.io.FileManagerClient import search_files
def new_addon(addon_name: str):
new_addon_path = os.path.join(ADDON_ROOT, addon_name)
if os.path.exists(new_addon_path) or not bool(addon_namespace_pattern.match(addon_name)):
raise ValueError("Invalid addon name: " + addon_name + " Please name it as a python package name")
shutil.copytree(os.path.join(ADDON_ROOT, _ADDON_TEMPLATE), new_addon_path)
all_py_file = search_files(new_addon_path, {".py"})
for py_file in all_py_file:
content = read_utf8(py_file).replace(_ADDON_TEMPLATE, addon_name)
write_utf8(py_file, content)
def test_addon(addon_name, enable_watch=True):
init_file = get_init_file_path(addon_name)
start_test(init_file, addon_name, enable_watch=enable_watch)
def get_init_file_path(addon_name):
# addon_name is the name defined in addon's config.py
target_init_file_path = os.path.join(ADDON_ROOT, addon_name, "__init__.py")
if not os.path.exists(target_init_file_path):
raise ValueError(f"Release failed: Addon {addon_name} not found.")
return target_init_file_path
# https://devtalk.blender.org/t/plugin-hot-reload-by-cleaning-sys-modules/20040
start_up_command = """
import bpy
from bpy.app.handlers import persistent
import os
import sys
existing_addon_md5 = ""
try:
bpy.ops.preferences.addon_enable(module="{addon_name}")
except Exception as e:
print("Addon enable failed:", e)
def watch_update_tick():
global existing_addon_md5
if os.path.exists("{addon_signature}"):
with open("{addon_signature}", "r") as f:
addon_md5 = f.read()
if existing_addon_md5 == "":
existing_addon_md5 = addon_md5
elif existing_addon_md5 != addon_md5:
print("Addon file changed, start to update the addon")
try:
bpy.ops.preferences.addon_disable(module="{addon_name}")
all_modules = sys.modules
all_modules = dict(sorted(all_modules.items(),key= lambda x:x[0])) #sort them
for k,v in all_modules.items():
if k.startswith("{addon_name}"):
del sys.modules[k]
bpy.ops.preferences.addon_enable(module="{addon_name}")
except Exception as e:
print("Addon update failed:", e)
existing_addon_md5 = addon_md5
print("Addon updated")
return 1.0
@persistent
def register_watch_update_tick(dummy):
print("Watching for addon update...")
bpy.app.timers.register(watch_update_tick)
register_watch_update_tick(None)
bpy.app.handlers.load_post.append(register_watch_update_tick)
"""
def start_test(init_file, addon_name, enable_watch=True):
update_addon_for_test(init_file, addon_name)
test_addon_path = os.path.join(default_blender_addon_path(BLENDER_PATH), addon_name)
if not enable_watch:
def exit_handler():
if os.path.exists(test_addon_path):
shutil.rmtree(test_addon_path)
atexit.register(exit_handler)
try:
subprocess.call(
[BLENDER_PATH, "--python-use-system-env --python-expr",
f"import bpy\nbpy.ops.preferences.addon_enable(module=\"{addon_name}\")"])
finally:
exit_handler()
return
# start_watch_for_update(init_file, addon_name)
stop_event = threading.Event()
thread = threading.Thread(target=start_watch_for_update, args=(init_file, addon_name, stop_event))
thread.start()
def exit_handler():
stop_event.set()
thread.join()
if os.path.exists(test_addon_path):
shutil.rmtree(test_addon_path)
atexit.register(exit_handler)
python_script = start_up_command.format(addon_name=addon_name,
addon_signature=os.path.join(test_addon_path,
__addon_md5__signature__).replace("\\", "/"))
try:
subprocess.call([BLENDER_PATH, "--python-expr", python_script])
finally:
exit_handler()
def release_addon(target_init_file, addon_name, with_timestamp=False, release_dir=DEFAULT_RELEASE_DIR, need_zip=True):
# # if release dir is under PROJECT_ROOT, it's not allowed
# if is_subdirectory(release_dir, PROJECT_ROOT):
# raise ValueError("Invalid release dir:", release_dir,
# "Please set a release/test dir outside the current workspace")
if not bool(addon_namespace_pattern.match(addon_name)):
raise ValueError("InValid addon_name:", addon_name, "Please name it as a python package name")
if not os.path.isdir(release_dir):
os.mkdir(release_dir)
# remove the folder if already exists
release_folder = os.path.join(release_dir, addon_name)
if os.path.exists(release_folder):
shutil.rmtree(release_folder)
os.mkdir(release_folder)
shutil.copyfile(target_init_file, os.path.join(release_folder, "__init__.py"))
# Copy the plugin folder to the publish directory
shutil.copytree(os.path.join(ADDON_ROOT, addon_name), os.path.join(release_folder, _ADDONS_FOLDER, addon_name))
shutil.copyfile(os.path.join(ADDON_ROOT, "__init__.py"), os.path.join(release_folder, _ADDONS_FOLDER, "__init__.py"))
all_py_files = search_files(os.path.join(ADDON_ROOT, addon_name), {".py"})
# Analyze each py file in the plugin folder and find other py files that each py file depends on
visited_py_files = set()
for py_file in all_py_files:
visited_py_files.add(os.path.abspath(py_file))
# Be careful not to miss the __init__.py file
visited_py_files.add(os.path.abspath(os.path.join(ADDON_ROOT, "__init__.py")))
print("Bundling dependencies...")
dependencies = find_all_dependencies(list(visited_py_files), PROJECT_ROOT)
for dependency in dependencies:
dependency = os.path.abspath(dependency)
if dependency in visited_py_files:
continue
visited_py_files.add(dependency)
target_path = os.path.join(release_folder, os.path.relpath(dependency, PROJECT_ROOT))
if not os.path.exists(os.path.dirname(target_path)):
os.makedirs(os.path.dirname(target_path))
shutil.copy(dependency, os.path.join(release_folder, os.path.relpath(dependency, PROJECT_ROOT)))
print(f"Bundled {len(visited_py_files)} dependencies.\n")
remove_pyc_files(release_folder)
removed_path = 1
while removed_path > 0:
removed_path = remove_empty_folders(release_folder)
print("Enhancing module imports...\n")
enhance_import_for_py_files(release_folder)
real_addon_name = "{addon_name}_{timestamp}".format(addon_name=release_folder,
timestamp=datetime.now().strftime(
"%Y%m%d_%H%M%S")) if with_timestamp else "{addon_name}".format(
addon_name=release_folder)
released_addon_path = os.path.abspath(os.path.join(release_dir, real_addon_name) + ".zip")
# zip the addon
if need_zip:
zip_folder(release_folder, real_addon_name)
print("Add-on zip created:", released_addon_path)
return released_addon_path
bl_info_regex = re.compile("^\s*bl_info\s*=\s*{\s*$")
bl_info_version_regex = re.compile('"version":\(\d*,\d*,\d*,?\),\s*')
bl_info_end_regex = re.compile("^\s*}\s*$")
def set_plugin_version(init_py_path: str, major: int, minor: int, patch: int):
found_version = False
lines = []
with open(init_py_path, mode="r", encoding="utf-8") as f:
in_bl_info_block = False
lines = f.readlines()
for i, line in enumerate(lines):
if in_bl_info_block:
if re.fullmatch(bl_info_end_regex, line):
break
if re.fullmatch(bl_info_version_regex, line.replace(" ", "")):
found_version = True
lines[i] = f' "version": ({major}, {minor}, {patch}),\n'
break
else:
if re.fullmatch(bl_info_regex, line):
in_bl_info_block = True
if not found_version:
raise ValueError("Invalid addon __init__.py file: Could not locate bl_info['version'].")
with open(init_py_path, mode="w", encoding="utf-8") as f:
f.writelines(lines)
# pyc files are auto generated, need to be removed before release
def remove_pyc_files(release_folder: str):
all_pyc_file = search_files(release_folder, {"pyc"})
for pyc_file in all_pyc_file:
os.remove(pyc_file)
def remove_empty_folders(root_path):
all_folder_to_remove = []
for root, dirnames, filenames in os.walk(root_path, topdown=False):
for dirname in dirnames:
dir_to_check = os.path.join(root, dirname)
if not os.listdir(dir_to_check):
all_folder_to_remove.append(dir_to_check)
for folder in all_folder_to_remove:
shutil.rmtree(folder)
return len(all_folder_to_remove)
# Zip the folder in a way that blender can recognize it as an addon.
def zip_folder(target_root, output_zip_file):
shutil.make_archive(output_zip_file, 'zip', Path(target_root).parent, base_dir=os.path.basename(target_root))
def find_imported_modules(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
root = ast.parse(file.read(), filename=file_path)
imported_modules = set()
for node in ast.walk(root):
if isinstance(node, ast.Import):
for alias in node.names:
imported_modules.add(alias.name)
elif isinstance(node, ast.ImportFrom):
if node.module:
module_name = node.module
imported_modules.add(module_name)
for alias in node.names:
if node.module:
imported_modules.add(f"{node.module}.{alias.name}")
else:
imported_modules.add(alias.name)
return imported_modules
def resolve_module_path(module_name, base_path, project_root):
if not module_name.endswith(".*"):
# Handle import all
module_path = module_name.replace('.', '/')
module_path = os.path.join(project_root, module_path)
if os.path.isdir(module_path):
module_path = os.path.join(module_path, '__init__.py')
return [module_path]
elif os.path.isfile(module_path + '.py'):
module_path = module_path + '.py'
return [module_path]
else:
current_search_dir = os.path.dirname(base_path)
while is_subdirectory(current_search_dir, project_root):
module_path = module_name.replace('.', '/')
module_path = os.path.join(current_search_dir, module_path)
if os.path.isdir(module_path):
module_path = os.path.join(module_path, '__init__.py')
return [module_path]
elif os.path.isfile(module_path + '.py'):
module_path = module_path + '.py'
return [module_path]
current_search_dir = os.path.dirname(current_search_dir)
return []
else:
module_name = module_name[:-2]
module_path = module_name.replace('.', '/')
possible_root_path = os.path.join(project_root, module_path)
if os.path.isdir(possible_root_path):
# 返回文件夹中所有py文件
return search_files(possible_root_path, {".py"})
elif os.path.isfile(module_path + '.py'):
module_path = module_path + '.py'
return [module_path]
else:
current_search_dir = os.path.dirname(base_path)
while is_subdirectory(current_search_dir, project_root):
possible_root_path = os.path.join(current_search_dir, module_path)
if os.path.isdir(module_path):
return search_files(possible_root_path, {".py"})
elif os.path.isfile(module_path + '.py'):
module_path = module_path + '.py'
return [module_path]
current_search_dir = os.path.dirname(current_search_dir)
return []
def find_all_dependencies(file_paths: list, project_root: str):
dependencies = set()
to_process = file_paths.copy()
processed = set()
while to_process:
current_file = os.path.abspath(to_process.pop())
if current_file in processed:
continue
processed.add(current_file)
dependencies.add(current_file)
try:
imported_modules = find_imported_modules(current_file)
except SyntaxError as e:
raise SyntaxError(f"Syntax error in file {current_file}: {e}")
# potential_init_file = os.path.abspath(os.path.join(os.path.dirname(current_file), '__init__.py'))
# if os.path.exists(potential_init_file) and potential_init_file not in processed:
# to_process.append(potential_init_file)
# dependencies.add(potential_init_file)
for module in imported_modules:
module_path = resolve_module_path(module, current_file, project_root)
if len(module_path) > 0:
for each_module_path in module_path:
each_module_path = os.path.abspath(each_module_path)
if each_module_path not in processed:
to_process.append(each_module_path)
return dependencies
def enhance_import_for_py_files(addon_dir: str):
namespace = os.path.basename(addon_dir)
all_py_modules = find_all_py_modules(addon_dir)
all_py_file = search_files(addon_dir, {".py"})
for py_file in all_py_file:
content = read_utf8(py_file)
for module_path in import_module_pattern.finditer(content):
original_module_path = module_path.groups()[0]
if original_module_path in all_py_modules:
content = content.replace("from " + original_module_path + " import",
"from " + namespace + "." + original_module_path + " import")
write_utf8(py_file, content)
def find_all_py_modules(root_dir: str) -> set:
all_py_modules = set()
all_py_file = search_files(root_dir, {".py"})
for py_file in all_py_file:
rel_path = str(os.path.relpath(py_file, root_dir))
modules = rel_path.replace("__init__.py", "").replace(".py", "").split(os.path.sep)
if len(modules[-1]) == 0:
modules = modules[0:-1]
module_name = ""
for i in range(len(modules)):
module_name += modules[i] + "."
all_py_modules.add(module_name[0:-1])
return all_py_modules
class FileUpdateHandler(FileSystemEventHandler):
def __init__(self):
super(FileUpdateHandler, self).__init__()
self.has_update = False
def on_any_event(self, event):
source_path = event.src_path
if source_path.endswith(".py") and event.event_type not in ["opened", "closed"]:
print(event.event_type, source_path)
self.has_update = True
def clear_update(self):
self.has_update = False
def start_watch_for_update(init_file, addon_name, stop_event: threading.Event):
path = os.path.join(PROJECT_ROOT, "addons")
event_handler = FileUpdateHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while not stop_event.is_set():
time.sleep(1)
if event_handler.has_update:
try:
update_addon_for_test(init_file, addon_name)
event_handler.clear_update()
except Exception as e:
print(e)
print(
"Addon updated failed: Please make sure no other process is using the addon folder. You might need to restart the test to update the addon in Blender.")
print("Stop watching for update...")
except KeyboardInterrupt:
observer.stop()
observer.join()
def update_addon_for_test(init_file, addon_name):
addon_path = release_addon(init_file, addon_name, with_timestamp=False,
release_dir=TEST_RELEASE_DIR, need_zip=False)
executable_path = os.path.join(os.path.dirname(addon_path), addon_name)
test_addon_path = os.path.join(default_blender_addon_path(BLENDER_PATH), addon_name)
if os.path.exists(test_addon_path):
shutil.rmtree(test_addon_path)
shutil.copytree(executable_path, test_addon_path)
# write an MD5 to the addon folder to inform the addon content has been changed
addon_md5 = get_md5_folder(executable_path)
write_utf8(os.path.join(test_addon_path, __addon_md5__signature__), addon_md5)