Precached Filter-View Shotgrid query library


This is my first post here and is to show a small library to create and manage queries in Shotgrid in a easy way and avoiding some of the issues that I always see in the orm when they need to query a big number of elements or go deep in the links between entities.

The basic idea behind this library is make a big query and store the data locally using the pandas library. in this way we can work with that data and we can go to the next linked entity dinamically as the library manage the linked entities and make the same. We can work with this library in a precached or not precached way

This library only have 3 classes and a helper, the classes are DataBase, View and TableFilter, and the helper is EmptyView to don’t break some navigation between links

This also have a config file where we can set the fields that we want to first cache of each entity or a init filter to avoid load too much elements

to start to work with this library we need to create and set the DataBase class

import database 
shotgun_db = database.DataBase()
shotgun_db.fill(project='Demo: Animation', precatch=True)

with this we created the main object and then we can create the views using filters. we can use as filter any of the entity fields and it will manage the seach type depend if is a list of elements or a single element

view_filter = shotgun_db.create_view('Sequence')
view_filter.code = 'bunny_080'
view_filter.unique = True
view = view_filter.get()

print(view.code, view.sg_status_list, view.description)

In this example, using the Demo Animation project tooks 11 seconds to cache all the elements sets to be precatched and 0.0 in the rest of the queries

we can access the fields directly from the view class and it will work in two ways, If is a single item it will return the value and other case it will return a list of all the values of that field

if we access to a linked field it will download all the data from that entity or only the data that we need, depending of the DataBase settings and we can access to them

if we want to access an specific field of those shots we can


as the view is working also as a iterator we can loop over the shots and get more linked entities like the tasks

we can also access to the entities with a dictionary syntax

this_shot = view.shots['bunny_080_0020']

or check if a item is in the view as a View instance or as a code/name/content

print('bunny_080_0020' in view.shots)
print(['bunny_080_0010', 'bunny_080_0020'] in view.shots)
print(['bunny_080_0010', 'bunny_280_0020'] in view.shots)

we can set in the config file which field is the one that will sort the view so we can query for max, min …

versions = this_shot.sg_versions.with_value('sg_status_list', 'rev')

as internally is a pandas DataFrame we can use other libraries like plotly easilly to create chars

So what do you think of this approach?