User:Pline: Difference between revisions

From Official Kodi Wiki
Jump to navigation Jump to search
(Blanking this, since I moved it to Audio/Video tutorial)
 
(24 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This is a staging area for some rewrites to the Python add-on documentation. Ok, let's go!


= Python add-ons =
== Hello, World! ==
The simplest form of Python add-on is one that gets run, adds some list items, and exits to let XBMC take over the navigation; people who have written server-side code for the web should be familiar with how this works. (More complex add-ons can process user input in real-time, but we'll discuss those later.) Let's start by looking at an extremely basic example script:
<source lang="python">
import sys
import xbmcgui
import xbmcplugin
addon_id = int(sys.argv[1])
url = 'http://localhost/some_video.mkv'
li = xbmcgui.ListItem('My First Video!', iconImage='DefaultVideo.png')
li.setInfo(type='video')
xbmcplugin.addDirectoryItem(handle=addon_id, url=url, listitem=li)
xbmcplugin.endOfDirectory(addon_id)
</source>
While not particularly exciting, this shows the minimal amount of code you need to get an XBMC add-on up and running in Python. Most important is that we first create an <code>xbmcgui.ListItem</code> with the name and icon we want (<code>'DefaultVideo.png'</code> comes from the XBMC skin), set it to be a video item, and then add it to the directory. Once we're done adding items, we need to be sure to call <code>xbmcplugin.endOfDirectory</code> to let XBMC know we're done!
== The entry point ==
As we saw above, XBMC passes some arguments to us via <code>sys.argv</code>. This is important, since it's what will let us tailor the output on the add-on based on user input. Remember, much like a web site, each folder (or page) in an XBMC add-on is the result of a separate invocation of our script. The arguments available to us are:
{| class="wikitable"
! Index !! Description
|-
| 0 || The base URL of your add-on, e.g. <code>'plugin://plugin.video.myaddon/'</code>
|-
| 1 || The process handle for this add-on, as a numeric string
|-
| 2 || The query string passed to your add-on, e.g. <code>'?foo=bar&baz=quux'</code>
|}
Of particular interest is <code>sys.argv[2]</code>, which is what we'll generally use to show different things in the add-on.

Latest revision as of 00:33, 18 December 2013