Skip to content

Example: Finding a Shot

kporangehat edited this page Dec 10, 2010 · 3 revisions

Find a Shot

We are going to assume we know the 'id' of the Shot we're looking for in this example.

filters = [['id','is',40435]]
result = sg.find_one('Shot',filters)

Pretty simple right? Well here's a little more insight into what's going on.

  • filters is an array of filter conditions. In this example we are filtering for Shots where the 'id' column is 40435.
  • sg is the Shotgun API instance.
  • find_one() is the API method we are calling. We provide it with the entity type we're searching for and our filters.

So what does this return? The variable result now contains:

{'type': 'Shot','id': 40435}

By default, find_one() returns a single dictionary {} object with the type and id fields. So in this example, we found a Shot matching that id, so Shotgun returned it represented as the default dictionary object with type and id keys .

Seeing the Result

How do we know that result contains the Shot dictionary object? You can trust us... but just to be sure, the pprint (PrettyPrint) module is a really good tool to help with debugging. It will print out objects to the console in a nicely formatted way that makes things easier to read. So we'll add that to the import section of our script.

from shotgun import Shotgun
from pprint import pprint # useful for debugging

Putting It All Together

So now you have your awesome first Shotgun Python script"

#!/usr/bin/env python
# ---------------------------------------------------------------------------------------------

# ---------------------------------------------------------------------------------------------
# Imports
# ---------------------------------------------------------------------------------------------
from shotgun import Shotgun
from pprint import pprint # useful for debugging

# ---------------------------------------------------------------------------------------------
# Globals
# ---------------------------------------------------------------------------------------------
SERVER_PATH = "https://demo.shotgunstudio.com" # make sure to change this to https if your studio uses it.
SCRIPT_NAME = 'my_script'     
SCRIPT_KEY = '27b65d7063f46b82e670fe807bd2b6f3fd1676c1'


# ---------------------------------------------------------------------------------------------
# Main 
# ---------------------------------------------------------------------------------------------
if __name__ == '__main__':    

    sg = Shotgun(SERVER_PATH, SCRIPT_NAME, SCRIPT_KEY)

    # ----------------------------------------------
    # Find a Shot by id
    # ----------------------------------------------
    filters = [ ['id','is',40435], ]
    result = sg.find_one('Shot',filters)    
    pprint(result)
    print "The id of the %s is %s." % (result['type'], result['id])

Results:

{'type': 'Shot','id': 40435}
The id of the Shot is 40435.
Clone this wiki locally