Python development

Out of Date

 * With the release of Eden 11.0, some function are out of date. Please view http://wiki.xbmc.org/index.php?title=Eden_API_changes for the latest on incorporating Python scripts.
 * With the release of Frodo 12.0, some function are out of date. Please view http://wiki.xbmc.org/index.php?title=Frodo_API_changes for the latest on incorporating Python scripts.

Addon system using python
XBMC includes a built-in Python interpreter that allows users to develop add-ons (scripts and plugins) that interface easily and cleanly with the XBMC dashboard. These add-ons can extend the functionality of XBMC without requiring extensive programming experience or ability. While you may not feel comfortable browsing the XBMC source code and submitting patches (or even bug reports), you can learn how to write a script or plugin with just a few hours' practice, using the information available in these pages.

This page is intended as an introduction to XBMC Python for new developers, and a quick reference for more experienced programmers. If you're not interested in programming, you might want to visit this page for information about installing and using Python add-ons as an end user. If you're already familiar with XBMC Python, you can probably skip on down to the environment details or the resource links below for quick reference material.

The main 'HOW-TO' articles for making your own plugins and scripts can be found here:
 * HOW-TO write plugins for XBMC
 * HOW-TO write Python Scripts

PyDocs
The very latest XBMC python API documentation "PyDocs" with classes and functions is automatically generated from XBMC's source code and can be found here (not all functions are available in previous versions):


 * http://mirrors.xbmc.org/docs/python-docs/ (Main index page)
 * http://mirrors.xbmc.org/docs/python-docs/stable/ (for stable release)
 * http://mirrors.xbmc.org/docs/python-docs/12.2-frodo/ (for 12.2 Frodo)
 * http://mirrors.xbmc.org/docs/python-docs/13.0-gotham/ (for 13.0 Gotham)

Python plugins versus scripts
Please do not confuse "Plugins" with "Scripts". Unlike the Scripts, Plugins are not meant to be directly invoked by the user. Instead, Plugins are automatically invoked when the user enters such a virtual folder. Do not try to run Plugins files from the scripts window as that will only give you a weird error message. Plugins, unlike Scripts, do not really provide new functionality to XBMC, instead what they do do is provide an easy way to present content listings in XBMC through the native GUI interface.

Script development
If you're new to Python programming (or just new to XBMC Python), the easiest way to get started is with a script. The traditional Hello World program, written as an XBMC Python script, would look like this:

print "Hello World!"

That's the same code you would enter at the Python command line, because XBMC runs a full-featured, standard Python interpreter (for more information concerning the current version number and included modules see the environment details below). If you're already familiar with Python programming, the only new challenge is learning the custom modules that allow you to gather information from XBMC and manipulate the Graphical User Interface (GUI).

There are some excellent tutorials available to introduce you to XBMC scripting (and Python in general). See the HOW-TO included in the XBMC Online Manual, or visit Alexpoet's XBMC Scripting site for a popular beginner's tutorial (PDF).

Plugin development
While scripts offer you flexibility and full control over the XBMC GUI, plugins allow you to quickly and consistently present information to the user through the standard XBMC menu structure.

When a user launches a plugin, the plugin generates a list of menu items and hands them to XBMC to draw on the screen (regardless of screen resolution, skin, or any other user setting). While plugin developers lose some amount of control over the presentation, they no longer have to make up their own UIs, or worry about creating a usable look and feel across multiple displays.

Plugins are most commonly used to scrape websites for links to streaming videos, displaying the video list in XBMC just like it would movie files on the local hard drive, but a plugin can be used anywhere a script could, as long as the menu structure is a sufficient GUI for the add-on's needs.

Also, note that a script can launch a plugin, and a plugin can launch a script (and, for that matter, it can call all the same functions available to a script) so the distinction is more theoretical than practical.

Resource links
While the three custom libraries give you control over the XBMC GUI, you'll eventually want to be able to interact with other parts of the system. Much of XBMC's functionality is exposed to Python, one way or another. These are some of the systems you can use to make your Python add-ons more useful.

Must read
A must read for developers are is style guide for writing python. This contains the do's and dont's when starting to write code.

List of built-in functions
You can call any of the XBMC Built-in Commands using the function xbmc.executebuiltin.

List of Built In Functions

XBMC databases (media metadata libraries)
XBMC uses Music and Video Libraries, stored in a SQL (SQLite) database, to store massive amounts of additional meta data concerning local media files (assuming the user has enabled the libraries and chosen the right Content and scraper for each of his sources). Although there's no direct access from Python to the XBMC Database, you can gain access to the databases from Python through the JSON RPC. Read more about that here.

InfoLabels
XBMC uses InfoLabels to display media file information (such as Artist and Album for a song, or Title and Cast list for a movie or TV show). This information shows up in the browsing menu (when using Library Mode), as well as in the Now Playing details when the media player is active.

You can get the currently-playing InfoLabel using the getInfoLabel function in xbmc. You can also set the InfoLabel information for any menu items you create in a plugin, making a much more versatile user experience.

Other python pages
There are several other pages in the XBMC Online Manual dedicated to Python development. You can find a comprehensive list by clicking "Python" in the Categories list at the bottom of any Python page. Definitely check out following pages with tons of great sample code.


 * HOW-TO write Python Scripts


 * HOW-TO:Write_Python_Scripts_for_XBMC


 * HOW-TO write plugins for XBMC


 * HOW-TO:Debug_Python_Scripts_with_Eclipse


 * HOW-TO:HelloWorld_addon


 * Script Lyrics


 * Script Sources


 * Add-on unicode path

Need to clean this up:


 * | GUIDE HOW TO ADD A LOGIN FUNCTION TO YOUR ADDON


 * Passion list of pydocs


 * |Code Snippet


 * python interface for google's JSON api


 * addons_xml_generator.py for repository owners


 * Tips for coding python scripts / plugins compatible with Linux, Mac, Windows AND Xbox


 * WindowXML GUI Toolkit (replaces GUIBuilder for XBMC python scripts for GUI coding)


 * HOW-TO write GUI settings for XBMC python plugins and scripts (addons)

General Python Info:


 * http://www.python.org/doc/


 * http://www.diveintopython.net/toc/index.html


 * http://pound-python.org/


 * http://stackoverflow.com/questions/tagged/python


 * http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#id10

A few useful libs for scraping websites:


 * http://pypi.python.org/pypi/simplejson/ (included in py2.6 + )


 * http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm (included in py2.6 + )


 * http://www.crummy.com/software/BeautifulSoup/


 * http://www.feedparser.org/


 * http://wwwsearch.sourceforge.net/mechanize/


 * http://wwwsearch.sourceforge.net/ClientForm/

Version
XBMC Python is an implementation of Python 2.6 or higher depending on platform.