Access Shotgun context through python within an engine

Hello all,

Within an engine with active toolkit, let’s say Nuke, is there a way to reach the context without reopening a new shotgun connection?


1 Like
## How to get a Shotgun API Connection

import sgtk

# get the engine we are currently running in

current_engine = sgtk.platform.current_engine()

# get hold of the shotgun api instance used by the engine, (or we could have created a new one)

sg = current_engine.shotgun

# Get current Context

current_context = current_engine.context

To be honest, it’s @philip.scadding’s code :wink:


Ok, that’s basically what I meant with reopening a new shotgun connection.
I was editing my question as it was not super clear while you were answering :wink:
This command: sgtk.platform.current_engine() is what I’m using currently. Just wanted to be sure it was the best practice!

Thank you @Ricardo_Musch!


Yeah, make sure you use that everywhere where you can expect the engine running.

I’ll even fail code when the engine isn’t running as in our case, if it aint, thats strange…
That would only happen on the farm.

Thank you for jumping in!

sgtk.platform.current_engine() is just returning the globally stored engine value, so it’s not reopening anything as such, just returning the pointer to the current engine. It should be pretty lightweight, assuming that sgtk has previously been imported in the current session.

1 Like