-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtd_manager.py
More file actions
390 lines (322 loc) · 15.8 KB
/
td_manager.py
File metadata and controls
390 lines (322 loc) · 15.8 KB
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
"""TouchDesigner version discovery and .toe file inspection."""
import os
import platform
import re
import glob
import plistlib
import subprocess
import logging
from typing import Dict, Optional, Tuple
from utils import get_resource_path
logger = logging.getLogger(__name__)
class TDManager:
"""Manages TouchDesigner version discovery and .toe file operations."""
def __init__(self):
self.versions: Dict[str, dict] = {}
self.player_versions: Dict[str, dict] = {}
self.discover_versions()
def discover_versions(self) -> Dict[str, dict]:
"""Discover installed TouchDesigner and TouchPlayer versions."""
if platform.system() == 'Windows':
self.versions = self._query_windows_registry("TouchDesigner")
# On Windows TouchPlayer.exe lives next to TouchDesigner.exe
self.player_versions = self._derive_windows_player_versions()
else:
self.versions = self._query_mac_applications("TouchDesigner")
self.player_versions = self._query_mac_applications("TouchPlayer")
return self.versions
def _derive_windows_player_versions(self) -> Dict[str, dict]:
"""Derive TouchPlayer versions from existing TD installations on Windows.
TouchPlayer.exe sits alongside TouchDesigner.exe in each install's bin/ folder.
"""
player_dict = {}
for td_key, info in self.versions.items():
install_path = info.get('install_path', '')
player_exe = os.path.join(install_path, "bin", "TouchPlayer.exe")
if os.path.exists(player_exe):
# Convert key from TouchDesigner.X.Y to TouchPlayer.X.Y
numeric = td_key.split('.', 1)[1] if '.' in td_key else td_key
player_key = f"TouchPlayer.{numeric}"
player_dict[player_key] = {
'install_path': install_path,
'executable': player_exe
}
return player_dict
def _query_windows_registry(self, product: str = "TouchDesigner") -> Dict[str, dict]:
"""Query Windows registry for TouchDesigner or TouchPlayer installations.
Primary: HKLM\\SOFTWARE\\Derivative\\<product> — version entries matched
to Path entries by substring. Fallback for any unmatched versions:
HKEY_CLASSES_ROOT\\<product>.<version>\\shell\\open\\command which stores
the executable path directly via file-association registration.
"""
try:
import winreg
except ImportError:
return {}
td_dict = {}
# -- Primary: HKLM path-based lookup --------------------------------
versions = []
try:
key = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
rf"SOFTWARE\Derivative\{product}"
)
num_values = winreg.QueryInfoKey(key)[1]
paths = {}
for i in range(num_values):
name, value, vtype = winreg.EnumValue(key, i)
if name.startswith('Path'):
paths[name] = value
elif re.match(r'^\d{4}\.\d+$', name):
versions.append(name)
# Match each version to its install path
for version in versions:
install_path = None
for path_name, path_value in paths.items():
if version in path_value:
install_path = path_value
break
if install_path:
exe_path = os.path.join(install_path, "bin", f"{product}.exe")
if os.path.exists(exe_path):
td_key = f"{product}.{version}"
td_dict[td_key] = {
'install_path': install_path,
'executable': exe_path
}
winreg.CloseKey(key)
except (WindowsError, FileNotFoundError):
pass
# -- Fallback: HKCR file-association lookup for unmatched versions ---
resolved_versions = {k.split('.', 1)[1] for k in td_dict}
unmatched = [v for v in versions if v not in resolved_versions]
if unmatched:
logger.debug(
"HKLM path match missed %d version(s), falling back to HKCR: %s",
len(unmatched), unmatched,
)
for version in unmatched:
hkcr_key_path = rf"{product}.{version}\shell\open\command"
try:
command_val = winreg.QueryValue(
winreg.HKEY_CLASSES_ROOT, hkcr_key_path
)
# Value is typically: "C:\...\TouchDesigner.exe" "%1"
exe_path = command_val.split('"')[1] if '"' in command_val else None
if exe_path and os.path.exists(exe_path):
td_key = f"{product}.{version}"
install_path = os.path.dirname(os.path.dirname(exe_path))
td_dict[td_key] = {
'install_path': install_path,
'executable': exe_path,
}
except OSError:
pass
return td_dict
def _query_mac_applications(self, product: str = "TouchDesigner") -> Dict[str, dict]:
"""Query macOS Applications folder for TouchDesigner or TouchPlayer installations.
Reads Info.plist to get actual version info.
"""
td_dict = {}
applications_dir = "/Applications"
td_pattern = os.path.join(applications_dir, f"{product}*")
logger.debug(f"Searching pattern: {td_pattern}")
td_apps = glob.glob(td_pattern)
logger.debug(f"Found {len(td_apps)} potential {product} apps")
for app_path in td_apps:
if not app_path.endswith('.app'):
continue
app_name = os.path.basename(app_path)
info_plist_path = os.path.join(app_path, "Contents", "Info.plist")
logger.debug(f"Processing app: {app_name}")
try:
# Read the Info.plist file
with open(info_plist_path, 'rb') as f:
plist_data = plistlib.load(f)
# Extract version information
bundle_version = plist_data.get('CFBundleVersion', '')
logger.debug(f"Bundle version: {bundle_version}")
if bundle_version:
version_parts = bundle_version.split('.')
if len(version_parts) >= 2:
year = version_parts[0]
build = version_parts[1] if len(version_parts) > 1 else "0"
td_key = f"{product}.{year}.{build}"
executable_path = os.path.join(app_path, "Contents", "MacOS", product)
td_dict[td_key] = {
'executable': executable_path,
'app_path': app_path,
'bundle_version': bundle_version
}
logger.debug(f"Found {product}: {td_key} at {executable_path}")
else:
logger.warning(f"Could not parse version from {bundle_version}")
else:
logger.warning(f"No bundle version found for {app_name}")
except (FileNotFoundError, plistlib.InvalidFileException, KeyError) as e:
logger.error(f"Could not read Info.plist for {app_path}: {e}")
return td_dict
@staticmethod
def parse_version_string(version_str: str) -> Tuple[int, int]:
"""Parse version string into (year, build) tuple.
Handles 'TouchDesigner.2025.32280', 'TouchPlayer.2025.32280', or just '2025.32280'.
"""
try:
# Remove product prefix if present
for prefix in ('TouchDesigner.', 'TouchPlayer.'):
if version_str.startswith(prefix):
version_str = version_str[len(prefix):]
break
parts = version_str.split('.')
year = int(parts[0]) if len(parts) > 0 else -1
build = int(parts[1]) if len(parts) > 1 else -1
return (year, build)
except Exception:
return (-1, -1)
# --- TouchDesigner accessors ---
def get_sorted_version_keys(self) -> list:
"""Get version keys sorted by year and build number."""
return sorted(list(self.versions.keys()), key=self.parse_version_string)
def is_version_installed(self, version: str) -> bool:
"""Check if a specific version is installed."""
return version in self.versions
def get_executable(self, version: str) -> Optional[str]:
"""Get the executable path for a version."""
if version in self.versions:
return self.versions[version].get('executable')
return None
def get_app_path(self, version: str) -> Optional[str]:
"""Get the .app bundle path for a version on macOS."""
if version in self.versions:
return self.versions[version].get('app_path')
return None
# --- TouchPlayer accessors ---
def get_sorted_player_keys(self) -> list:
"""Get player version keys sorted by year and build number."""
return sorted(list(self.player_versions.keys()), key=self.parse_version_string)
def is_player_installed(self, version: str) -> bool:
"""Check if a matching TouchPlayer version is installed.
Accepts either 'TouchDesigner.X.Y' or 'TouchPlayer.X.Y' — matches by numeric part.
"""
target = self.parse_version_string(version)
return any(self.parse_version_string(k) == target for k in self.player_versions)
def get_player_executable(self, version: str) -> Optional[str]:
"""Get the TouchPlayer executable path for a version."""
if version in self.player_versions:
return self.player_versions[version].get('executable')
return None
def get_player_app_path(self, version: str) -> Optional[str]:
"""Get the TouchPlayer .app bundle path for a version on macOS."""
if version in self.player_versions:
return self.player_versions[version].get('app_path')
return None
def get_toeexpand_path(self) -> Optional[str]:
"""Get path to toeexpand tool from an installed TD version."""
if platform.system() == 'Windows':
# On Windows, look for bundled toeexpand
toeexpand_path = get_resource_path(os.path.join("toeexpand", "toeexpand.exe"))
if os.path.exists(toeexpand_path):
return toeexpand_path
else:
# On Mac, use toeexpand from the newest installed TD version
# (older toeexpand may fail to read .toe files from newer TD versions)
if self.versions:
sorted_keys = self.get_sorted_version_keys()
for key in reversed(sorted_keys):
app_path = self.versions[key].get('app_path')
if app_path:
toeexpand_path = os.path.join(app_path, "Contents", "MacOS", "toeexpand")
if os.path.exists(toeexpand_path):
return toeexpand_path
return None
def inspect_toe_file(self, file_path: str) -> Optional[str]:
"""Inspect a .toe file to determine its required TD version.
Uses toeexpand tool from TouchDesigner installation.
Returns version string like 'TouchDesigner.2023.12370' or None on error.
"""
if not os.path.exists(file_path):
logger.error(f"File does not exist: {file_path}")
return None
toeexpand_path = self.get_toeexpand_path()
if not toeexpand_path:
logger.error("No toeexpand found - need TouchDesigner installation")
return None
logger.info("Analyzing TOE file version...")
logger.debug(f"Using toeexpand: {toeexpand_path}")
command = [toeexpand_path, '-b', file_path]
logger.debug(f"Running command: {command}")
try:
startupinfo = None
if platform.system() == 'Windows':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
process = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
startupinfo=startupinfo
)
out, err = process.communicate()
raw_output = out.decode('utf-8')
raw_error = err.decode('utf-8')
logger.debug(f"toeexpand stdout: {repr(raw_output)}")
if raw_error:
logger.debug(f"toeexpand stderr: {repr(raw_error)}")
# toeexpand often returns 1 even with valid output
if process.returncode != 0:
logger.debug(f"toeexpand returned exit code {process.returncode} (often normal)")
build_info = raw_output.replace('\r', '')
logger.debug(f"Cleaned build_info: {repr(build_info)}")
if not build_info or len(build_info.strip()) < 5:
logger.error("toeexpand produced no useful output")
return None
info_split = [line.strip() for line in build_info.split('\n') if line.strip()]
logger.debug(f"Filtered info: {info_split}")
if len(info_split) < 2:
logger.error(f"Unexpected toeexpand output format: {info_split}")
return None
version_line = info_split[1]
logger.debug(f"Version line: {version_line}")
version_number = version_line.split(" ")[-1]
build_option = f'TouchDesigner.{version_number}'
logger.info(f"TOE file requires {build_option}")
return build_option
except Exception as e:
logger.error(f"Error running toeexpand: {e}")
return None
def generate_download_url(self, build_option: str) -> Optional[str]:
"""Generate download URL for a TD version."""
try:
split_options = build_option.split('.')
if len(split_options) < 3:
return None
product = split_options[0]
year = split_options[1]
build = split_options[2]
# Platform and architecture-specific file extension
if platform.system() == 'Windows':
extension = '.exe'
arch_suffix = ''
else: # Mac
extension = '.dmg'
machine = platform.machine().lower()
if machine in ['arm64', 'aarch64']:
arch_suffix = '.arm64'
elif machine in ['x86_64', 'amd64']:
arch_suffix = '.intel'
else:
arch_suffix = '.intel'
logger.warning(f"Unknown Mac architecture '{machine}', defaulting to Intel")
# Generate URL based on build option and platform
if year in ["2017", "2018"] and platform.system() == 'Windows':
url = f'https://download.derivative.ca/TouchDesigner099.{year}.{build}.64-Bit{extension}'
elif year == "2019" and platform.system() == 'Windows':
url = f'https://download.derivative.ca/TouchDesigner099.{year}.{build}{extension}'
else:
url_product = product if product == 'TouchPlayer' and platform.system() != 'Windows' else 'TouchDesigner'
url = f'https://download.derivative.ca/{url_product}.{year}.{build}{arch_suffix}{extension}'
return url
except Exception as e:
logger.error(f"Error generating download URL: {e}")
return None