-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreloader.py
47 lines (40 loc) · 1.73 KB
/
reloader.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
#Util imports
import sys
import importlib
from pkgutil import iter_modules
from pathlib import Path
from inspect import isclass, ismodule
#Project imports
from em import root_module_name
#This is useful for module reloading when using the 'Reload Scripts' option inside Blender
#It is not pretty, and I'm not sure if there's a much better way to handle it completely automatically
def reload_all_modules():
root_dir = Path(__file__).parent
modulenames = []
_list_modules(modulenames, root_dir, root_module_name)
_reload_all_modules(modulenames, root_dir, root_module_name)
def _reload_all_modules(modulenames, path, module_path):
if module_path:
module_path += '.'
for (_, modulename, ispkg) in iter_modules([path]):
if ispkg:
_reload_all_modules(modulenames, path.joinpath(modulename), module_path + modulename)
elif module_path + modulename in sys.modules:
module = sys.modules[module_path + modulename]
for vn in dir(module):
v = getattr(module, vn)
if not ismodule(v):
continue
v_name = v.__name__
if v_name in modulenames and v_name in sys.modules:
importlib.reload(sys.modules[v_name])
importlib.reload(module)
def _list_modules(modulenames, path, module_path):
if module_path:
module_path += '.'
for (_, modulename, ispkg) in iter_modules([path]):
if ispkg:
modulenames.append(module_path + modulename)
_list_modules(modulenames, path.joinpath(modulename), module_path + modulename)
else:
modulenames.append(module_path + modulename)