-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.py
1359 lines (1089 loc) · 54.4 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2023 Autodesk Inc.
#
# CONFIDENTIAL AND PROPRIETARY
#
# This work is provided "AS IS" and subject to the ShotGrid 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 ShotGrid Pipeline Toolkit Source Code License. All rights
# not expressly granted therein are reserved by Autodesk Inc.
import os
import sys
import pprint
import sgtk
from sgtk.util import LocalFileStorageManager
class AliasEngine(sgtk.platform.Engine):
"""Alias engine for Flow Production Tracking Toolkit."""
# The name of the hidden window, used to parent SG widgets to the Alias main window.
__PROXY_WINDOW_TITLE = "sgtk dialog owner proxy"
def __init__(self, tk, context, engine_instance_name, env):
"""Initialize the engine."""
# Get all environment variables that the engine cares about
self.alias_execpath = os.getenv("TK_ALIAS_EXECPATH", None)
self.alias_bindir = os.path.dirname(self.alias_execpath)
self.alias_codename = os.getenv("TK_ALIAS_CODENAME", "autostudio")
self.alias_version = os.getenv("TK_ALIAS_VERSION", None)
self.__hostname = os.getenv("SHOTGRID_ALIAS_HOST", None)
self.__namespace = os.getenv("SHOTGRID_ALIAS_NAMESPACE", None)
self.__port = os.getenv("SHOTGRID_ALIAS_PORT", None)
if self.__port is not None:
self.__port = int(self.__port)
# The tk-alias python module
self._tk_alias = None
# Keep track of the Flow Production Tracking context by stage name and path to allow context switching.
self._contexts_by_stage_name = {}
self._contexts_by_path = {}
# The menu generator is responsible for adding the Flow Production Tracking menu to the Alias window.
self.__menu_generator = None
# The event watcher manages handling Alias event callbacks.
self.__event_watcher = None
# The data validator provides the functionality for validating Alias data. This is
# set up to be used by the Data Validation App.
self.__data_validator = None
# A socketio client used to communicate with Alias in OpenModel
self.__sio = None
# Information about the server that the socketio client is connected to.
self.__server_info = {}
# Information about the plugin that bootstrapped the engine
self.__plugin_info = {}
# This module will be initialized with the Alias Python API and utility functions on
# calling '__init_api'
self.__alias_py = None
# Create a QWidget to parent all Flow Production Tracking windows to. This widget will set its parent
# as the Alias main window.
self.__proxy_window = None
# When running in the same process as Alias (for Alias versions <2024.0), the engine
# will create the qt app instance.
self.__qt_app = None
if not hasattr(sys, "argv"):
sys.argv = [""]
# Flag inidicating if Flow Production Tracking is running in the same process as Alias. This will
# determine how Flow Production Tracking will communicate with Alias.
self.__in_alias_process = os.path.basename(sys.executable) == "Alias.exe"
open_model = os.getenv("TK_ALIAS_OPEN_MODEL")
if open_model is None:
# For backward compatibility, OpenModel mode is when not executing in the same process
# as Alias
self.__is_open_model = not self.__in_alias_process
else:
self.__is_open_model = open_model in ("1", "true", "True")
# Determine if the engine is running with a GUI or not
if "TK_ALIAS_HAS_UI" in os.environ:
# Found the environment variable to explicitly turn on/off GUI mode.
self.__has_ui = os.environ.get("TK_ALIAS_HAS_UI", False) in (
"1",
"true",
"True",
)
else:
# Not explicitly defined to have a UI, we will say there is a UI if running in the
# same process as Alias (for backward compatibility)
self.__has_ui = self.__in_alias_process
# Call the base engine init method
super(AliasEngine, self).__init__(tk, context, engine_instance_name, env)
# -------------------------------------------------------------------------------------------------------
# Plugin version < 4.0.0 methods
# -------------------------------------------------------------------------------------------------------
@staticmethod
def get_current_engine():
"""
Return the engine that Toolkit is currently running. This is used by the Alias
C++ Plugin to ensure that its reference to the engine is not stale (e.g. the
plugin's reference will become stale after the engine has been reloaded).
"""
return sgtk.platform.current_engine()
def on_plugin_init(self, plugin, alias, python):
"""
Called on plugin initialization for shotgrid.plugin (plugin version <= 3) and for
Alias version < 2024.0.
"""
self.logger.info(
f"Plugin initialized: v{plugin} Alias v{alias} Python v{python}"
)
self.__plugin_info = {
"plugin_version": plugin,
"alias_version": alias,
"python_version": python,
}
# -------------------------------------------------------------------------------------------------------
# Properties
# -------------------------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------------------------
# Base Engine properties
@property
def host_info(self):
"""
Returns information about the application hosting this engine.
:returns: A {"name": application name, "version": application version}
dictionary. eg: {"name": "AfterFX", "version": "2017.1.1"}
"""
return dict(name="Alias", version=self.plugin_info.get("alias_version"))
@property
def has_ui(self):
"""Return True if Alias is running in interactive mode (OpenAlias), otherwise False (for OpenModel)."""
return self.__has_ui
@property
def context_change_allowed(self):
"""Specifies that context changes are allowed by the engine."""
return True
# -------------------------------------------------------------------------------------------------------
# Alias Engine properties
@property
def plugin_info(self):
"""Get the information about the plugin the engine is running with."""
return self.__plugin_info
@property
def in_alias_process(self):
"""Get whether or not the engine is running in the same process as Alias or not."""
return self.__in_alias_process
@property
def alias_py(self):
"""
Get the AliasPy object that can be used to communicate with Alias.
Use the AliasPy.api property to access the Alias API and make requests. Use the other
AliasPy properties for api helper methods.
"""
return self.__alias_py
@property
def event_watcher(self):
"""Get the AliasEventWatcher object to help manager Alias message events and Python callbacks."""
return self.__event_watcher
@property
def data_validator(self):
"""Get the AliasDataValidator object to help validate the Alias data."""
return self.__data_validator
@property
def executable_path(self):
"""Get the path to the currently running Alias executable."""
return self.alias_execpath
# -------------------------------------------------------------------------------------------------------
# Override base Engine class methods
# -------------------------------------------------------------------------------------------------------
def pre_app_init(self):
"""
Sets up the engine into an operational state. Executed by the system and typically
implemented by deriving classes. This method called before any apps are loaded.
"""
self.logger.debug("%s: Initializing..." % (self,))
# Ensure that the framework has been initialiazed. This must be called
# before import 'tk-alias' module, since this module will attempt to
# import the framework.
self.__ensure_framework()
# Import python/tk_alias module
self._tk_alias = self.import_module("tk_alias")
# Initialize the Alias Python API module (requires tk_alias module)
self.__init_api(self.__hostname, self.__port, self.__namespace)
if self.__sio:
self.__server_info = self.__sio.call_threadsafe("server_info")
self.__plugin_info = self.__server_info.get("client") or {}
self.logger.debug(
f"Alias server info: {pprint.pformat(self.__server_info)}"
)
else:
# No server, we're in OpenModel mode.
self.__server_info = {}
def post_app_init(self):
"""This method called after all apps have been loaded."""
from sgtk.platform.qt import QtGui
# If qt is already running (e.g. on engine restart) do the post qt init now, otherwise
# post_qt_init will be called in startup/bootstrap.py after engine created.
instance = QtGui.QApplication.instance()
if instance:
self.post_qt_init()
def post_context_change(self, old_context, new_context):
"""
Runs after a context change has occurred.
:param old_context: The previous context.
:param new_context: The current context.
"""
self.logger.debug("%s: Post context change...", self)
# Rebuild the menu only if we change of context and if we're running Alias in interactive mode
if self.has_ui and self.__menu_generator:
self.__menu_generator.build()
def destroy_engine(self):
"""Called when the engine should tear down itself and all its apps."""
self.logger.debug("%s: Destroying...", self)
if self.__event_watcher:
self.__event_watcher.shutdown()
self.__event_watcher = None
if self.__menu_generator:
if self.__sio and not self.__sio.connected:
self.logger.error(
"No connection to Alias server. Can't remove ShotGrid menu."
)
else:
self.__menu_generator.remove_menu()
self.__menu_generator = None
# Close all Shotgun app dialogs that are still opened since some apps do threads
# cleanup in their onClose event handler
# Note that this function is called when the engine is restarted (through "Reload
# Engine and Apps")
#
# Important: Copy the list of dialogs still opened since the call to close() will modify created_qt_dialogs
dialogs_still_opened = self.created_qt_dialogs[:]
for dialog in dialogs_still_opened:
dialog.close()
if self.__sio:
if self.__sio.connected:
self.__sio.disconnect()
self.__sio = None
if self.__qt_app:
self.__qt_app.quit()
def show_panel(self, panel_id, title, bundle, widget_class, *args, **kwargs):
"""
Show a dialog as panel in Alias as they are not properly supported. In case the widget has already been opened,
do not create a second widget but use the existing one instead.
:param panel_id: Unique identifier for the panel, as obtained by register_panel().
:param title: The title of the panel
:param bundle: The app, engine or framework object that is associated with this window
:param widget_class: The class of the UI to be constructed. This must derive from QWidget.
Additional parameters specified will be passed through to the widget_class constructor.
:returns: the created widget_class instance
"""
self.logger.debug("Begin showing panel {}".format(panel_id))
if not self.has_ui:
self.logger.error(
"Sorry, this environment does not support UI display! Cannot show "
"the requested window '{}'.".format(title)
)
return None
# try to find existing window in order to avoid having many instances of the same app opened at the same time
for qt_dialog in self.created_qt_dialogs:
if not hasattr(qt_dialog, "_widget"):
continue
if qt_dialog._widget.objectName() == panel_id:
widget_instance = qt_dialog
widget_instance.raise_()
widget_instance.activateWindow()
break
# in case we can't find an existing widget, create a new one
else:
# Widgets application
widget_instance = self.show_dialog(
title, bundle, widget_class, *args, **kwargs
)
widget_instance.setObjectName(panel_id)
return widget_instance
def _get_dialog_parent(self):
"""Get Alias dialog parent window."""
# No parent dialog if the engine is not running in GUI mode.
if not self.has_ui:
return None
window = self.__get_or_create_proxy_window()
if window:
return window
return super(AliasEngine, self)._get_dialog_parent()
def _emit_log_message(self, handler, record):
"""
Called by the engine to log messages in Alias Terminal.
All log messages from the toolkit logging namespace will be passed to this method.
:param handler: Log handler that this message was dispatched from.
Its default format is "[levelname basename] message".
:type handler: :class:`~python.logging.LogHandler`
:param record: Standard python logging record.
:type record: :class:`~python.logging.LogRecord`
"""
# TODO: improve Alias API to redirect the logs to the Alias Promptline
pass
def _initialize_dark_look_and_feel(self):
"""
Override the base engine method.
Apply specific styling for Alias.
"""
from sgtk.platform.qt import QtGui
# Initialize the SG Toolkit style to the application.
super()._initialize_dark_look_and_feel()
# Apply Alias specific styling
app = QtGui.QApplication.instance()
app_palette = app.palette()
# The default placeholder text for Alias is black, let's set it back to
# the text color (as it was in Qt5), but with the current placeholder
# text alpha value.
new_placeholder_text_color = app_palette.text().color()
placeholder_text_color = app_palette.placeholderText().color()
new_placeholder_text_color.setAlpha(placeholder_text_color.alpha())
app_palette.setColor(QtGui.QPalette.PlaceholderText, new_placeholder_text_color)
# Set the palette back with the Alias specific styling
app.setPalette(app_palette)
# -------------------------------------------------------------------------------------------------------
# Public methods
# -------------------------------------------------------------------------------------------------------
def restart_process(self):
"""
Restart the process that the engine is running in.
This is only applicable when the engine has a socketio client set up to communicate
with Alias (e.g. OpenAlias mode).
"""
self.logger.info("Restarting the Alias Engine...")
if not self.__sio:
raise NotImplementedError()
if self.__menu_generator:
status = self.__menu_generator.remove_menu()
if status == self.alias_py.AlStatusCode.Success.value:
self.logger.debug(
"Removed Flow Production Tracking menu from Alias successfully."
)
elif status == self.alias_py.AlStatusCode.Failure.value:
self.logger.error(
"Failed to remove Flow Production Tracking menu from Alias"
)
else:
self.logger.warning(
f"Alias Python API menu.remove() returned non-success status code {status}"
)
self.__menu_generator = None
self.__sio.emit_threadsafe("restart")
def shutdown(self):
"""
Shutdown the application running the engine.
This method attempts to exit the Qt application runnign the engine, which will be
responsible for ensuring that the engine is destroyed properly (e.g. calling destroy
on the engine itself).
"""
self.logger.info("Shutting down the Alias Engine...")
from sgtk.platform.qt import QtGui
qt_app = QtGui.QApplication.instance() or self.__qt_app
if qt_app:
self.execute_in_main_thread(qt_app.quit)
else:
# Destroy the engine if there is no qt app
self.execute_in_main_thread(self.destroy)
def __init_qt_app(self):
"""
Initialize the Qt Application.
This should only be called when Flow Production Tracking is running in the same process as Alias, and
Alias does not create a Qt Application (for Alias < 2024.0).
"""
from sgtk.platform.qt import QtCore, QtGui
pyside_folder = os.path.dirname(QtCore.__file__)
site_packages_folder = os.path.dirname(pyside_folder)
lib_folder = os.path.dirname(site_packages_folder)
python_folder = os.path.dirname(lib_folder)
shotgun_create_folder = os.path.dirname(python_folder)
qt_folder = os.path.join(shotgun_create_folder, "Qt")
# PySide plugins
plugins_dir = os.path.join(pyside_folder, "plugins")
if os.path.exists(plugins_dir):
QtCore.QCoreApplication.addLibraryPath(plugins_dir)
# QT plugins
plugins_dir = os.path.join(qt_folder, "plugins")
if os.path.exists(plugins_dir):
QtCore.QCoreApplication.addLibraryPath(plugins_dir)
self.__qt_app = QtGui.QApplication.instance()
if not self.__qt_app:
self.__qt_app = QtGui.QApplication(
["Flow Production Tracking Alias Engine"]
)
self.__qt_app.setQuitOnLastWindowClosed(False)
def _app_quit():
QtGui.QApplication.processEvents()
self.__qt_app.aboutToQuit.connect(_app_quit)
def post_qt_init(self):
"""
Initialize the engine after Qt has been initialized and an app has been created.
This is used by the startup/bootstrap.py to finish setting up the engine after the
Qt app instance has been created, but before the event loop has started.
"""
from sgtk.platform.qt import QtCore, QtGui
instance = QtGui.QApplication.instance()
if not instance:
# Nothing to do if there is no QApplication running.
self.logger.warning(
"Attempted to initialize for Qt but Qt application instance not found."
)
self.__has_ui = False
return
# Ensure that the has ui flag has been set, though it should have been set on engine
# creation to ensure it was initialized properly.
self.__has_ui = True
# Initialie the SG Toolkit style to the application.
self._initialize_dark_look_and_feel()
# unicode characters returned by the shotgun api need to be converted
# to display correctly in all of the app windows
# tell QT to interpret C strings as utf-8
utf8 = QtCore.QTextCodec.codecForName("utf-8")
QtCore.QTextCodec.setCodecForCStrings(utf8)
self.logger.debug("set utf-8 codec for widget text")
# Create the parent dialog for Flow Production Tracking widgets
self.__get_or_create_proxy_window()
# Check that the Alias version is supported. Pop up a warning dialog if not.
self.__check_version_support()
# Initialize engine members that require Qt
self.__menu_generator = self._tk_alias.menu_generation.AliasMenuGenerator(self)
self.__data_validator = self._tk_alias.data_validator.AliasDataValidator(self)
self.__event_watcher = self.__init_alias_event_watcher()
# Build the Flow Production Tracking menu in Alias. It will be added to the Alias main menu bar.
self.__menu_generator.build()
# Run the start up commands
self.__run_app_instance_commands()
# Check if there is a file set to open on startup
path = os.environ.get("SGTK_FILE_TO_OPEN", None)
if path:
if self.__sio:
self.open_file(path)
else:
# Add a timer to delay opening the file for 5 seconds. This is a work around for
# Alias when running SG in the same process (<2024), which is not ready to open
# a file on engine startup. This is not a bullet proof solution, but it should
# work in most cases, and there is not a better alternative to support older
# versions of Alias.
QtCore.QTimer.singleShot(1000 * 5, lambda: self.open_file(path))
# Clear the env var after loading so that it doesn't get reopened on an engine restart.
del os.environ["SGTK_FILE_TO_OPEN"]
def save_context_for_stage(self, context=None):
"""
Save the context associated to the current Alias stage.
We need to save and restore the context because of the different stages the user can use.
As the Stages can change their name, we need to store the context for both the stage name and the stage path.
:param context: We can specify a context to associate to the current stage. If no one is supplied, we will use
the current one.
"""
if not context:
context = self.context
current_stage = self.alias_py.get_current_stage()
if not current_stage:
return
if current_stage.path:
self._contexts_by_path[current_stage.path] = context
self._contexts_by_stage_name[current_stage.name] = context
#####################################################################################
# File I/O
def save_file(self):
"""
Convenience function to call the Alias Python API to save the file and ensure
the context is saved for the current stage.
"""
status = self.alias_py.save_file()
if status == self.alias_py.AlStatusCode.Failure.value:
self.logger.error("Alias Python API save_file failed")
elif status != self.alias_py.AlStatusCode.Success.value:
self.logger.warning(
"Alias Python API save_file returned non-success status code {}".format(
status
)
)
# Save context for the current stage that was updated
self.save_context_for_stage()
def save_file_as(self, path):
"""
Convenience function to call the Alias Python API to save the file and ensure
the context is saved for the current stage.
:param path: the file path to save.
:type path: str
"""
status = self.alias_py.save_file_as(path)
if status == self.alias_py.AlStatusCode.Failure.value:
self.logger.error("Alias Python API save_file_as failed")
elif status != self.alias_py.AlStatusCode.Success.value:
self.logger.warning(
"Alias Python API save_file_as('{}') returned non-success status code {}".format(
path, status
)
)
# Save context for the current stage that was updated
self.save_context_for_stage()
def open_file(self, path):
"""
Convenience function to call the Alias Python API to open a file and ensure
the context is saved for the current stage.
:param path: the file path to open.
:type path: str
"""
status = self.alias_py.open_file(path)
if status == self.alias_py.AlStatusCode.Failure.value:
self.logger.error("Alias Python API open_file failed")
elif status != self.alias_py.AlStatusCode.Success.value:
self.logger.warning(
"Alias Python API open_file('{}') returned non-success status code {}".format(
path, status
)
)
# Save context for the current stage that was updated
self.save_context_for_stage()
#####################################################################################
# AliasEventWatcher callbacks
def on_stage_active(self, result):
"""
This is a callback that is triggered by Alias "StageCreated" events.
Update the Flow Production Tracking context according to the current stage (since it may have changed).
:param result: The result of the Alias stage created event.
:type result: alias_api.PythonCallbackMessageResult
"""
current_stage = self.alias_py.get_current_stage()
# Do nothing if the current stage is invalid
if not current_stage or (not current_stage.name and not current_stage.path):
return
# Do nothing if there are no SG contexts saved for Alias stages yet
if not self._contexts_by_path and not self._contexts_by_stage_name:
return
# Attempt to get the saved SG context for the current Alias stage
context = None
if current_stage.path and current_stage.path in self._contexts_by_path:
# Found the context form the stage path
context = self._contexts_by_path[current_stage.path]
elif current_stage.name and current_stage.name in self._contexts_by_stage_name:
# Found the context form the stage name
context = self._contexts_by_stage_name[current_stage.name]
else:
# Context not found, reset to the project context
context = self.sgtk.context_from_entity_dictionary(self.context.project)
# Only change the context if we found one and it is not the current context
if context and context != self.context:
self.change_context(context)
#####################################################################################
# QT Utils
def open_save_as_dialog(self):
"""
Try to open tk-multi-workfiles2 Save As... dialog if it exists otherwise
launch a Qt file browser for the Save As...
"""
workfiles = self.apps.get("tk-multi-workfiles2", None)
if workfiles:
if hasattr(workfiles, "show_file_save_dlg"):
kwargs = {"use_modal_dialog": True}
workfiles.show_file_save_dlg(**kwargs)
else:
# Alias doesn't appear to have a "save as" dialog accessible via
# python. so open our own Qt file dialog.
from sgtk.platform.qt import QtGui
file_dialog = QtGui.QFileDialog(
parent=self._get_dialog_parent(),
caption="Save As",
directory=os.path.expanduser("~"),
filter="Alias file (*.wire)",
)
file_dialog.setLabelText(QtGui.QFileDialog.Accept, "Save")
file_dialog.setLabelText(QtGui.QFileDialog.Reject, "Cancel")
file_dialog.setOption(QtGui.QFileDialog.DontResolveSymlinks)
file_dialog.setOption(QtGui.QFileDialog.DontUseNativeDialog)
if not file_dialog.exec_():
return
path = file_dialog.selectedFiles()[0]
if os.path.splitext(path)[-1] != ".wire":
path = "{0}.wire".format(path)
if path:
self.save_file_as(path)
def open_delete_stages_dialog(self, new_file=False):
"""
Launch a QT prompt dialog to ask the user if he wants to delete all the existing stages or keep them when
opening a new file.
"""
from sgtk.platform.qt import QtGui
if new_file:
message = "DELETE all objects, shaders, views and actions in all existing Stage before Opening a New File?"
else:
message = "DELETE all objects, shaders, views and actions in all existing Stage before Opening this File?"
message_type = (
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel
)
answer = QtGui.QMessageBox.question(
self._get_dialog_parent(), "Open", message, message_type
)
return answer
#####################################################################################
# Utils
def get_tk_from_project(self, project):
"""
Get the tank instance given the project ID.
This is useful when you have to deal with files from library project.
This method performs the same operation as get_tk_from_project_id, except that it
provides a fallback code path to ensure the pipeline configuration is loaded.
:param project: The project you want to get the tank instance for.
:type project: dict
:return: An sgtk instance.
:rtype: :class`sgtk.Sgtk`
"""
project_id = project["id"]
# first of all, we need to determine if the file we're trying to import lives in the current project or in
# another one
in_current_project = project_id == self.context.project["id"]
if in_current_project:
return self.sgtk
# if the file we're trying to import lives in another project, we need to access the configuration used by this
# project in order to get the right configuration settings
else:
pc_local_path = self.__get_pipeline_configuration_local_path(project)
if not pc_local_path:
self.logger.warning(
"Couldn't get tank instance for project {}.".format(project_id)
)
return None
return sgtk.sgtk_from_path(pc_local_path)
def get_tk_from_project_id(self, project_id):
"""
Get the tank instance given the project ID.
This is useful when you have to deal with files from library project.
NOTE use get_tk_from_project for a more robust method to get the sgtk instance.
:param project_id: Id of the project you want to get the tank instance for
:return: An instance of :class`sgtk.Sgtk`
"""
# first of all, we need to determine if the file we're trying to import lives in the current project or in
# another one
in_current_project = project_id == self.context.project["id"]
if in_current_project:
return self.sgtk
# if the file we're trying to import lives in another project, we need to access the configuration used by this
# project in order to get the right configuration settings
else:
pc_local_path = self.__get_pipeline_configuration_local_path(project_id)
if not pc_local_path:
self.logger.warning(
"Couldn't get tank instance for project {}.".format(project_id)
)
return None
return sgtk.sgtk_from_path(pc_local_path)
def get_reference_template(self, tk, sg_data):
"""
Get the reference_template according to the given context
:param tk: Instance of :class`sgtk.Sgtk` for the project we want to get the reference template from
:param sg_data: Dictionary of Shotgun data containing some context information. This dictionary must contain
the 'task' field
:return: The reference template object
"""
if "task" not in sg_data.keys():
self.logger.error("Couldn't find 'task' key in sg_data dictionary")
return
ctx = tk.context_from_entity_dictionary(sg_data["task"])
if not ctx:
self.logger.error("Couldn't find context from data: {}".format(sg_data))
return
env = sgtk.platform.engine.get_environment_from_context(tk, ctx)
if not env:
self.logger.error("Couldn't get environment from context")
return
engine_settings = env.get_engine_settings(self.name)
if not engine_settings:
self.logger.error("Couldn't get engine settings")
return
reference_template_name = engine_settings.get("reference_template")
if not reference_template_name:
self.logger.error("Couldn't get reference template from settings")
return
return tk.templates.get(reference_template_name)
# -------------------------------------------------------------------------------------------------------
# Private methods
# -------------------------------------------------------------------------------------------------------
def __ensure_framework(self):
"""
Ensure that tk-framework-alias is initialized.
When the engine is launched from FPT Desktop, the engine startup is
executed which initializes the framework. However, when the engine is
bootstrapped in a headless mode (OpenModel), the framework is not
guaranteed to be initialized. The engine depends on the framework, so
ensure that it is set up correctly in this case.
"""
if self.__in_alias_process or not self.__is_open_model:
# The framework should have been initialized by startup.py
# prepare_launch.
return True
# Executing in headless mode (OpenModel), ensure the framework is
# initialized.
self.logger.info("Initializing tk-framework-alias for headless mode")
# Ensure the environment variables are set to import the Alias Python API module
os.environ["ALIAS_PLUGIN_CLIENT_ALIAS_VERSION"] = self.alias_version
os.environ["ALIAS_PLUGIN_CLIENT_ALIAS_EXECPATH"] = self.alias_execpath
# Get the framework location and add it to the python path. This is
# required since the framework uses absolute imports.
tk_alias_framework = self.frameworks.get("tk-framework-alias")
if not tk_alias_framework:
self.logger.error("Missing required tk-framework-alias framework.")
return False
framework_location = self.frameworks["tk-framework-alias"].disk_location
framework_python_path = os.path.join(framework_location, "python")
sys.path.insert(0, framework_python_path)
# Get the startup utils to help initialize the framework.
import tk_framework_alias_utils.startup as startup_utils
# Ensure the python c extension packages are installed in order to
# import the framework server module.
if not startup_utils.ensure_python_c_extension_packages_installed(
python_version=(sys.version_info.major, sys.version_info.minor),
logger=self.logger,
):
self.logger.error(
"Failed to install required python packages for tk-framework-alias."
)
return False
# Successfully initialized framework for headless mode.
return True
def __setup_sio(self, hostname, port, namespace):
"""
Set up the socketio communication with Alias.
Create a socketio client to connect to the running Alias server.
:param hostname: The api server host name to connect to.
:type hostname: str
:param port: The api server port name to connect to.
:type port: int
:param namespace: The api server namespace to connect to.
:type namespace: str
:return: True if the socketio client was created and is connected to the server, False
otherwise.
:rtype: bool
"""
# Create and connect to the server to communicate with Alias
self.__sio = self._tk_alias.ShotGridAliasSocketIoClient(
self, namespace, timeout=60 * 3
)
if not self.__sio:
raise Exception("Failed to create socketio client")
# Connect to the server to start communicating
self.__sio.start(hostname, port)
# Return the connection status
return self.__sio.connected
def __init_api(self, hostname=None, port=None, namespace=None, force=False):
"""
Initialize the Alias Python api module to allow communication with Alias.
The api can be initialized for either OpenAlias (GUI) or OpenModel (headless/batch)
mode.
For OpenAlias, the hostname, port and namespace arguments must be provided. These are
required to connect to the running instance of Alias via a socketio server, which will
provide the Alias API access.
For OpenModel, none of the arguments are needed. Instead of connecting to a server to
access the Alias API, the Alias Python API module can be directly imported (since it
does not need to communicate with a running instance of Alias).
:param hostname: For OpenAlias, the server host name to connect to, to access the api.
:type hostname: str
:param port: For OpenAlias, the server port to connect to, to access the api.
:type port: int
:param namespace: For OpenAlias, the server namespace to connect to, to access the api.
:type namespace: str
:param force: Force the api to be initialized, even if it has already been initialized.
:type force: bool
"""
if self.__alias_py and not force:
# Already initialized.
return
api_module = None
api_proxy_module = None
if self.__in_alias_process:
# Flow Production Tracking is running in the same process as Alias. This is the old way that the
# engine was set up for, and will not work with Alias versions that use Qt for its
# GUI (>=2024.0).
try:
import alias_api
except Exception as api_import_error:
raise Exception(
f"Failed to import Alias Python API in same process as Alias.\n{api_import_error}"
)
api_module = alias_api
if self.__has_ui:
# When running Flow Production Tracking in the same process as Alias, the qt app needs to be
# created by the engine
self.__init_qt_app()
else:
# Flow Production Tracking is running in a separate process than Alias. This is the new way how
# the engine operates: the Alias plugin will bootstrap the engine in a separate
# process than Alias (to avoid Qt conflicts between QtQuick/QML and QWidgets).
if self.__is_open_model or None in (hostname, port, namespace):
# Run in OpenModel model (headless/batch), directly import the Alias Python API
# module for OpenModel, which should already be imported via importing the
# tk-framework-alias module (in python/tk_alias/framework_alias.py).
try:
import alias_api_om
except Exception as api_import_error:
raise Exception(
f"Failed to import Alias Python API for OpenModel.\n{api_import_error}"
)
api_module = alias_api_om
# Initialize the universe to make the api ready for requests.
api_module.initialize_universe()
else:
# Run in OpenAlias mode, an instance of Alias should be running with a server
# listening for client connections to communicate with. Using the socket