The goal:
To create a Custom Action Menu to be used to trigger Python script. Before the script is run or executed it receives some data value from Shotgun.
The journey.
1. First of all, since everything in Shotgun is happening via Web Browser there must be some steps taken to make sure a web browser knows what to do.
From what I could understand (I am VFX artist and not a web programmer) it is impractical to type PYTHON command (or any other command you can run using OS Command Prompt) into a web browser's URL Address field and to expect Firefox or Chrome to execute it. To make Firefox or any other web browser "aware" of any command there is a need to set up a so-called CUSTOM PROTOCOL HANDLER. While the term "Custom protocol handler" is quite annoying and difficult to remember it is a quite simple by the definition. If you wouldn't mind I would like to take a chance to try to explain the concept behind of it to those who like me desperately tries to understand what you wrongly call "a documentation" (which in a reality is just a computer geeks chat recorded in a form of online forum).
But let's get back to CUSTOM PROTOCOL HANDLER.
An important part of Windows Operation System is a so-called Registry. Registry functions are quite wide. And most of the Windows users don't even know about its existence. As a matter of fact the Registry is hidden by default. You have to know some geeky commands to be able to open the application that would let you edit the Registry's values. As I am aware there are two such commands: "regedit" and "regedit32". You can run both applications (both very similar to each other while "regedit32" is more capable then regedit) by going to Windows Start button and click Run. Then just type "regedit" or "regedit32".
As you could be able to see from RegEdit the Registry is an endless data sheet of different internal to Windows operation system values. Most of the data is beyond of human mind to understand. But there are some values that make sense. Those are for example the associations of the file types with the software applications. It is possible to manually tweak the Registry to break *.PDF file association with Acrobat Reader and open PDFs with any other application of choice (not that another application will open it. A double clicking a PDF file in a file browser will start the application that is being defined in Registry as a default application to open PDF document). Needless to say since Registry is hidden from the user by default (so they wouldn't mess with it) Microsoft has supplied the user with a number of tools to modify the values in Registry indirectly. One of the ways to modify the Registry values is to write a simple text file (in Notepad for example) and to save it with the *.reg file extension. Double clicking such *.reg file will make Windows attempt to make the changes to Registry. If *.reg file script file was filled properly (with a proper syntax) the result of the *.reg file execution are the updated values in Registry.
While defining a CUSTOM PROTOCOL HANDLER we would want to use Windows Registry as well. We choose any word we like to be used as a "protocol". Then we associate or "link" this word of choice to the executable program file. The syntax for the registry script can be observed in RV Player rvlink.reg scrip file.
Here is the syntax:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\rvlink]
@="URL:RV Protocol"
"URL Protocol"=""
[HKEY_CLASSES_ROOT\rvlink\DefaultIcon]
@="rv.exe,1"
[HKEY_CLASSES_ROOT\rvlink\shell]
[HKEY_CLASSES_ROOT\rvlink\shell\open]
[HKEY_CLASSES_ROOT\rvlink\shell\open\command]
@="\"C:\\Program Files\\Tweak\\RV-3.12.12-64\\bin\\rv.exe\" \"%1\""
The second line in this script is what defines the PROTOCOL name: [HKEY_CLASSES_ROOT\rvlink]
In this particular case the name of protocol is defined as "rvlink"
The last line of the *.reg script defines the file path to the executable program file (in this case it is rv.exe):
@="\"C:\\Program Files\\Tweak\\RV-3.12.12-64\\bin\\rv.exe\" \"%1\""
All you need to do is to double click the *.reg script file. And the Registry will be updated.
Now any Web browser will look for C:\Program Files\Tweak\RV-3.12.12-64\bin\rv.exe executable as soon as beginning of
URL address typed into URL Address fields starts with "rvlink" (in place where you usually use "http"
So, for example, if you instead of typing into the URL field of Web Browser:
you type:
rvlink://www.google.com
the Web Browser assumes that RV player needs to be used to open (or to resolve) the "www.google.com" address.
RV player (rv.exe) is opened. But nothing is being played back since www.google.com is not the path to the movie file or the image sequence.
But if you type into the URL Address field something like:
rvlink://C:/Users/theUser/Documents/maya/projects/fluid_render/images/frame_01.1.png
RV player reads an image file frame_01.1.png from the local drive from the location specified in file path:
C:/Users/theUser/Documents/maya/projects/fluid_render/images
From playing with RV player and "rvlink" Custom Protocol Handler I assume that a Web Browser
processes the URL address after it is typed into URL field (or if URL link was clicked) in two parts.
First the Web Browser determinates what application is used with supplied Custom Protocol Handler that starts the
URL address typed into the URL field. If it HTTP the web browser proceeds recolving the URL address by itself.
If the Protocol handler is ITMS then a Web Browser via Operation System (using Registry data values and its associations) opens iTunes and the rest of URL address (everything followed after Protocol Handler) is sent or forwarded to iTunes to be processed or resolved.
Same with RVLINK Protocol Handler which will instruct OS to open rv.exe file path to which is defined in Registry.
And so on.
Now excited by all of these I have created a Custom Handler Protocol for Python.exe. I called this protocol as Python_Protocol.
I have uploaded the python file to a web server with the URL address: www.myCoolWebSite.com/my_python_script.py
Then I typed into the web browser URL address field:
Python_Protocol://www.myCoolWebSite.com/my_python_script.py
and hit enter. And nothing happened. All I saw was a blink of the Python window with some error message I couldn't read since it was so fast.
Needless to say that if I run the very same script from a normal command prompt using a "traditional" appoach:
python c:/my_python_script.py
the script runs just fine.
This is a half of the battle which I didn't win yet. I don't understand how Shotgun would pass the data values I need to Python?
Let's imagine I have a Custom Action Menu in Shotgun which when clicked runs a Python script from a web location such as:
www.myCoolWebSite.com/my_python_script.py
As I understand all that would happen here (if it works at all) is that Python will be started, the my_python_script.py script will be downloaded from
www.myCoolWebSite.com and the script will be executed or run. That's it. I don't see here how Shotgun or when Shotgun is passing the arguments to Python (such as file path to image sequence or the name of the shot the Action Menu Item was executed). Please explain the mechanics of it. When and how Shotgun is controlled to pass the data to the variables defined in Python Script? Or what would be the way to make sure Python is pulling the data from the shot from where the Custom Action Menu was executed? What makes Shotgun to send the data to Python Variables declared in python.py script what is downloaded and run from a web location and run by python.exe? How Shotgun passes the data from Shotgun is remaining to be unclear.