-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartup.py
395 lines (331 loc) · 13.6 KB
/
startup.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
# Copyright (c) 2020 Autodesk, Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the Shotgun Pipeline Toolkit
# Source Code License included in this distribution package. See LICENSE.
# By accessing, using, copying or modifying this work you indicate your
# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Autodesk, Inc.
import os
import subprocess
import re
import sgtk
from sgtk.platform import SoftwareLauncher, SoftwareVersion, LaunchInformation
from sgtk.util import is_windows, is_macos, is_linux
class VREDLauncher(SoftwareLauncher):
"""
Handles launching VRED executables. Automatically starts up
a tk-vred engine with the current context in the new session
of VRED.
"""
# Product code names
CODE_NAMES = {
"VRED Pro": dict(icon="icon_pro_256.png"),
"VRED Design": dict(icon="icon_design_256.png"),
"VRED Presenter": dict(icon="icon_presenter_256.png"),
}
@property
def minimum_supported_version(self):
"""The minimum VRED version that is supported by the launcher."""
return "2020.0"
@property
def minimum_supported_presenter_version(self):
"""The minimum VRED Presenter version that is supported by the launcher."""
return "2021.2"
def prepare_launch(self, exec_path, args, file_to_open=None):
"""
Prepares an environment to launch VRED in that will automatically load
Toolkit and the tk-vred engine when VRED starts.
:param str exec_path: Path to VRED executable to launch.
:param str args: Command line arguments as strings.
:param str file_to_open: (optional) Full path name of a file to open on launch.
:returns: :class:`LaunchInformation` instance
"""
required_env = {}
# Command line arguments
args += " -insecure_python"
if os.getenv("DISABLE_VRED_OPENGL", "0") == "1":
args += " -no_opengl"
if os.getenv("ENABLE_VRED_CONSOLE", "0") == "1":
args += " -console"
# Register plugins
plugin_dir = os.path.join(self.disk_location, "plugins", "Shotgun")
vred_plugins_dir = os.path.join(os.path.dirname(exec_path), "Scripts")
# be sure to not override the VRED_SCRIPT_PLUGINS environment variable if it's already declared
if "VRED_SCRIPT_PLUGINS" in os.environ.keys():
required_env["VRED_SCRIPT_PLUGINS"] = "{};{};{}".format(
plugin_dir, vred_plugins_dir, os.environ["VRED_SCRIPT_PLUGINS"]
)
else:
required_env["VRED_SCRIPT_PLUGINS"] = "{};{}".format(
plugin_dir, vred_plugins_dir
)
# SHOTGUN_ENABLE is an extra environment variable required by VRED
required_env["SHOTGUN_ENABLE"] = "1"
# Prepare the launch environment with variables required by the
# classic bootstrap approach.
self.logger.debug("Preparing VRED Launch...")
required_env["SGTK_ENGINE"] = self.engine_name
required_env["SGTK_CONTEXT"] = sgtk.context.serialize(self.context)
# Add the `file to open` to the launch environment
if file_to_open:
required_env["SGTK_FILE_TO_OPEN"] = file_to_open
# Add VRED executable path as an environment variable to be used by the translators
required_env["TK_VRED_EXECPATH"] = exec_path
# Add VRED version to control API imports in hooks
raw_version = _get_windows_version(exec_path, self.logger)
required_env["TK_VRED_VERSION"] = self._map_version_year(raw_version)
return LaunchInformation(exec_path, args, required_env)
def scan_software(self):
"""
Scan the filesystem for VRED executables.
:return: A list of :class:`SoftwareVersion` objects.
"""
self.logger.debug("Scanning for VRED executables...")
if is_macos():
# No Mac version
return []
if is_linux():
# TODO: Add linux support
self.logger.debug("Linux support coming soon.")
return []
supported_sw_versions = []
for sw_version in self._find_software():
# First check if it is in the list of versions defined by the engine laucher
if self.versions and not sw_version.version in self.versions:
self.logger.debug(
f"Version '{sw_version.version}' not included in the Software Entity versions list {self.versions} -- skipping"
)
continue
if re.search("Presenter", sw_version.product):
supported = False
reason = "VRED Presenter is not supported"
else:
supported, reason = self._is_supported(sw_version)
if supported:
supported_sw_versions.append(sw_version)
else:
self.logger.debug(
"SoftwareVersion %s is not supported: %s" % (sw_version, reason)
)
return supported_sw_versions
def scan_for_presenter(self):
"""
Scan the filesystem for VRED Presenter executables.
:return: A list of :class:`SoftwareVersion` objects.
"""
self.logger.debug("Scanning for VRED Presenter...")
supported_sw_versions = []
for sw_version in self._find_software():
# First check if it is in the list of versions defined by the engine laucher
if self.versions and not sw_version.version in self.versions:
self.logger.debug(
f"Version '{sw_version.version}' not included in the Software Entity versions list {self.versions} -- skipping"
)
continue
supported, reason = self._is_supported(sw_version)
if supported:
supported_sw_versions.append(sw_version)
else:
self.logger.debug(
"SoftwareVersion %s is not supported: %s" % (sw_version, reason)
)
return supported_sw_versions
##########################################################################################
# private methods
def _icon_from_executable(self, code_name):
"""
Find the application icon based on the code_name.
:param code_name: Product code_name (AutoStudio, Design, ...).
:returns: Full path to application icon as a string or None.
"""
if code_name in self.CODE_NAMES:
icon_name = self.CODE_NAMES.get(code_name).get("icon")
path = os.path.join(self.disk_location, "icons", icon_name)
else:
path = os.path.join(self.disk_location, "icon_256.png")
return path
@staticmethod
def _map_version_year(version):
"""
Convert the string version to integers for comparison
:param version: string value of the version
"""
try:
year = int(version[:2]) + 2008
return "{0}{1}".format(year, version[2:6]) # Can include build number here
except Exception:
return version
def _find_software(self):
"""
Find executables in the Registry for Windows
:returns: List of :class:`SoftwareVersion` instances
"""
sw_versions = []
if is_windows():
# Determine a list of paths to search for VRED executables based
# on the windows registry
install_paths_dicts = _get_installation_paths_from_windows_registry(
self.logger
)
for install_paths in install_paths_dicts:
executable_version = self._map_version_year(install_paths["version"])
executable_path = install_paths["path"]
launcher_name = install_paths["_name"]
icon_file = self._icon_from_executable(launcher_name)
# Create The actual SoftwareVersions
sw_versions.append(
SoftwareVersion(
executable_version,
launcher_name,
executable_path,
icon_file,
)
)
return sw_versions
def _is_supported(self, sw_version):
"""
Determine if a software version is supported or not
:param sw_version:
:return: boolean, message
"""
if re.search("Presenter", sw_version.product):
minimum_supported = self.minimum_supported_presenter_version
else:
minimum_supported = self.minimum_supported_version
try:
if int(sw_version.version.replace(".", "")) >= int(
str(minimum_supported).replace(".", "")
):
return True, ""
else:
return False, "Unsupported version of VRED"
except Exception:
return False, "Error determining VRED version"
def _get_installation_paths_from_windows_registry(logger):
"""
Query Windows registry for VRED installations.
:returns: List of dictionaries of paths and versions
where VRED is installed.
"""
# Local scope here
from tank_vendor.shotgun_api3.lib import six
winreg = six.moves.winreg
logger.debug(
"Querying Windows registry for keys "
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Autodesk\\VRED "
"Pro | Design | Presenter"
)
install_paths = []
# VRED install keys
base_key_names = [
[
"SOFTWARE\\Autodesk\\VREDPro",
"VREDLocation",
"\\VREDPro.exe",
"VRED Pro",
],
[
"SOFTWARE\\Autodesk\\VREDDesign",
"VREDLocation",
"\\VREDDesign.exe",
"VRED Design",
],
[
"SOFTWARE\\Autodesk\\VREDPresenter",
"VREDLocation",
"\\VREDPresenter.exe",
"VRED Presenter",
],
]
for base_key_name in base_key_names:
sub_key_names = []
# find all subkeys in keys
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, base_key_name[0])
sub_key_count = winreg.QueryInfoKey(key)[0]
i = 0
while i < sub_key_count:
sub_key_names.append(winreg.EnumKey(key, i))
i += 1
winreg.CloseKey(key)
except WindowsError:
logger.debug("error opening key %s" % base_key_name[0])
# Query the value VREDLocation on all subkeys.
try:
for name in sub_key_names:
key_name = base_key_name[0] + "\\" + name
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, key_name)
try:
base_path = winreg.QueryValueEx(key, base_key_name[1])
# V12.2 has a trailing backslash here
if base_path[0].endswith("\\"):
base_path_used = base_path[0].rstrip("\\")
else:
base_path_used = base_path[0]
full_path = base_path_used + base_key_name[2]
version = _get_windows_version(full_path, logger)
name = base_key_name[3]
install_paths.append(
{"path": full_path, "version": version, "_name": name}
)
logger.debug("Found VREDLocation value for key %s" % key_name)
# Add Presenter from Pro directory here
if name == "VRED Pro":
full_path = base_path_used + "\\VREDPresenter.exe"
name = "VRED Presenter"
install_paths.append(
{"path": full_path, "version": version, "_name": name}
)
logger.debug("Added VREDPresenter.exe from VRED Pro directory")
except WindowsError:
logger.debug(
"Value VREDLocation not found for key %s, skipping key"
% key_name
)
winreg.CloseKey(key)
except WindowsError:
logger.debug("Error opening key %s" % key_name)
return install_paths
def _get_windows_version(full_path, logger):
"""
Use `wmic` to determine the installed version of VRED
"""
version = "0.0.0.0"
# define some startup info to be able to run the command in a silent mode
startup_info = subprocess.STARTUPINFO()
startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startup_info.wShowWindow |= subprocess.SW_HIDE
try:
version_command = subprocess.check_output(
[
"wmic",
"datafile",
"where",
"name=" + '"' + str(full_path).replace("\\", "\\\\") + '"',
"get",
"Version",
"/value",
],
startupinfo=startup_info,
)
except subprocess.CalledProcessError:
command_string = (
"wmic" + " "
"datafile" + " "
"where" + " "
"name=" + '"' + str(full_path).replace("\\", "\\\\") + '"' + " "
"get" + " "
"Version" + " "
"/value"
)
version_command = subprocess.check_output(
command_string, startupinfo=startup_info
)
finally:
logger.debug("Could not determine version using `wmic`.")
if version_command:
version_list = re.findall(r"[\d.]", str(version_command))
version = "".join(map(str, version_list))
return version