Python, OSC and QLab

Installing Homebrew

This part is pretty simple. You need to begin by opening the terminal. This can be found in the utilities folder in Applications or by using spotlight.

It’s as simple as copy and pasting this into the open terminal window:


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;">ruby </span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: #000000;">e </span><span class="str" style="color: #008800;">"$(curl -fsSL </span><span style="color: #f8701f; text-decoration: none;">[[https://raw.github.com/mxcl/homebrew/go]]</span><span class="str" style="color: #008800;">)"'</span></span>

Installing Python3

This is also pretty simple - thanks to the magics of homebrew! Copy and paste (or type) this into your terminal window:


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;"> brew install python3</span></span>

Installing python-osc

This is the last bit of the puzzle - the magic program that allows us to send OSC commands within python! This worked for me - but didn’t work for Mark. If this (simpler) way doesn’t work I’ve included the alternative way as well.

Nice Way

Copy and paste (or type) this into your terminal window:


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;"> sudo pip3 install python</span><span class="pun" style="color: #666600;">-</span><span class="pln" style="color: #000000;">osc</span></span>

You’ll need to type in your password when prompted after this.

Not so Nice Way

  1. Download the ‘source code’ from this site.
  2. Once you’ve extracted the zip file run this from the terminal: cd ~/Downloads/python-osc-1.2
  3. And then this… sudo python setup.py install

Fun Stuff Time!

Alright so you’ve got your basic environment set up. I’m not going to go through the basics of python, and all that jazz, as there’s plenty of much better writers out there that have already covered this a million times over (once again, google it!). We’re just going to jump straight in.

Here’s all the boring boilerplate code. This basically asks for the IP address of the QLab machine, sets it to localhost by default. It also builds the OSC ‘client’


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="kwd" style="color: #000088;">from</span><span class="pln" style="color: #000000;"> pythonosc </span><span class="kwd" style="color: #000088;">import</span><span class="pln" style="color: #000000;"> osc_message_builder
</span><span class="kwd" style="color: #000088;">from</span><span class="pln" style="color: #000000;"> pythonosc </span><span class="kwd" style="color: #000088;">import</span><span class="pln" style="color: #000000;"> udp_client
 
ip </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: #000000;"> input</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"IP Address of QLab Machine (127.0.0.1): "</span><span class="pun" style="color: #666600;">)</span><span class="kwd" style="color: #000088;">ifnot</span><span class="pln" style="color: #000000;"> ip</span><span class="pun" style="color: #666600;">:</span><span class="pln" style="color: #000000;">
ip </span><span class="pun" style="color: #666600;">=</span><span class="str" style="color: #008800;">'127.0.0.1'</span><span class="pln" style="color: #000000;">
 
client </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: #000000;"> udp_client</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">UDPClient</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">ip</span><span class="pun" style="color: #666600;">,</span><span class="lit" style="color: #006666;">53000</span><span class="pun" style="color: #666600;">)</span></span>

Alright now the fun stuff: actually sending the OSC messages. The documentation for all the messages for QLab is available here. This also includes how to select the specific cues you want.

Once you’ve found the address that you want it’s as simple as running


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;">msg </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: #000000;"> osc_message_builder</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">OscMessageBuilder</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">address </span><span class="pun" style="color: #666600;">=</span><span class="str" style="color: #008800;">"YOUR ADDRESS HERE"</span><span class="pun" style="color: #666600;">)</span></span>

For example, to create a new cue we run


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;">msg </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: #000000;"> osc_message_builder</span><span class="pun" style="color: #666600;">.</span><span class="typ" style="color: #660066;">OscMessageBuilder</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">address </span><span class="pun" style="color: #666600;">=</span><span class="str" style="color: #008800;">"/new"</span><span class="pun" style="color: #666600;">)</span></span>

This is fine for simple addresses, but what if you wanted to to create say a video cue? That requires ‘arguments’ which are added by using the following command:


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;">msg</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">add_arg</span><span class="pun" style="color: #666600;">(</span><span class="str" style="color: #008800;">"video"</span><span class="pun" style="color: #666600;">)</span></span>

You can add as many arguments as you want this way - just repeat the command over and over until you have everything you want.

Finally when you’ve added all your arguments you need to ‘build’ the message and then send it off. This is achieved by using the following commands:


<span style="background-color: #ffffff; color: #282828; font-size: 18px;"><span class="pln" style="color: #000000;">msg </span><span class="pun" style="color: #666600;">=</span><span class="pln" style="color: #000000;"> msg</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">build</span><span class="pun" style="color: #666600;">()</span><span class="pln" style="color: #000000;">
client</span><span class="pun" style="color: #666600;">.</span><span class="pln" style="color: #000000;">send</span><span class="pun" style="color: #666600;">(</span><span class="pln" style="color: #000000;">msg</span><span class="pun" style="color: #666600;">)</span></span>

Wrap-up

These are the basics of using OSC, Python and QLab together to create awesome programs. The opportunites are limitless - you could program a R-PI with a button to change the text overlay of a video based on how quickly they press the button quite easily. It really opens up QLab to be a much more dynamic piece of software for everyone.

I’ve uploaded the full script that I based this post on @ my Github