I am creating a custom app here and was wondering how that might work with the before app launch hook or if it’s possible at all to set that up. Currently my before app launch hook sets up some environment variable that I use for adding custom python modules to the PYTHONPATH env. I need these custom modules for the app I am building but the app doesn’t seem to call into before app launch. If it’s not possible to call into the before app launch where might I put the logic for appending the PYTHONPATH in my custom app?
Good question.
So the before_app_launch.py
hook belongs to the tk-multi-launchapp
. That app is responsible for launching Software such as Maya or Nuke but is not used to directly launch other Toolkit apps.
In short, the before_app_launch.py
hook can’t be used to set the environment for your app, unless your app will be run from some software such as Nuke that was launched via the tk-multi-launchapp
. For instance, if you launched your custom app from SG Desktop directly, then the tk-multi-launchapp
would not be invoked at any point in the process.
If you are needing to import some modules in your custom app, I would simply sys.path.append("/location/of/modules")
and import the required modules as the first thing you do in the app.
I don’t believe setting the PYTHONPATH
after python is initiated, will work, unless you plan to spawn another python instance off your current one, (which is kind of what the tk-multi-launchapp
does when it starts up software which has its own python interpreter.)
If you’re already in the python instance you will want to append to the sys.path instead.
Does that make sense?
Best
Phil
Got ya that’s basically what I was getting to here so I will just continue on that path. Was just hoping to avoid some redundancy of logic if I could there. Thanks!
If there is a bunch of setup that you want to avoid duplicating, you could make a custom Toolkit framework or python module, that handles the setup. Then the before_app_launch.py
and the custom app could call the same framework/python module.
Ok so having a bit of a problem here. I can’t seem to figure out just where to import my custom tools and modules. I got around the sys.path imports and when my app launches my tools paths are in the sys.path list there. However when I try and import my tools in the app dialog.py file the imports don’t seem to be in scope of the app. I can get around this by importing these things inline of the class methods using the custom tools but if I try to do so any where else they are not available. I assume this is because the app dialog.py file itself is not being imported anywhere and the show_dialog function is being called to instantiate the app I just don’t know where to actually put these imports so they are available in my app and can be called. I hope that makes sense.
Hey, I’m not sure I’m following exactly.
When you say “app”, do you mean the Application
instance specifically or more generally the files inside the app.
I tried adding
sys.path.append("/sg_toolkit/python")
import studio_tools
to here, and then
print "Location", studio_tools.get_studio_location()
to here and that worked.
Are you instead talking about importing it in the app.py
and then being able to access it in the dialog.py
?
If it’s needed in both places I would probably append the location in the app.py
and import it there, and then import it again in the dialog.py
. But if you really wanted to you could define it in the Application
instance. Something like:
from sgtk.platform import Application
import sys
sys.path.append("/sg_toolkit/python")
import studio_tools
class StgkStarterApp(Application):
def init_app(self):
"""
Called as the application is being initialized
"""
self.studio_tools = studio_tools
...
and then in the dialog.py
it would be:
print "Location", self._app.studio_tools.get_studio_location()
Ok was able to figure it out there turns out I was getting a couple of conflicting problems with some other logic I had in my code. Got that ironed out but now onto a new problem lol.
So here is the situation. To build my sys.path information I need to pull some info from shotgun as to where some of my custom tool paths will be residing due to our distributed configs. In the case of non-custom apps (maya etc) in the hook for before app launch I call to self.disk_location in order to derive the current desktop apps configuration location path. However in the context of writing a custom app that is part of the tk-desktop I can’t make that type of call. I tried to use sgtk.platform.current_bundle().disk_location but that does not seem to return the correct configuration path that I am looking for. It should basically be the same path that you would get from the “Open config location on disk” option in the shotgun desktop app. I just don’t know the call for it.
The Application
instance has a disk_location
property as well, which should return back the location on disk where the app is located:
# from dialog.py
self._app.disk_location
> /Users/philips1/Library/Caches/Shotgun/bundle_cache/app_store/tk-multi-publish2/v2.3.8
If it’s the config location you want I think you could do something like this:
# If inside the app instance
self.sgtk.configuration_descriptor.get_path()
# If inside dialog.py
self._app.self.sgtk.configuration_descriptor.get_path()
# else where
import sgtk
engine = sgtk.platform.current_engine()
engine.sgtk.configuration_descriptor.get_path()
> /Users/username/Library/Caches/Shotgun/bundle_cache/app_store/tk-config-basic/v1.2.10
Ok so while this does work in some instances I dont know that it will for me here. The instantiation of self._app in the dialog.py file happens in the AppDialog init so it would not be part of my header imports there for I can’t get to the location there. The use of engine.sgtk.configuration_descriptor.get_path() does return what I would want however an instance of an engine must exist. Since this app is being set up in the shotgun desktop app when the dialog.py file gets called it does not have an instance of an engine as of that point (the call to engine = sgtk.platform.current_engine() returns none).
Yeah fair enough. I would append the path in the App itself before the dialog is even imported, as you can access the app here. Then you can just import it in the dialog without needing to access the path.
Would that work for you?
Where are you getting the self.sgtk.configuration_descriptor.get_path() in app.py? self.sgtk doesn’t exist there…
Woops nevermind its inherited lol looks like this might work!