User in context is None when machine login is different from sg login


I stumbled across some template issues that seem to be related to a missing user in the context.
As a simple test I launch the Shotgun Python Console from Shotgun Desktop. When my login user matches my shotgun user I get this:

>>> print sgtk.platform.current_engine().context.user
{'type': 'HumanUser', 'id': 90, 'name': 'Nico Rehberg'}
>>> print sgtk.authentication.ShotgunAuthenticator().get_user()

But when I login to the machine with a different user I get this:

>>>  print sgtk.platform.current_engine().context.user
>>> print sgtk.authentication.ShotgunAuthenticator().get_user()

So while it seems I am correctly authorized through SGDesktop I’m still missing the user in the context. Since we use the HumanUser in our templates I guess that is why the workfiles2 app now starts to have issues resolving templates. It also does not show me my tasks. And there will be probably more issues in other scripts too.

Is this supposed to happen, does my login user have to match the SG User? That used to be the case, but I had hoped that with SGDesktop it wasn’t necessary anymore.



So after some more digging this seems to be related to our script user that is still present in our configurations shotgun.yml. When I remove that things work better. For now we need to keep it for some older tools.

It seems sgtk at some point prefers the script user over the actual logged in user. Wouldn’t it make more sense the other way around and use the script user as a fallback option?


Hi @Nico

Glad you found the reason, whilst I can see why that would be desirable, the script user approach is a legacy path, and the reason it takes priority is likely for backward compatibility.

When we introduced the sign-in window, we would have wanted to preserve the original behaviour until the studio was ready to switch over to the new behaviour, so removing the script user from the shotgun.yml would then switch them over to the new behaviour.

1 Like

I think I found a way for us by customizing the get_current_login hook with the following before the default logic:

    auth_user =  authentication.ShotgunAuthenticator().get_user()
    if auth_user and auth_user.login:
        return auth_user.login

Seems to work so far and should give us the best of both worlds. Let’s see if it breaks something down the line.


For everyone wondering as I did:
It is possible to override the get_current_login hook and integrate it in your configuration core/hooks folder.
The config hook will get called without the need to change environment yaml.