1 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.
2 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:
The very latest XBMC python API documentation 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/ (possibly outdated)
- http://mirrors.xbmc.org/docs/python-docs/frodo/ (for XBMC Frodo)
- http://mirrors.xbmc.org/docs/python-docs/gotham/ (for XBMC Gotham)
3 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.
4 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).
5 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.
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.
6.1 List of built-in functions
You can call any of the XBMC Built-in Commands using the function xbmc.executebuiltin().
6.2 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.
See XBMC databases for database layout
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.
- See: InfoLabels
6.4 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.
Need to clean this up:
General Python Info:
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 + )
7 Environment details
XBMC Python is an implementation of Python 2.4 (specifically, 2.4.5 (#71, Nov 18 2008, 18:41:39) [MSC v.1310 32 bit (Intel)]).
XBMC Python comes with all the standard modules from Python 2.4. If you need to find a list of them, you can use the following command to generate a list of built-ins:
import sys print sys.builtin_module_names
For a list of the other standard libraries, find the file "python24.zlib" in your XBMC installations /system/python/ folder, and open it with an archive viewer such as WinRAR or 7-zip.
7.2.1 Custom libraries
In addition to the built-ins and standard libraries, XBMC Python uses a handful of custom libraries to expose XBMC functionality to Python.
Up to date documentation about these modules can be found at http://mirrors.xbmc.org/docs/python-docs/
|xbmc||Offers classes and functions that provide information about the media currently playing and that allow manipulation of the media player (such as starting a new song). You can also find system information using the functions available in this library.|
|xbmcgui||Offers classes and functions that manipulate the Graphical User Interface through windows, dialogs, and various control widgets.|
|xbmcplugin||Offers classes and functions that allow a developer to present information through XBMC's standard menu structure. While plugins don't have the same flexibility as scripts, they boast significantly quicker development time and a more consistent user experience.|
|xbmcaddon||Offers classes and functions that manipulate the add-on settings, information and localization.|
|xbmcvfs||Offers classes and functions offers acces to the Virtual File Server (VFS) which you can use to manipulate files and folders.|
7.2.2 Installing additional libraries
Additional libraries can be installed by adding them to the /system/python/Lib folder of your XBMC installation. A Python module placed in this folder can be called from any script or plugin within XBMC.
Of course, you can also place the library files you want to import in your script or plugin folder, directly in the root or in a subfolder. A popular method is to add a "resources" subfolder to your script's folder, and add it to the path within your script.
If you are going to use a module that will possibly used by more add-on you can better create a separate add-on of this so more add-ons can make use of it. Be sure to checkout which modules are already available from our XBMC repository by looking at http://mirrors.xbmc.org/addons/ and choose the XBMC version your are developing for.
8 See also