-
Notifications
You must be signed in to change notification settings - Fork 15
/
engine.py
173 lines (136 loc) · 5.25 KB
/
engine.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
"""
A 3dequalizer engine for SGTK.
"""
import os
import re
import logging
import shutil
import traceback
import tde4
from vl_sdv import rot3d, mat3d, VL_APPLY_ZXY
import sgtk
from sgtk.platform import Engine
from sgtk.util.filesystem import ensure_folder_exists
_HEARTBEAT_INTERVAL_MS = 50
_MENU_FOLDER_PATH = os.environ["TK_3DE4_MENU_FOLDER_PATH"]
class TDEqualizerEngine(Engine):
def __init__(self, *args, **kwargs):
self._current_file = tde4.getProjectPath()
Engine.__init__(self, *args, **kwargs)
def _heartbeat(self):
from sgtk.platform.qt import QtCore
# Keep Qt alive
QtCore.QCoreApplication.processEvents()
# check for open file change
cur_file = tde4.getProjectPath()
if self._current_file != cur_file:
if cur_file:
new_context = self.sgtk.context_from_path(cur_file, self.context)
if new_context != self.context:
sgtk.platform.change_context(new_context)
self._current_file = cur_file
def pre_app_init(self):
from sgtk.platform.qt import QtCore, QtGui
if not QtCore.QCoreApplication.instance():
# WARNING: need to keep a python reference to
# the qt app, or python will destroy it and
# ruin everything
self._qt_app = QtGui.QApplication([])
self._initialize_dark_look_and_feel()
tde4.setTimerCallbackFunction(
"sgtk.platform.current_engine()._heartbeat",
_HEARTBEAT_INTERVAL_MS,
)
def post_app_init(self):
self.create_shotgun_menu()
def post_context_change(self, old_context, new_context):
self.create_shotgun_menu()
def destroy_engine(self):
self.logger.debug("%s: Destroying...", self)
self._cleanup_custom_scripts_dir_path()
@property
def context_change_allowed(self):
return True
@property
def host_info(self):
host_info = dict(name="3DEqualizer", version="unknown")
try:
host_info["name"], host_info["version"] = re.match(
r"^([^\s]+)\s+(.*)$", tde4.get3DEVersion()
).groups()
except Exception:
# Fallback to initialized above
pass
return host_info
def create_shotgun_menu(self):
if self.has_ui:
from sgtk.platform.qt import QtCore, QtGui
self.logger.info("Creating ShotGrid menu...")
self._cleanup_custom_scripts_dir_path()
# Get temp folder path and create it if needed.
ensure_folder_exists(_MENU_FOLDER_PATH)
for i, (name, _) in enumerate(self.commands.items()):
display_name = name
# Very strange glitch
if display_name == "Export...":
display_name = "Export ..."
script_path = os.path.join(_MENU_FOLDER_PATH, "{:04d}.py".format(i))
f = open(script_path, "w")
f.write(
"\n".join(
(
"# 3DE4.script.name: {}".format(display_name),
"# 3DE4.script.gui: Main Window::ShotGrid",
"if __name__ == '__main__':",
" import sgtk",
" sgtk.platform.current_engine().commands[{}]['callback']()".format(
repr(name)
),
)
)
)
f.close()
QtCore.QTimer.singleShot(0, tde4.rescanPythonDirs)
self.logger.info("ShotGrid menu created.")
return True
return False
def _cleanup_custom_scripts_dir_path(self):
try:
shutil.rmtree(_MENU_FOLDER_PATH)
except OSError as e:
self.logger.debug(traceback.format_exc())
@property
def has_ui(self):
return True
##########################################################################################
# logging
def _emit_log_message(self, handler, record):
if record.levelno < logging.INFO:
formatter = logging.Formatter("Debug: ShotGrid %(basename)s: %(message)s")
else:
formatter = logging.Formatter("ShotGrid %(basename)s: %(message)s")
msg = formatter.format(record)
print(msg)
def _create_dialog(self, title, bundle, widget, parent):
from sgtk.platform.qt import QtCore
dialog = super(TDEqualizerEngine, self)._create_dialog(
title, bundle, widget, parent
)
dialog.raise_()
dialog.activateWindow()
return dialog
#############
# custom api
@property
def api(self):
return self.import_module("tk_3dequalizer").api
def get_scene_rotation_3d_zxy(self):
return rot3d(mat3d(tde4.getSceneRotation3D())).angles(VL_APPLY_ZXY)
def iter_all_cameras(self):
return self.api.TDECamera.iter_all()
def iter_selected_cameras(self):
return self.api.TDECamera.iter_selected()
def iter_all_point_groups(self):
return self.api.TDEPointGroup.iter_all()
def iter_selected_point_groups(self):
return self.api.TDEPointGroup.iter_selected()