Set ENV variables using tk-multi-launchapp hook before_app_launch

Hi,

I’ve recently moved our studio’s SG config from tk_config_default to tk_config_default2; I have to say it was not a trivial task, and I’d like to give special thanks to Philip Scalding for his numerous advice on the support forum.

Now to my question:
In the old config, we had set up our ENV variables using:
config\hooks\before_app_launch.py

It works perfectly fine and it was called from config\env\includes\app_launchers.yml. For example Maya block looks like:

launch_maya_2018:
  defer_keyword: ''
  engine: tk-maya
  hook_app_launch: default
  hook_before_app_launch: **'{config}/before_app_launch.py'**
  location:
    version: v0.10.1
    type: app_store
    name: tk-multi-launchapp
  versions: [Maya2018]
  windows_path: '@maya_windows2018'

And the key ‘launch_maya_2018’ was called from inside config\env\project.yml

With the tk_config_default2, where should we put the call to before_app_launch.py?

I assumed the call should go under a maya block in
config\env\includes\settings\tk-multi-launchapp.yml for example:

# app-specific launchapp configurations
# mari
settings.tk-multi-launchapp.mari:EWh
  engine: tk-mari
  linux_path: "@path.linux.mari"
  mac_path: "@path.mac.mari"
  windows_path: "@path.windows.mari"
  menu_name: Mari
  location: "@apps.tk-multi-launchapp.location"

## Maya
settings.tk-multi-launchapp.maya:
  engine: tk-maya
  location: "@apps.tk-multi-launchapp.location"
  hook_before_app_launch: "{config}/before_app_launch.py"

But that doesn’t work.

My other option was to use includes/settings/tk-maya.yml but it doesn’t have a block for tk-multi-launchapp

Btw, I have added engine_name arg to before_app_launch.py in the execute method.

2 Likes

EDIT: Ignore that message, Philip gave the solution down there

Hi there,
I am currently searching for this too, it seems from your post you forgot to add tk-multi-launchapp to tk-maya.yml:

:diamonds: copy before_app_launch.py to {config}/tk-multi-launchapp/before_app_launch.py
:diamonds: add those lines to tk-multi-launchapp.yml:

settings.tk-multi-launchapp.maya:
engine: tk-maya
location: "@apps.tk-multi-launchapp.location"
hook_before_app_launch: "{config}/tk-multi-launchapp/before_app_launch.py"

:diamonds: add tk-multi-launchapp: "@settings.tk-multi-launchapp.tk-maya" to the apps part of your different environments in tk-maya.yml

Please note that I’m not exactly sure I’m right there as this gives me errors when launching SG Desktop, but I think that’s the logic here.

TankError: Include error. Could not resolve references for L:\_tech\sgtk_sandbox\config\env\.\includes\settings\tk-maya.yml: Undefined Reference settings.tk-multi-launchapp.tk-maya!

2 Likes

In the default2 config, the Maya launcher is controlled via the Software entity, as are most of the software we integrate with. So as the software is configured in Shotgun, we have two software generic launch app settings blocks:

(One is specific to the tk-shotgun engine though)

So you would add the hook definition to those two blocks.

Then as you mention, your before_app_launch.py hook needs to now include the engine_name parameter, as this hook will be called for all software. You can use this value to work out which software is launching.

That should hopefully do it.

1 Like

Hi Philip,
Thanks for the reply. I added before_app_launch.py hook to those two blocks in tk-multi-launchapp.ym:

# auto discover DCCs for launch
settings.tk-multi-launchapp:
  use_software_entity: true
  hook_before_register_command: "{config}/tk-multi-launchapp/before_register_command.py"
  hook_before_app_launch: "{config}/before_app_launch.py"
  location: "@apps.tk-multi-launchapp.location"

# shotgun
settings.tk-multi-launchapp.shotgun:
  use_software_entity: true
  skip_engine_instances: ["tk-nukestudio"]
  hook_before_register_command: "{config}/tk-multi-launchapp/before_register_command.py"
  hook_before_app_launch: "{config}/before_app_launch.py"
  location: "@apps.tk-multi-launchapp.location":

