0

sg.update() failing for 'user' field (on Versions)

New guy here. Having a lot of fun with the API so far. Like most of you, we're developing our own standalone tools, but we're also working to integrate with Softimage. So if any of you are Softimage users, a special shout out to you!

I'm working on tidying up our submitter tool, and one of the things I want to do is update the 'Artist' field on Versions to show the name of the user who is making the submission. By default it inherits the name of the entity who creates it, in this case a Script. I figured I could just push an update to the Artist field, which is internally called 'user', and is an entity. But that's where I'm hitting a snag.

Here's a quick example of what I'm trying to do, in Python:

 

#My first step is to identify who is using the submitter. I query the Windows Login for that. (Windows Logins match our SG logins)

currentUser = os.environ.get( "USERNAME" )
filters = [

    ['login','is', str(windowsLogin)]
]
userName = sg.find('HumanUser',filters, ['name'])[0]['name']  
#This correctly returns the full name of the artist (i.e. 'Tim Crowson')


#My next step is try and push this to the 'user' field of a Version I have just created (and whose ID I get from the creation process)
data = {

     'user': userName
     }
update = sg.update('Version', version_id, data)


And this is where the problem is. When I do this, I get an exception: API update() Version.user expected [Hash, NilClass] data type(s) but got String: 'Tim Crowson'

My guess is that it's because I'm trying to pass a string to an Entity field. Do I need a dict instead? Fairly sure it's just a syntax problem, but like I said, I'm quite new to this still.

 

Not entirely sure what syntax I should be using...

3 comments

  • 0
    Avatar
    Mathieson Facer

    The "user" field on the version expects more than just the user's name. It will create a link between the user entity and the version entity, therefore it needs data to be able to correctly identify that user entity. There can be multiple users with the same name, so providing just the name is not enough information.

    What you need to do is provide a dictionary value containing the entity type and id value of the user you want to assign to the version.

     

    currentUser = os.environ.get("USERNAME")

    filters = [

        ['login', 'is', str(windowsLogin)]

        ]

    user = sg.find('HumanUser', filters)

     

    data = {

        'type'=user['type'],

        'id'=user['id']

        }

    update = sg.update('Version', version_id, data)

     

    That should be more along the lines of what you need to do. Please note that I haven't tested the above code.

  • 0
    Avatar
    Mathieson Facer

    Sorry, wasn't quite right. Wasn't able to test earlier when I posted. The explanation is still the same, just my sample code didn't work. Here is an update.

     

    username = os.environ.get("USERNAME")

    filters = [['login', 'is', str(username)]]

    user = sg.find_one('HumanUser', filters)

    data = {'user':user}

    sg.update('Version', version_id, data)

     

    The variable user is the dictionary returned from the query. That dictionary already has the 'type' and 'id' keys that I was mentioning in my previous post. Since it already has the 'type' and 'id' keys, we can just pass it straight into the data dictionary we are creating.

    Hope that helps.

  • 0
    Avatar
    Tim Crowson

    Mat, that did the trick. Thanks! I figured a dictionary is what I needed. Funny thing is, I had tried a similar route at one point, but using find() instead of find_one(). The latter makes more sense.

    And thanks for explaining how the user field works. That clears things up.

Please sign in to leave a comment.