-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbld_main_exp_addon.py
152 lines (120 loc) · 4.32 KB
/
bld_main_exp_addon.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
import importlib
import sys
import os
SELECTED_ACTION = "show" # show caputure
ENV_NEW = {"ENV_AR_RENDER_ENGINE": "EEVEE",
"RENDER_MAX_CNT": "200",
"DISABLE_REFRESH":"false"}
RELOAD_ALL_MODULES = True
def _handle_dev_msg(sender, **kwargs):
print("*hm:", sender, kwargs)
from bld_gen.utils_core.addon_dev_msg_panel import MY_PT_DevMsgPanel
MY_PT_DevMsgPanel.receive_dev_msg(sender, kwargs["message"])
class _InterfaceDevMsg(object):
def __init__(self):
from bld_gen.utils_core.dev_signal_msg import get_sig_msg
self.sig_msg = get_sig_msg()
from bld_gen.utils_core.addon_dev_msg_panel import MY_PT_DevMsgPanel
MY_PT_DevMsgPanel.init()
def send_dev_msg(self, msg):
if self.sig_msg is not None:
ret = self.sig_msg.send('sender', message=msg)
return ret
return []
def connect_dev_msg(self, msg_handler):
if self.sig_msg is not None:
self.sig_msg.connect(msg_handler)
def disconnect_dev_msg(self, msg_hander):
if self.sig_msg is not None:
self.sig_msg.disconnect(msg_hander)
class _RunEnv(object):
instance_dev_msg = None
@classmethod
def init(cls):
if cls.instance_dev_msg is None:
root_dir = cls.get_root_dir()
if root_dir not in sys.path:
sys.path.append(root_dir)
cls.instance_dev_msg = _InterfaceDevMsg()
@classmethod
def get_interface_dev_msg(cls):
return cls.instance_dev_msg
@classmethod
def _pop_last_sub_folder(cls, root_dir):
subfolders = ["_reserved_", "bldmc_"]
names = root_dir.split(os.sep)
for subfolder in subfolders:
if names[-1].startswith(subfolder):
names.pop()
root_dir = os.sep.join(names)
return root_dir
@classmethod
def get_root_dir(cls):
host_file = __file__
print("host_file", host_file)
host_dir = os.path.dirname(host_file)
if host_dir.endswith(".blend"):
root_dir = os.path.dirname(host_dir)
else:
raise ValueError("blend need to be open first")
root_dir = cls._pop_last_sub_folder(root_dir)
root_dir = os.path.dirname(root_dir)
print("root_dir", root_dir)
return root_dir
def _update_sys_paths(renv):
import bpy
#print("__file__", __file__)
blend_dir = os.path.dirname(bpy.data.filepath)
if blend_dir not in sys.path:
sys.path.append(blend_dir)
root_dir = renv.get_root_dir()
if root_dir not in sys.path:
sys.path.append(root_dir)
app_dir = root_dir + os.sep + "bld_gen"
if app_dir not in sys.path:
sys.path.append(app_dir)
print("updated sys.path")
for path in sys.path:
print(path)
print()
def _prepare_run_env():
import os
for key, val in ENV_NEW.items():
os.environ[key] = val
_RunEnv.init()
inf_dev_msg = _RunEnv.get_interface_dev_msg()
if inf_dev_msg is not None:
inf_dev_msg.connect_dev_msg(_handle_dev_msg)
return _RunEnv
def _prepare_to_run(renv):
print("prepare to run")
from bld_gen.utils_core.addon_dev_msg_panel import register
register()
def _prepare_to_exit(renv):
print("prepare to exit")
#from bld_gen.utils_core.dev_msg_panel import unregister
#unregister()
inf_dev_msg = renv.get_interface_dev_msg()
if inf_dev_msg is not None:
inf_dev_msg.disconnect_dev_msg(_handle_dev_msg)
def _dyn_load_to_run(renv, action):
import bld_gen.utils_core.exp_run_core_selection as exp_run_core_selection
print("reload main from main_debug")
importlib.reload(exp_run_core_selection)
exp_run_core_selection.select_core_to_run(renv, action=action)
def _reload_all_modules(renv):
import bld_gen.utils_core.reload_all_modules as reload_all_modules
print("reload reload_all_modules", reload_all_modules)
importlib.reload(reload_all_modules)
root_dir = renv.get_root_dir()
reload_all_modules.reload_all_modules(root_dir)
def debug_main(renv, action):
_prepare_to_run(renv)
_dyn_load_to_run(renv, action)
_prepare_to_exit(renv)
if __name__ == '__main__':
renv = _prepare_run_env()
_update_sys_paths(renv)
if RELOAD_ALL_MODULES:
_reload_all_modules(renv)
debug_main(renv, SELECTED_ACTION)