• 1
  • 2(current)
  • 3
  • 4
  • 5
  • 15
Your 1st Add-On: Hello World!
#16
The Code is:

PHP Code:
import sys
import urllib
import urlparse
import xbmcgui
import xbmcplugin
import urllib2
import socket
import xbmcaddon
import datetime
import re
import os

addon       
xbmcaddon.Addon()
addonname   addon.getAddonInfo('name')
base_url sys.argv[0]
addon_handle int(sys.argv[1])
args urlparse.parse_qs(sys.argv[2][1:])


line1 ''
line2 "We can write anything we want here"
line3 "Using Python"

xbmcplugin.setContent(addon_handle'movies')

def build_url(query):
    return 
base_url '?' urllib.urlencode(query)

mode args.get('mode'None)

if 
mode is None:
    
url build_url({'mode''folder''foldername''Folder One'})
    
li xbmcgui.ListItem('Folder One'iconImage='DefaultFolder.png')
    
xbmcplugin.addDirectoryItem(handle=addon_handleurl=url,
                                
listitem=liisFolder=True)

    
url build_url({'mode''folder''foldername''Folder Two'})
    
li xbmcgui.ListItem('Folder Two'iconImage='DefaultFolder.png')
    
xbmcplugin.addDirectoryItem(handle=addon_handleurl=url,
                                
listitem=liisFolder=True)

    
url build_url({'mode''folder''foldername''Folder Three'})
    
li xbmcgui.ListItem('Folder Three'iconImage='DefaultFolder.png')
    
xbmcplugin.addDirectoryItem(handle=addon_handleurl=url,
                                
listitem=liisFolder=True)

    
url build_url({'mode''folder''foldername''Folder Four'})
    
li xbmcgui.ListItem('Folder Four'iconImage='DefaultFolder.png')
    
xbmcplugin.addDirectoryItem(handle=addon_handleurl=url,
                                
listitem=liisFolder=True)

    
xbmcplugin.endOfDirectory(addon_handle)


elif mode[0] == 'folder':
    
foldername args['foldername'][0]
    
url 'http://localhost/some_video.mkv'
    
li xbmcgui.ListItem(foldername ' Video'iconImage='DefaultVideo.png')
    
xbmcplugin.addDirectoryItem(handle=addon_handleurl=urllistitem=li)
    
xbmcgui.Dialog().ok(addonnameline1line2line3)
    
