# Set ENV variables using tk-multi-launchapp hook before\_app\_launch

**URL:** <https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984>\
**Category:** Pipeline Integrations\
**Created:** [November 27, 2019, 1:24am UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984 "2019-11-27T01:24:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shervinion](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@Shervinion](https://community.shotgridsoftware.com/u/Shervinion)\
**Post date:** [November 27, 2019, 1:24am UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/1 "2019-11-27T01:24:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![benwall](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/benwall/32/1386_2.png) [@benwall](https://community.shotgridsoftware.com/u/benwall)\
**Post date:** [November 27, 2019, 10:59am UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/2 "2019-11-27T10:59:36Z")

</div>

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:

♦ copy `before_app_launch.py` to `{config}/tk-multi-launchapp/before_app_launch.py`  
♦ 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"`

♦ 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!`

---

<div class="post-metadata">

**Author:** ![philip.scadding](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/philip.scadding/32/30_2.png) [@philip.scadding](https://community.shotgridsoftware.com/u/philip.scadding)\
**Post date:** [November 27, 2019, 11:08am UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/3 "2019-11-27T11:08:47Z")

</div>

In the default2 config, the Maya launcher is controlled via [the `Software` entity,](https://support.shotgunsoftware.com/hc/en-us/articles/115000067493-Integrations-Admin-Guide#Configuring%20software%20launches) 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:

> <https://github.com/shotgunsoftware/tk-config-default2/blob/v1.2.10/env/includes/settings/tk-multi-launchapp.yml#L20-L30>

(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](https://github.com/shotgunsoftware/tk-multi-launchapp/blob/6a884aa144851148e8369e9f35a2471087f98d16/hooks/before_app_launch.py#L26), 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.

---

<div class="post-metadata">

**Author:** ![Shervinion](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@Shervinion](https://community.shotgridsoftware.com/u/Shervinion)\
**Post date:** [November 27, 2019, 5:47pm UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/4 "2019-11-27T17:47:47Z")

</div>

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 >>
```

---

<div class="post-metadata">

**Author:** ![Shervinion](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@Shervinion](https://community.shotgridsoftware.com/u/Shervinion)\
**Post date:** [November 27, 2019, 8:08pm UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/5 "2019-11-27T20:08:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shervinion](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@Shervinion](https://community.shotgridsoftware.com/u/Shervinion)\
**Post date:** [November 27, 2019, 8:33pm UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/6 "2019-11-27T20:33:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![philip.scadding](https://sea1.discourse-cdn.com/shotgunsoftware/user_avatar/community.shotgridsoftware.com/philip.scadding/32/30_2.png) [@philip.scadding](https://community.shotgridsoftware.com/u/philip.scadding)\
**Post date:** [November 28, 2019, 9:05am UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/7 "2019-11-28T09:05:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Shervinion](https://avatars.discourse-cdn.com/v4/letter/s/58f4c7/32.png) [@Shervinion](https://community.shotgridsoftware.com/u/Shervinion)\
**Post date:** [November 29, 2019, 5:04pm UTC](https://community.shotgridsoftware.com/t/set-env-variables-using-tk-multi-launchapp-hook-before-app-launch/3984/8 "2019-11-29T17:04:02Z")

</div>

Using one hook and passing in the **engine\_name** works perfectly. Thanks.
