Getting Started with Multi-Touch Development in Python

I have noticed a lot of tutorials dealing with Multi-Touch in C++, C#, or AS3.  There seems be a lack of Python tutorials, but there is a growing number of people wanting to use Python for Multi-Touch apps.    I intend to change that with a set of tutorials to teach people how to write Multi-Touch Apps in Python, starting with setting up the environment, all the way to writing a PhotoApp clone with Python-Lux(which is not released yet, and i’m not going to tell you when it will be released in any of these guides.  If thats all you came for, sorry I disappointed you).

These tutorials assume basic knowledge of Python, and at times may use some obscure feature of the Standard Library.  If you have any questions regarding something you don’t understand, or anything else about Python Development and such, feel free to email me at xelapond @ gmail . com, or PM me on nuigroup(username xelapond).  I also usually hang around IRC, #nuigroup, username xelapond.  I am happy to assist you with any problems you are having, and I would love feedback on how well this tutorial goes.

NOTE:  A lot of this is going to change soon with the release of Python-Lux.  Lux is a multimedia framework tuned for Multi-Touch Multi-Model interactions.  Once it is released you will not need to use the TouchPy callbacks for 90% of apps.  They are still nice to know though, for any application you don’t intend on using Python-Lux for.

NOTE 2:  WordPress appears to have killed the indentation on the code snippets.  In your code, follow standard Python tabbing and you should be fine.

This guide assume you have the following:

  • Working TUIO Source(TUIO Sim, Touchlib, tbeta)
  • A working TouchPy installation.  Though there are many great TUIO parsers for Python out there, my favorite is TouchPy.  I think it is the most capable, and easiest to use in a client application.  It requires no knowledge of how TUIO works.
  • Python 2.5.2(I Haven’t tested it with any other version, let me know if it works)

This guide was written from a GNU/Linux Standpoint, but most(if not all) if the code should work on Mac OS X or Windows.  I do not know if it is possible to install liblo(needed for TouchPy) on Windows, so if you managed to do it please let me know how:)

So, lets get started!

cd(or use a GUI if you like) into your TouchPy directory.  If you deleted it after you installed, no big deal, just check it out again.  If you do have one laying around, make sure it is svn revision 65 or later.  Copy(not move) the skeleton.py file to wherever you would like to keep your Multi-Touch programs.  I usually put programs I write as the result of a tutorial in ~/Programming/Practice.  But that is a matter of personal preference, it doesn’t matter where you put it.  Name it hellomt.py.

So, looking at this file, what do we see.

#! /usr/bin/python -u
”’
This is a Skeleton file.  It provides a
template to be used when writing a new
TouchPy application.
”’

from touch import *

class Observer(object):
def __init__(self, subject):
subject.push_handlers(self)

class touch_up(Observer):
def TOUCH_UP(self,blobID, xpos, ypos):
#Do something here

class touch_down(Observer):
def TOUCH_DOWN(self,blobID):
#Do something here

class touch_move(Observer):
def TOUCH_MOVE(self,blobID):
#Do something here

t = touchpy()
tu = touch_up(t)
td = touch_down(t)
tm = touch_move(t)

try:
while True:
t.update()

except (KeyboardInterrupt, SystemExit):
del t

Some of this should already make sense to you, but I am going to explain it line by line anyway.

from touch import *

Imports the entire TouchPy library into the local namespace.

class Observer(object):
def __init__(self, subject):
subject.push_handlers(self)

You can safely ignore this for now.  We will get to it in later tutorials.

class touch_up(Observer):
def TOUCH_UP(self,blobID, xpos, ypos):
#Do something here

This is the code that will run when a blob is “upped”, or removed from the surface.

class touch_down(Observer):
def TOUCH_DOWN(self,blobID):
#Do something here

This is the code when a blob is “downed”, or placed on the surface.

class touch_move(Observer):
def TOUCH_MOVE(self,blobID):
#Do something here

This is the code that is run when a blob is moved.

t = touchpy()
tu = touch_up(t)
td = touch_down(t)
tm = touch_move(t)

This code is there so that with one command(t.update()) you can dispatch any changes in blobs to the appropriate callbacks.  It will be explained in more detail in later parts of the tutorial.

try:
while True:
t.update()

except (KeyboardInterrupt, SystemExit):
del t

This code pretty much says, “While the program is running, keep tracking and reporting blob data.  Otherwise, delete everything and exit”.  There are many different ways to do this same chunk of code, and in later parts we will change this to a gobject.timeout, and many other scheduling techniques.

Now that you [hopefully] understand at least some of the TouchPy front end, lets get coding!

In the touch_up callback, delete the comment and add:

print ‘Something Left the Table!’

in the touch_move callback, delete the comment and add:

print ‘Something Moved on the Table!’

and in the touch_down callback, delete the commend and add:

print ‘Something was Placed on the Table!’

Now open your TUIO source(Touchlib, TUIO Sim).  Open a terminal, cd into the directory with hellomt.py, and run:

python ./hellomt.py

Then touch the surface.  If all goes well it should display each message when each event happens.  If IPv6 is enabled on your liblo installation, then it may take a few seconds to start up.  Give it at least 15 seconds before deciding it doesn’t work.

Assuming all went well, now you have written your first Multi-Touch application in Python!  Its not terribly useful, but its a great basis for more useful apps.  In the next tutorial(probably going to be posted tomorrow), we will get into blobIDs and coordinates of where touch events occur, then write a program using Clutter to draw circles on each point.

5 comments so far

  1. gally on

    Thx a lot, for this. I’ll take the time to digg this, python looks really efficient.

  2. Larky on

    Awesome, been looking for something like this for some time. Many thanks. 🙂

  3. xelapondsstuff on

    Glad I could help! I will be posting a second one tomorrow morning.

  4. […] Getting Started with Multi-Touch Development in Python: Part 2 Posted August 26, 2008 Filed under: DIY, Multi-Touch, Python, Software, Tutorial | This is the second part in an endless series of tutorials dealing with Multi-Touch Development in Python.  If you have not read the first one yet, it can be found here. […]

  5. Erik on

    Looks really nice, great tutorial and I will definitely be looking into developing in Python instead of continuing in C++


Leave a comment