Get HumanUserID for current user?

I’m new to the shotgrid API. Is there a straightforward way to get the HumanUserID for the current user?

I can get it now, but I have to get the user to enter First and Last name:

filters = [['login', 'is', 'john.doe@mycompany.com']]
userID = sg.find('HumanUser', filters)
userID = userID[0]['id']

It depends a bit. Are you using toolkit? e.g. are you running in a program with ShotGrid integration? Then you can get it from the current context

import sgtk
sgtk.platform.current_engine().context.user

For the ShotGrid api (the one that communicates with the database) I see there is

sg._user.resolve_entity()

which gives you the HumanUser

I’m using the API, not using toolkit.

Hmmm. Not working for me. I get this error message:

#     curUser = sg._user.resolve_entity()
# AttributeError: 'Shotgun' object has no attribute '_user'

Not sure tbh. I guess it depends on how you access the api.
Are you running this in the terminal?In a dcc?

Connecting to shotgrid from a python script in Maya.

If Maya is running with the ShotGrid engine, then you have access to toolkit and shotgun instances there.

import sgtk
engine = sgtk.platform.current_engine()
tk = engine.sgtk
ctx = engine.context
sg = tk.shotgun
user = ctx.user

I can import the api, but can’t seem to import sgtk:

import shotgun_api3
import sgtk

# Error: ImportError: file <maya console> line 3: No module named sgtk #

Hi @brianm

EDIT: this post initially stated that there were no way to know information about yourself with the ShotGrid Python API, that is incorrect. There are information about the current session in Shotgun.config.user_login and Shogun.config.script_name

I would instead suggest that you use the REST API and decode the access token. You will se a “user” section which will contain the user id.

Hope this helps,

-Patrick

Ok, but is there a way to get the user’s “Login” instead? If I had that, then I could get userID. In fact all I need is first name and last name.

SO you are not running toolkit (no Maya engine available in Maya?), how are you authenticating your script to the API?

Not running toolkit. Connecting to the API by using SERVER_PATH, SCRIPT_NAME, and SCRIPT_KEY.

Hi @brianm

If you are using a SCRIPT_NAME and SCRIPT_KEY to connect to ShotGrid, then you are not a connected as a HumanUser. You don’t have a name or login.

By the way, using a Script name and key is NOT meant for regular user interaction with ShotGrid. Due to the access usually granted to script users, they are meant to be used only in secure contexts (e.g. running background tasks on a server). Having a user connect with a script name and key for working with ShotGrid is a potential security risk.

Is there any reason for you not to use the ShogGrid desktop to start your Maya ?

If you did so, then you would have a session in a user’s context and be able to use the code cited by @mmoshev and get the current user id, from which you can get all of the user’s infos.

-Patrick

Replying to my own post to highlight the fact that I was wrong : there IS a way to get the current user information when using the Python ShotGrid API.

from shotgun_api3 import Shotgun
sg = Shotgun(SITE_URL, login=USER_LOGIN, password=USER_PASSWORD)
infos = sg.find_one('HumanUser', [['login', 'is', sg.config.user_login]], ['id', 'firstname', 'lastname'])
print(infos['id'])
print(infos['firstname'])
print(infos['lastname'])

Admittedly, since you need to use your login to initiate the connection, you would already know what your own login is and be able to query your own name right away…

But this can be useful for some library code which works with a pre-existing connection and being passed only the Shotgun object : it can deduce the connection context by the values of which of Shotgun.config.script_name or Shotgun.config.user_login is defined. Unfortunately, it seems that initiating a ShotGrid session with a session_token will not define Shotgun.config.user_login, which is unfortunate.

-Patrick

2 Likes

Ok, thanks for the tip.