0

To Send and Receive Data: Make Shotgun Work

It is probably evident to everyone the real power of Shotgun is its ability to exchange the stored or yet-to-be-stored data (such as Project Name, Shot Name, Task Name, First Frame Number, Output Resolution and etc etc). If set up properly any application supporting Python (such as Maya) would be able to send and receive the information (the data) from Shotgun. Unfortunately this extremely important subject is not well documented. I make my best efforts to puzzle together the randomly spread across Shogun's forum pieces of the information that would help me to understand how this all works in Shotgun. This message is an attempt to summarize of what I could learn so far (from perspective of the Computer Graphics professional for whom Python and other programming languages are subset of the main toolset).

SENDING DATA FROM SHOTGUN.

a. Create Custom Action Menu Item First

To be able to send the data from Shotgun the user should create a Custom Action Menu Item (or for simplicity sake let's call it just a "Custom Action Menu"). After it is created the Custom Action Menu will appear as a new command under the Gear icon or in Right-Click pull down menu.

The Shotgun's Custom Action Menu is created outside Shotgun itself. You can create it from Python command prompt, from Maya Python script editor. Or you can simply write a Python script with a proper syntax, save it and double click to execute. If the Python script was written properly next time you login to your Shotgun session you should be able to see your new Custom Action Menu in place.

The Python syntax for the Custom Action Menu's creation is very simple.

First you need to load so-called "Python modules", or simply-put - a pre-written set of instructions Python needs to be aware of before it executes the commands written in your script.

from shotgun_api3 import Shotgun
from pprint import pprint

 

Then specify a basic info for your Shotgun web site:

SERVER_PATH = 'https://yourShogun.shotgunstudio.com'
SCRIPT_NAME = 'your_api_script'
SCRIPT_KEY = 'your_api_script_key_long_series_of_numbers_and_letters'

Now type the Python command to get connected to your Shotgun

sg = Shotgun(SERVER_PATH, SCRIPT_NAME, SCRIPT_KEY)


Now type the values for the Action Menu itself. Give it a tittle in "title": key. "URL:" is where the real Python script will be uploaded to and downloaded from

"entity_type": key is used to make your Action Menu appear under only specific Shotgun entities such as Project, Shot, Version, Asset and etc

If you create an Action Menu for Versions then type "version". If it is to be used with the Shot entities then type "shot" and etc.


data = {
      "title":"My_Action_Menu",
      "url": "http://www.mydomainnameaddress.com/my_python_scripts/myPython_Script.py:",
      "list_order": 1,
      "entity_type": "Shot",
      "selection_required": False,
    }

 

# Now, the very last command is to actually create the Action Menu

 

menu_item = sg.create("ActionMenuItem", data)


Now. Let talk what happens when the user selects this Action Menu from pull down menu by Right-Clicking the entity name or from the Gear icon pull down menu.


The following description on what happens when the Action Menu is clicked is based on my assumptions other than on my knowledge. So I can easily make false statements here.

As soon as Action Menu is clicked Shotgun looks at this Action Menu's definition (set at the time the Action Menu was created). It is this line of code that is most important:

   "url": "http://www.mydomainnameaddress.com/my_python_scripts/myPython_Script.py:",


Shotgun reads the value defined in this line. First It determinate a so-called PROTOCOL. A Protocol is that word that proceeds the www address. In this case it is "HTTP". After Shotgun knows it is HTTP it is now can download the script into system memory from the specified URL location:

www.mydomainnameaddress.com/my_python_scripts/myPython_Script.py

Since it is a Web Based Protocol (since it is HTTP Protocol) Shotgun is sending all the data using once again a web-based GET protocol.

Depending on what was selected in Shotgun at the time the Action Menu was clicked (Project, Shot, Asset or Version) Shotgun sends a different data to the system memory (via GET protocol). For this example, let's assume Shotgun sends the "Project Name", "User Name" and "Shot Name" data.

The data is sent. The Python script was downloaded from the specified URL address and placed into the system memory for an execution. Python executable is called. Python reads the Python script just downloaded and placed in memory. Hopefully the Python script was written to be able to process the data sent by Shotgun via GET protocol. There must be some string or array variable defined in the downloaded Python script that would then allocate the sent by Shotgun data and process it.

We would need to make sure that the Python script that was downloaded and now being executed  by Python has the right syntax or the set of commands that are able to receive and to process the Shotgun data.

The rest is quite simple. It  is up to the user to decide what they want to do with the information or with the data received from Shotgun.

What I would like to know now is how to write a Python script to make sure Python is able to get the data sent via GET protocol. What would be a command syntax for it? If let's say I have a string array

my_string_array = []

what line of code I should be using to store the data sent from Shotgun via GET protocol into my_string_array?


3 comments

  • 0
    Avatar
    Sputniks

    Now. Let talk what happens when the user select your Action Menu from pull down menu on righ-click of from the Gear icon.

  • 0
    Avatar
    Support

    Hi Pasha,

     

    Per our earlier email conversation, we have some example code and documentation for API scripting available on our github site.  

     

    This article provides example code for how to write the part of a Python script to make sure Python is able to get the data sent via GET protocol:

    https://github.com/shotgunsoftware/python-api/wiki/Example%3A-Handling-Shotgun-ActionMenuItem-calls

     

    It is an example ActionMenu Python class to handle the GET request sent from an ActionMenuItem. It doesn't manage dispatching custom protocols but rather takes the arguments from any GET data and parses them into the easily accessible and correctly typed instance variables for your Python scripts. 

     

    The code demonstrates  how to parses the url into easy to access types variables from the parameters,  action, and protocol sections of the url. This example url  myCoolProtocol://doSomethingCool?user_id=123&user_login=miled&title=All%20Versions&...

    would be parsed like this:

    (string) protocol: myCoolProtocol
    (string) action: doSomethingCool
    (dict) params: user_id=123&user_login=miled&title=All%20Versions&...

    The parameters variable will be returned as a dictionary of string key/value pairs. Here's
    how to instantiate:

    sa = ShotgunAction(sys.argv[1]) # sys.argv[1]

    sa.params['user_login'] # returns 'miled'
    sa.params['user_id'] # returns 123
    sa.protocol # returns 'myCoolProtocol'

     

    You can incorporate the code itself, or simply use examples from it to understand how to achieve the same thing within your script.

     

    I hope that helps.  Please feel free to let us know if you still have questions.

     

    Thanks,

    -Rebecca

  • 0
    Avatar
    Tom Stratton

    Rebecca - Can you provide any suggestions for where to look for more information about getting the script to run on the web-server side of things?


    Writing the python is the easy part, getting info about getting the script to go to "work" when being served from the web-server is the part of the puzzle I am missing. I know this is NOT a shotgun question but thought you might have a suggestion of where to look for an answer.

     

    Thanks

     

    Tom

Please sign in to leave a comment.