Setting environment variables

Hi all,

I’d like to set project/scene/shot environment variables when launching apps from the shotgun app launcher.

I think this is pretty straight forward to do but I’ve had a browse around the docs as well google searching but I’m none the wiser.

Could someone point me in the right direction on how to achieve this?

Thanks

Steve

1 Like

Hi Steve –

The tk-multi-launchapp is the app that handles the launching of software from Desktop, Shotgun, or Create. The app has a hook called before_app_launch, which, as you might guess from the name, runs just before it launches software, and is often used to set the environment before launching.

Take a look at the hook here in github.

You can see that there’s even a commented out example of setting an environment variable in the default hook.

If you’re not familiar with the process of taking over hooks, there’s a very concise walk-through in this post – let me know if you hit any snags, and I can go into more details.

Hope that helps! Let us know if you have any other questions.

2 Likes

Thanks Tannaz, this looks like just what I was looking for
:slight_smile:

2 Likes

also found this link to the python Shotgun python api. Happy days!!

2 Likes

Hi Tannaz,

Would you mind giving me some direction?

The main thing Im trying to do is to set an ocio environment variable with a path to the current project’s ocio config. I had planned to achieve this by creating a text field in the Project entity called colorspace and setting the value to the show working colorspace. If the value was ‘aces’, a bit of python in the before_app_launch hook would set the env variable with the correct path.

I’ve spent a couple of days in the shotgun python API and googling and I have learned that I cannot extract the value, that I enter in the ui, via python as the value is not saved in the schema. Is this correct or am I missing somthing?

I had a look at adding a path in the project’s Pipeline Configuration page but couldn’t work out how to access this with python.

Any help is much appreciated.

Thanks

Steve

1 Like

Hi Steve!

You should be able to use the Shotgun API’s find() or find_one() method to get the value of your custom field from the Project entity. Adding a field to an entity definitely does store the value in your Shotgun database, so I’d be curious to see any documentation that implies otherwise.

I just added a custom field called colorspace to a test project and set its value to ocio; here’s how I got its value (note that Shotgun prepends sg_ to the system names of custom fields):

>  shotgun.find_one('Project', [['id', 'is', 135]], ['sg_colorspace'])

{'type': 'Project', 'id': 135, 'sg_colorspace': 'ocio'}

And if you want to get straight to the field value, you can do:

> shotgun.find_one('Project', [['id', 'is', 135]], ['sg_colorspace'])['sg_colorspace']

'ocio'

Once you have that value, you should be able to do your python magic to determine the path to point your environment to.

I hope that clears things up! Let me know if not!

1 Like

Thanks Tannaz,
This does make things clear. I did think it odd that values entered in the ui wasn’t accessable via python. I think it came from a blog post and I, obviously, misunderstood something in the API docs.
Thanks again
:slight_smile:

2 Likes