xbmcplugin.endOfDirectory(addon_handle



So How can i link a video with the item "Folder Three video" ?
I thought it has something to do with the url but changeing the url has no reaction.
Reply
#17
I have been trying to work through the tutorial at HOW-TO:Write_Python_Scripts_for_XBMC (wiki) as it seems to give much more detail than the hello world one. On the face of it the tutorial doesn't look too old as the page is shown as last updated in April 2014.

However when I work through the examples my results are quite different to what is expected.

The 2 major issues are the screen layouts and one particular keyboard action.

1. Screen layout - the windows created by the examples are way too small and only show a small part of the labels required on the screen. Changing the screen layouts is not too difficult so I am ok with this.

2. The biggest problem is the keyboard action of BACK which is included in almost all the examples. As the tutorial states you need the BACK action to exit the scripts or the device will just be in a continuous loop and require a reboot to continue. My BACK action works fine for all Gotham scripts from keyboard or remote control but not for the examples in the tutorial.

So for the following code I change ACTION_PREVIOUS_MENU = 10 to ACTION_PREVIOUS_MENU = 1 which becomes LEFT arrow key to exit the program.

PHP Code:
import xbmcxbmcgui
 
#get actioncodes from https://github.com/xbmc/xbmc/blob/master/xbmc/guilib/Key.h
ACTION_PREVIOUS_MENU 10
ACTION_SELECT_ITEM 
7
 
class MyClass(xbmcgui.Window):
  
def __init__(self):
    
self.strActionInfo xbmcgui.ControlLabel(100120200200'''font13''0xFFFF00FF')
    
self.addControl(self.strActionInfo)
    
self.strActionInfo.setLabel('Push BACK')
 
  
def onAction(selfaction):
    if 
action == ACTION_PREVIOUS_MENU:
      
self.close()
    if 
action == ACTION_SELECT_ITEM:
      
self.message()
 
  
def message(self):
    
dialog xbmcgui.Dialog()
    
dialog.ok(" My message title"" This is a nice message ")
 
mydisplay MyClass()
mydisplay .doModal()
del mydisplay 

The references to press A or B etc are also no longer relevant and are generally covered by ENTER.

I use various addons that use ACTION_PREVIOUS_MENU = 10 and they work fine so my first question is why doesn't ACTION_PREVIOUS_MENU = 10 work for the tutorial examples (thus requiring the mapping of a different key to exit the script)? Will ACTION_PREVIOUS_MENU = 10 still work when Helix is released and is there a schedule somewhere of the Gotham to Helix changes?

I have very basic understanding of Python but I have been able to work out the json calls to run addon scripts etc. Do you know of any up to date guide that would show me the Python code to play a particular video stream? It is just a single stream from a given website and the video is in mp4 format. The video is only a couple of minutes long and from the tutorials I know how to download the mp4 file, but not the code to then play the video. However I don't really want to download it as I would prefer to simply play the stream in Kodi. Could someone please direct me towards a tutorial that would show me how to do this?

Thanks.
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#18
I received a PM following my last post and thought I would add a few details here.

Unless anyone knows otherwise the xbmc and xbmcgui libraries are only available within xbmc so they can't be used within your normal Python interface.

The only way I have found of debugging the scripts is via the xbmc.log and I see now why addon authors are so insistent on seeing your logs when their scripts fail. I knew that the logs were useful but hadn't realised that they also provide a reasonable debugging tool. For Raspbmc the log is in .xbmc/temp directory.

So after each failed script attempt I pull up the log with WinSCP and Notepad++. Not as intuitive as a regular Python IDE but better than nothing.

Before working through some of the tutorials I contacted the author of a basic addon to ask if I could modify it for my own use. I informed the author that I would make reference to his work within my modified addon and he was fine with that. Many of the addons are OpenSource and available for download on Github so they provide a useful learning tool but if you are going to use them please contact the author for their approval.

I have made reasonable progress over the last 24 hours and I am now able to play the online video via Kodi. I am currently playing the video via a json call rather than my basic addon but it should be easy enough now to modify the addon to play the video.

I still don't know what has changed since the tutorial was written that breaks ACTION_PREVIOUS_MENU = 10 and look forward to receiving advice from the experienced codes on the forum in due course.
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#19
If you are testing your development on the Pi, then I recommend using Putty to ssh in to the Pi and track the xbmc.log using tail.
PHP Code:
tail -/home/pi/.xbmc/temp/xbmc.log 
This will track the changes to the log in realtime. Much easier than constantly opening the log in a text editor.

Also, if you need to reboot Kodi because it is stuck, then type this into putty:
PHP Code:
sudo initctl stop xbmc 
then
PHP Code:
sudo initctl start xbmc 
Reply
#20
(2014-12-17, 10:49)Karnagious Wrote: If you are testing your development on the Pi, then I recommend using Putty to ssh in to the Pi and track the xbmc.log using tail.
Code:
tail -f /home/pi/.xbmc/temp/xbmc.log
This will track the changes to the log in realtime. Much easier than constantly opening the log in a text editor.

Also, if you need to reboot Kodi because it is stuck, then type this into putty:
Code:
sudo initctl stop xbmc
then
Code:
sudo initctl start xbmc

Just used your 3 pieces of 'code', excellent. Thanks.
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#21
pieman i'm also new in xbmc python addon development i know basic stuff mind we share ideas together? facebook or gtalk i'll PM you my details

I have an add-on that plays a m3u8 for now, what I'm moving on to next is creating folders using lists and regular xpression for web scraping and i think i'm getting a hang of it
Yeah, Me, Myself, and I, The Three Musketeers
Image
Reply
#22
Please do post more examples in this thread as you learn.

There have already been some excellent extensions of hello world.

Anyone else want to post something?
Reply
#23
(2014-12-18, 11:38)zag Wrote: Please do post more examples in this thread as you learn.

There have already been some excellent extensions of hello world.

Anyone else want to post something?

PHP Code:
tail -/home/pi/.xbmc/temp/xbmc.log
sudo initctl stop xbmc
sudo initctl start xbmc 

From Karnagious has certainly made it MUCH easier for me, and no doubt MANY other beginners.

I am still struggling with this ACTION_PREVIOUS_MENU = 10 stuff.

The following, 4 year old, code is from https://www.assembla.com/spaces/oniontv-...nd_plugins still works fine EXCEPT it will not exit with BACK and window / control items are wrongly sized. Changing 10 to 1 (LEFT key is fine)

PHP Code:
import xbmcxbmcgui
#get actioncodes from keymap.xml
ACTION_PREVIOUS_MENU 10

class MyClass(xbmcgui.Window):
    
def __init__(self):
        
self.strActionInfo xbmcgui.ControlLabel(100120200200'''font13''0xFFFF00FF')
        
self.addControl(self.strActionInfo)
        
self.strActionInfo.setLabel('Push BACK to quit')
        
self.button0 xbmcgui.ControlButton(3505008030"HELLO")
        
self.addControl(self.button0)
        
self.setFocus(self.button0)

    
def onAction(selfaction):
        if 
action == ACTION_PREVIOUS_MENU:
            
self.close()

    
def onControl(selfcontrol):
        if 
control == self.button0:
            
self.message('you pushed the button')

    
def message(selfmessage):
        
dialog xbmcgui.Dialog()
        
dialog.ok(" My message title"message)

mydisplay MyClass()
mydisplay .doModal()
del mydisplay[/code]

So the working code becomes:

[
code]import xbmcxbmcgui

#get actioncodes from keymap.xml
ACTION_PREVIOUS_MENU 1

class MyClass(xbmcgui.Window):
    
def __init__(self):
        
self.strActionInfo xbmcgui.ControlLabel(100120800200'''font13''0xFFFF00FF')
        
self.addControl(self.strActionInfo)
        
self.strActionInfo.setLabel('Push LEFT arrow key to quit')
        
self.button0 xbmcgui.ControlButton(35050038030" Welcome to Kodi scripting, press OK now. ")
        
# control button text will scroll right to left if it is too big for the button
        
self.addControl(self.button0)
        
self.setFocus(self.button0)

    
def onAction(selfaction):
        if 
action == ACTION_PREVIOUS_MENU:
            
self.close()

    
def onControl(selfcontrol):
        if 
control == self.button0:
            
self.message('Congratulations you pushed the button, now press OK and then the LEFT arrow key to quit this basic Kodi script. ')

    
def message(selfmessage):
        
dialog xbmcgui.Dialog()
        
dialog.ok(" Your first Kodi script"message)

mydisplay MyClass()
mydisplay .doModal()
del mydisplay 

I still see ACTION_PREVIOUS_MENU = 10 working in modern scripts so why does it no longer work in the script above? Obviously 4 years is along time in software development terms so we would appreciate any tips or guides on the changes to Kodi in recent years.

For reference I am working with Gotham but will also want my scripts to work in Helix.
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#24
A useful tactic in debugging is to print the value of variables to the log.
PHP Code:
def onAction(selfaction):
        print 
action
        
if action == ACTION_PREVIOUS_MENU:
            
self.close() 

With the live logging you can immediately see what the value of the action is when you press PREVIOUS MENU.

But the way, every action has an ID, and the IDs are described here: https://github.com/xbmc/xbmc/blob/master...ilib/Key.h
Reply
#25
(2014-12-17, 10:49)Karnagious Wrote: If you are testing your development on the Pi, then I recommend using Putty to ssh in to the Pi and track the xbmc.log using tail.
PHP Code:
tail -/home/pi/.xbmc/temp/xbmc.log 
This will track the changes to the log in realtime. Much easier than constantly opening the log in a text editor.

Also, if you need to reboot Kodi because it is stuck, then type this into putty:
PHP Code:
sudo initctl stop xbmc 
then
PHP Code:
sudo initctl start xbmc 

Karnagious I am trying hard to catch up with you and pieman but I am stuck with your post above. I am using ATV2 and can SSH in and find the XBMC log but I don't know how to make use of the commands you provided above. How do I use tail and where within XBMC do I need to be in order to stop or start XBMC using the command line in my SSH client?

ALso when I am testing just using XBMX on my PC laptop are these command useful? Normally I just open the XBMC log using Notepad ++ and it seems to update each time I do something within XBMC.

Sorry for these dumb questions.
Reply
#26
(2014-12-19, 15:59)LandRover Wrote: Karnagious I am trying hard to catch up with you and pieman but I am stuck with your post above. I am using ATV2 and can SSH in and find the XBMC log but I don't know how to make use of the commands you provided above. How do I use tail and where within XBMC do I need to be in order to stop or start XBMC using the command line in my SSH client?

ALso when I am testing just using XBMX on my PC laptop are these command useful? Normally I just open the XBMC log using Notepad ++ and it seems to update each time I do something within XBMC.

Sorry for these dumb questions.

Just paste the tail command when you access command line in SSH and LIVE logging will appear in the terminal. tail is part of the OS and therefore available anywhere in xbmc (as are the start and stop xbmc commands). If you run something in xbmc you will see the log refreshing in your terminal window. Perhaps create an error in one of scripts / addons by removing a : or adding an incorrect indent, run the addon /script again and tail will automatically refresh the log to tell you which row created the error. MUCH better than manually opening the log in Notepad++

If PC laptop means Windows rather than Linux then you will need a Windows equivalent of tail. Google suggests you check http://stackoverflow.com/questions/18758...il-command

After a mammoth session I released my addon to the 'world' today. It plays a Christmas video so I had a deadline to get it finished.

I might take a look now at the variables in the following code as suggested by Karnagious to see why BACK fails in the older tutorials.

Code:
def onAction(self, action):
        print action
        if action == ACTION_PREVIOUS_MENU:
            self.close()
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#27
(2014-12-18, 15:22)Karnagious Wrote: A useful tactic in debugging is to print the value of variables to the log.
PHP Code:
def onAction(selfaction):
        print 
action
        
if action == ACTION_PREVIOUS_MENU:
            
self.close() 

With the live logging you can immediately see what the value of the action is when you press PREVIOUS MENU.

But the way, every action has an ID, and the IDs are described here: https://github.com/xbmc/xbmc/blob/master...ilib/Key.h

I am familiar with the key ID's and I now have the variable added to the log (with xbmc debugging on). It is not helping with this 'bug' because the ACTION_PREVIOUS_MENU gives the correct ID's (1 or 10) but not the correct action when set to 10 backspace.

However I have tracked down part of the problem, but not the fix:-)

With key ID 10, and the code in a permanent loop, it is recognised as backspace. But xbmc thinks I am requesting some kind of audio settings (I think) rather than trying to exit the code.

Code:
20:19:03 T:2918302784   DEBUG: Previous line repeats 1 times.
20:19:03 T:2918302784   DEBUG: CecLogMessage - >> POLL not sent
20:19:04 T:3036664608   DEBUG: Keyboard: scancode: 0x0e, sym: 0x0008, unicode: 0x0008, modifier: 0x0
20:19:04 T:3036664608   DEBUG: OnKey: backspace (0xf008) pressed, action is Back
20:19:04 T:3028268096    INFO: CActiveAESink::OpenSink - initialize sink
20:19:04 T:3028268096   DEBUG: CActiveAESink::OpenSink - trying to open device PI:HDMI
20:19:04 T:3028268096   DEBUG: CAESinkPi:SetAudioProps hdmi_stream_channels 0 hdmi_channel_map 00000008
20:19:04 T:3028268096   DEBUG: CAESinkPi:Initialize Format:15 Channels:2 Samplerate:44100 framesize:8 bufsize:17640 bytes/s=352800.00
20:19:04 T:3028268096   DEBUG: CAESinkPi:Initialize
20:19:04 T:3028268096   DEBUG: COMXCoreComponent::Initialize OMX.broadcom.audio_render input port 100 output port 100 m_handle 0xa3f9ff30
20:19:04 T:3028268096   DEBUG: COMXCoreComponent::AllocInputBuffers component(OMX.broadcom.audio_render) - port(100), nBufferCountMin(1), nBufferCountActual(2), nBufferSize(17640), nBufferAlignmen(16)
20:19:04 T:3028268096   DEBUG: CActiveAESink::OpenSink - SinkPi Initialized:
20:19:04 T:3028268096   DEBUG:   Output Device : HDMI
20:19:04 T:3028268096   DEBUG:   Sample Rate   : 44100
20:19:04 T:3028268096   DEBUG:   Sample Format : AE_FMT_FLOAT
20:19:04 T:3028268096   DEBUG:   Channel Count : 2
20:19:04 T:3028268096   DEBUG:   Channel Layout: FL,FR
20:19:04 T:3028268096   DEBUG:   Frames        : 2205
20:19:04 T:3028268096   DEBUG:   Frame Samples : 4410
20:19:04 T:3028268096   DEBUG:   Frame Size    : 8
20:19:04 T:2918302784   DEBUG: CecLogMessage - << Recorder 1 (1) -> TV (0): POLL
20:19:04 T:2918302784   DEBUG: CecLogMessage - << 10
20:19:05 T:2918302784   DEBUG: Previous line repeats 1 times.

Spent a few hours with Google trying to see why it is calling the audio settings rather than exiting the code but no joy yet.
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#28
(2014-12-19, 16:54)pieman Wrote: Just paste the tail command when you access command line in SSH and LIVE logging will appear in the terminal. tail is part of the OS and therefore available anywhere in xbmc (as are the start and stop xbmc commands). If you run something in xbmc you will see the log refreshing in your terminal window. Perhaps create an error in one of scripts / addons by removing a : or adding an incorrect indent, run the addon /script again and tail will automatically refresh the log to tell you which row created the error. MUCH better than manually opening the log in Notepad++

Thank you! I got the log the to work with my ATV2 and my PC

I am trying to do many things at the same time, working through my basic python tutorial, studying existing addons, and hopefully continuing to learn more here with the help of you and others.

Just to clarify for other ATV2 users, the path for the tail -f command is ...mobile/Library/Preferences/xbmc.log

I still don't understand why these scripts don't run in the Python IDE. For example when I run the original "Hello World, I get the following.

Python 3.3.4 (v3.3.4:7ff62415e426, Feb 10 2014, 18:13:51) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "C:\Users\Wayne\AppData\Roaming\XBMC\addons\plugin.program.hello.world-master\addon.py", line 1, in <module>
import xbmcaddon
ImportError: No module named 'xbmcaddon'
>>>

The addon does work as expected in XBMC.

Do I have something configured incorrectly in Python?



Thanks again.
Reply
#29
(2014-12-19, 21:23)LandRover Wrote: Just to clarify for other ATV2 users, the path for the tail -f command is ...mobile/Library/Preferences/xbmc.log

I still don't understand why these scripts don't run in the Python IDE. For example when I run the original "Hello World, I get the following.

ImportError: No module named 'xbmcaddon'

The addon does work as expected in XBMC.

Do I have something configured incorrectly in Python?

Thanks for adding the ATV2 path details and obviously they will be different for different set ups. Hopefully users can locate their xbmc.log and modify the tail command accordingly.

The modules that are prefixed with xbmc like xbmcgui and xbmc itself are not available in standalone Python.They will only run via XBMC. It maybe that the techies have worked out how to use them in an IDE but I certainly haven't found a way. It would be nice to use the IDE's we are familiar with but unless someone advises otherwise it is not possible. Even if you added the modules to Python I don't think they would work as they were developed to work within XBMC.

I would recommend that beginners (like me) take a look at PyXBMCt at http://forum.kodi.tv/showthread.php?tid=174859
It is a quick and easy way of creating windows and control items. It comes with it's own demo addon.
Read through the 9 pages of the thread as there are a few useful tips and study the pdf.
Currently the last post on the thread was written by me and it highlights the required set up (some of the setup is manual rather than the normal point and click of the XBMC interface).
Easy Home Automation to Control your RF device within Kodi XBMC
Want to watch TV everywhere you go? Pop by the Want To Watch TV site.
Image
Reply
#30
(2014-12-20, 14:15)pieman Wrote: Thanks for adding the ATV2 path details and obviously they will be different for different set ups. Hopefully users can locate their xbmc.log and modify the tail command accordingly.

The modules that are prefixed with xbmc like xbmcgui and xbmc itself are not available in standalone Python.They will only run via XBMC. It maybe that the techies have worked out how to use them in an IDE but I certainly haven't found a way. It would be nice to use the IDE's we are familiar with but unless someone advises otherwise it is not possible. Even if you added the modules to Python I don't think they would work as they were developed to work within XBMC.

I'll just have to get used to using the logs, as a beginner I find the IDE easier to find Python formatting issues Sad . Hopefully I figure it out as obviously everyone else has managed to do. Smile

Time to keep moving forward.
Reply
  • 1
  • 2(current)
  • 3
  • 4
  • 5
  • 15

Logout Mark Read Team Forum Stats Members Help
Your 1st Add-On: Hello World!5