But I get a KeyError for ‘MAYA_MODULE_PATH’:

2019-11-27 12:21:52,282 [   ERROR] [PROXY] Error calling __commands::maya_2018((), {}):
Traceback (most recent call last):
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\app_store\tk-desktop\v2.4.12\python\tk_desktop\desktop_engine_project_implementation.py", line 164, in _trigger_callback
    callback(*args, **kwargs)
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\core\python\tank\platform\engine.py", line 1084, in callback_wrapper
    return callback(*args, **kwargs)
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\app_store\tk-multi-launchapp\v0.10.3\python\tk_multi_launchapp\base_launcher.py", line 125, in launch_version
    *args, **kwargs
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\app_store\tk-multi-launchapp\v0.10.3\python\tk_multi_launchapp\base_launcher.py", line 354, in _launch_callback
    file_to_open,
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\app_store\tk-multi-launchapp\v0.10.3\python\tk_multi_launchapp\base_launcher.py", line 185, in _launch_app
    engine_name=app_engine,
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\core\python\tank\platform\bundle.py", line 575, in execute_hook
    **kwargs
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\install\core\python\tank\hook.py", line 618, in execute_hook_method
    ret_val = hook_method(**kwargs)
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\config\hooks\before_app_launch.py", line 227, in execute
    self.env_paths_sanity_check()
  File "X:\sgtk_studio\_projects\sg_advprjsetup\shotgun_configuration_dev\config\hooks\before_app_launch.py", line 260, in env_paths_sanity_check
    for item in os.environ[path_item].split(';'):
  File "C:\Program Files\Shotgun\Python\lib\os.py", line 425, in __getitem__
    return self.data[key.upper()]
KeyError: 'MAYA_MODULE_PATH'

To be clear this is the same before_app_launch.py that works fine on any project before DefCon2.

Looking at SG desktop log, there seems to be no args passed to the execute method:

2019-11-27 12:21:52,071 [   DEBUG] [PROXY] Running before app launch hook...
2019-11-27 12:21:52,073 [   DEBUG] [PROXY] >: Resolved hook expression (associated with setting 'hook_before_app_launch'): '{config}/before_app_launch.py' -> ['X:\\sgtk_studio\\_projects\\sg_advprjsetup\\shotgun_configuration_dev\\install\\app_store\\tk-multi-launchapp\\v0.10.3\\hooks\\before_app_launch.py', 'X:\\sgtk_studio\\_projects\\sg_advprjsetup\\shotgun_configuration_dev\\config\\hooks\\before_app_launch.py']
2019-11-27 12:21:52,101 [   DEBUG] [PROXY] app_path >> 
2019-11-27 12:21:52,101 [   DEBUG] [PROXY] app_args >> 
2019-11-27 12:21:52,102 [   DEBUG] [PROXY] version >> 
2019-11-27 12:21:52,104 [   DEBUG] [PROXY] engine_name >>
1 Like

Philip, please ignore my previous post, it’s solved.

For future references, in the legacy before_app_launch.py we had an if-else to identify maya version that looked like:

 if version == "Maya2018":
        module_path = module_path + os.sep + '2018'

However in DefCon2, version var doesn’t include DCC name so removing “Maya” fixed the problem.

2 Likes

I have a follow up question.
In the old config, I have separate pre_app_launch.py per DCC. For example:

config\hooks\before_app_launch.py >>> handles Maya only variables.
config\before_app_launch_nuke.py
config\before_fusion8_app_launch.py

In DefCon2, can we continue using separated before_app_launch files per DCC, and how would that work?

2 Likes

Using the software entity approach, you don’t get a separate tk-multi-launchapp settings block per software, so you can only define one hook. That hook, in turn, could import separate python files based upon the engine_name however, so that would be the way around it.

Or alternatively, you could go back to the old method of defining tk-multi-launchapp settings per software.

2 Likes

Using one hook and passing in the engine_name works perfectly. Thanks.

2 Likes