Use an ActionMenuItem to import thumbnails from Excel

You may want to upload thumbnails you have in your Excel sheets into Shotgun. The problem here is that thumbnails are stored on the website and need to be uploaded—it’s more than just updating the field on the database. The good news is, there is a workaround that adds one small step to accomplish exactly what you want.

But first, be sure that you are familiar with how to create a protocol handler and how to use the Shotgun API. You will need to use a protocol handler to connect your API script.

Follow the steps below to import your thumbnails from Excel.

Step 1. Create a text field called ThumbPath on the entity type you wish to upload thumbnails to (this example will use Shot).

Step 2. Import the path to your thumbnails into ThumbPath.

Use the importer to add this information to Shotgun.

Excel sheet ThumbPath

Step 3. Create a new Action Menu Item (AMI) linked to the Shot entity.

Call the new AMI “ThumbPath to Thumbnail.”

Note: You can also link similar AMIs to other entity types. The code will work for them unaltered.

Step 4. Connect the API script to Shotgun.

Note: The example protocol handler is called sgactionmenu.

Below is the code for the API script:

!/usr/bin/env python
import sys,os
import urllib
import shotgun_api3 as shotgun

SERVER_PATH = 'https://my_site.shotgunstudio.com'
SCRIPT_USER = 'my_script'
SCRIPT_KEY = '3th4a5542d38f4a6j3s711fef4e6d24baa0885e4'

###
# Take the param string arg1=value1&arg2=value2&arg3... and return it in a key:value dictionary
###
def getParams(a):
        args = a.split("&")
        p = {}
        for arg in args:
                key, value = map(urllib.unquote, arg.split("=", 1))
                p[key] = value
        return p

###
# Take everything after the protocolhandler as an arg
# Return tuple of the action and a string of all the args
# in the format: arg1=value1&arg2=value2&arg3...
###
def getActionFullArgs(fp):
        action, fullArgs = fp.split("?", 1)
        action = action.strip("/")
        return action, fullArgs

###
# This takes a /path/filename from a text field and uploads it as the thumbnail for an entity
# This assumes the field for the thumbnail is in a column called "ThumbPath" (sg_thumbpath)
###
def copyThumbs(sg, params):

        project_id = params['project_id']
        selected_ids = params['selected_ids']
        entity_type = params['entity_type']

        # Loop through the selected IDs
        for id in selected_ids.split(','):
                filters=[['project','is',{'type':'Project','id':int(project_id)}],['id', 'is', int(id)]]
                fields = ['sg_thumbpath', 'image']
                result = sg.find(entity_type, filters, fields)
                sg.upload_thumbnail(entity_type, id, result[0]['sg_thumbpath'])

if __name__ == '__main__':
        # Get the arg string which is 'protocolhandler://action?key1=value1&key2=value2...'
        arg = sys.argv[1]

        # Shotgun!
        sg = Shotgun(SERVER_PATH, SCRIPT_USER, SCRIPT_KEY)

        protocol, fullPath = arg.split(":", 1)
        action, fullArgs = getActionFullArgs(fullPath)
        params = getParams(fullArgs)


        if action == "thumb":
                copyThumbs(sg, params)

Step 5. Upload the thumbnails referenced in your ThumbPath field.

Select Version(s) that have /paths/to/thumbnails in a text field called ThumbPath. Then right-click and select the “ThumbPath to Thumbnail” AMI.

Upload thumbnails

And that’s it! Now you can see thumbnails that you have saved on disk in your Shotgun site.

Follow

1 Comments

  • 0
    Avatar
    Eloi Bartolome

    Very useful but, how is it supposed to convert thumbnails to its path?

Please sign in to leave a comment.