Shotgun API Tips and Tricks Megapost

Fix your Statuses!

In my days as a coordinator before joining Shotgun, I was working with a production team for an animated short. Asset production and Layout was largely wrapped, and we were moving towards Animation/early Lighting. In an effort to prep for that work, the director wanted to clean up all the Task statuses that were Asset/Layout specific, and add/adjust for the next stage of the pipe.

Now, my rookie Shotgun self didn’t realize at the time that Task statuses are global, and not bespoke to an Asset, or the Layout work in a Shot. So when I deleted statuses from the list, and renamed others, it affected all the in-flight and finished Tasks. Suffice to say, wiping out the status info for a couple of departments was not well received.

The Event Logs to the rescue! With the script below I was able to remap all the now blank statuses. Hope it helps anyone else who finds themselves in a similar situation!

from datetime import datetime, timedelta

# Find all Task change events in a certain range, in this example between today and yesterday
date_range = [datetime.today(), datetime.today() - timedelta(1)]
event_filters = [
    ['created_at', 'between', date_range],
    ['event_type', 'is', 'Shotgun_Task_Change'],
    ['attribute_name', 'is', 'sg_status_list']
]

events = sg.find('EventLogEntry', event_filters, ['meta'])

# Define status removed, and with what it should be replaced
removed_status = 'pndng'
new_status = 'wtg'

batch_data = list()

# Loop over events, pick out specific events where status field was left empty due to specific status removal
for event in events:
	if event['meta']['new_value'] == None and event['meta']['old_value'] == removed_status:
		print "Id of changed Task: %d, updating status to: %s"%(event['meta']['entity_id'],new_status)
		data = {'sg_status_list': new_status}
		batch_data.append({'request_type':'update','entity_type':'Task', 'entity_id':event['meta']['entity_id'], 'data':data})

# Mass update affected Tasks
updated_tasks = sg.batch(batch_data)
print updated_tasks
12 Likes