XBMC & Python

IntroductionPython is an interpreted language which is used for stacks of things, but for me, the main thing is that Xbox Media Center (XBMC) uses Python for it’s extension scripts.

XMBC is the best thing you can run on your Xbox; it allows playback of virtually any type of media on your TV. Extensions for XBMC allow it to do even more things, such as read streams of pictures from Flickr, view TV listings, listen to radio stations around the world, or read Dilbert.

Developing extensions for XBMC could be a bit of a pain; edit the script, copy over to the Xbox, then test. That could be quite time consuming. However, there is an XBMC emulator for the PC (details below) which helps take the pain out of developing extensions.

Installing Everthing You Need

  1. Install Python [ http://www.python.org/download/ ]
  2. Install PIL (image libraries) for Python [ http://www.pythonware.com/products/pil/ ]
  3. Install the XBMC emulator (from the modules section) [ http://www.xbmcscripts.com/ ]

Running an existing XMBC .py script

There are a few changes you need to make before a script will work in the emulator, this is all covered in the emulator’s manual, but here are the key changes:

– Add this after the imports…
Emulating = “Emulating” in dir(xbmcgui)

– And this in the main def __init__(self)
if Emulating: xbmcgui.Window.__init__(self)

Now you’re all set to try the script; run Python from the command line: python <scriptname>

References to the Xbox HD

Script that reference resources on the Xbox HD assume that XMBC scripts are installed on the Q drive, in a folder called Scripts. The emulator manual tells you how to set up your PC to emulate this, but I prefer to use different variables in my code, e.g.

#IMAGEFOLDER = “q:\\scripts\\CallerID\\”
IMAGEFOLDER = “c:\\win32app\\Python24\\”
IMAGEDIALOG = IMAGEFOLDER + “dialog.png”
IMAGEFRAME = IMAGEFOLDER + “frame.png”
IMAGEDEFAULT = IMAGEFOLDER + “default.jpg”

Before I’d deploy to the Xbox I comment out one line, and uncomment the other.

Auto-Running the Script

You can auto-start scripts when XBMC first runs by creating autoexec.py in the XBMC directory. Here’s an example:

# auto execute scripts when xbmc starts, place this file in xbmchome\scripts\
# note: – do not execute more than one script at a time which asks for user input!
# import xbmc
xbmc.executescript(‘q:\\scripts\\medusa\\start_medusa.py’) xbmc.executescript(‘q:\\scripts\\StartUpMP3\\mp3.py’)

For more information please see the XBMC manual page.

Extending the Caller Id Script

In the end, there wasn’t much to write about this. I ended up stripping more stuff out rather than coding things in. It was all to do with how image caching just wasn’t working with the threaded way that windows were being created/destroyed.

The new script does however detect whether the player is actually playing back a file & pauses it so you can take the call :-D

You’ll find this script (named CallerIdLite) in the Communications area of the XBMC Script installer (or XBMCScripts.com).

Publishing the Script

Publishing the script so that it’s visible in the XBMC Script installer is pretty simple. Get an account at XBMCScripts.com and you can get your script validated & released from there. It’s a great site, and the guys who run it are pretty cool.

Leave a comment