Kodi Community Forum
Call function of another kodi addon - Printable Version

+- Kodi Community Forum (https://forum.kodi.tv)
+-- Forum: Development (https://forum.kodi.tv/forumdisplay.php?fid=32)
+--- Forum: Add-ons (https://forum.kodi.tv/forumdisplay.php?fid=26)
+--- Thread: Call function of another kodi addon (/showthread.php?tid=362488)



Call function of another kodi addon - M4tRiX - 2021-05-09

Hi,

so at the moment I have to addons. One service addon (flask webservice) and a programm addon. Now I want to call a function of my programm addon via the service addon.
I found this thread stackoverflow. But I can't change it for my project.
Have someone implemented sth. like this?

Thanks.


RE: Call function of another kodi addon - pkscout - 2021-05-09

That Stack Overflow description is if you want to import a function from another add-on, which is certainly an option, although I've never tried that.  The other option is to have your service add-on actually call the script with some parameters that would tell it to run and what chunk of your other add-on to run.  You can use xbmc.executebuiltin to run a RunScript command something like this:

python:

scripttorun = "RunScript(script.addon, function)"
xbmc.executebuiltin(scripttorun)

You'd have to build some logic into the receiving addon to parse that arg and figure out what to run. The information on the argument you pass is in sys.argv.

You can also pass a bunch of parameters, and as before, you responsible for parsing them in the receiving add-on:

python:

scripttorun = "RunScript(script.addon, foo=bar&another=thing)"
xbmc.executebuiltin(scripttorun)

The only thing to mention here is that you can't pass any data back to your service add-on with this strategy.  OK, you can, but you'd have to use window properties to do it. So it isn't the most straightforward thing.


RE: Call function of another kodi addon - M4tRiX - 2021-05-11

(2021-05-09, 20:59)pkscout Wrote: That Stack Overflow description is if you want to import a function from another add-on, which is certainly an option, although I've never tried that.  The other option is to have your service add-on actually call the script with some parameters that would tell it to run and what chunk of your other add-on to run.  You can use xbmc.executebuiltin to run a RunScript command something like this:

python:

scripttorun = "RunScript(script.addon, function)"
xbmc.executebuiltin(scripttorun)

You'd have to build some logic into the receiving addon to parse that arg and figure out what to run. The information on the argument you pass is in sys.argv.

You can also pass a bunch of parameters, and as before, you responsible for parsing them in the receiving add-on:

python:

scripttorun = "RunScript(script.addon, foo=bar&another=thing)"
xbmc.executebuiltin(scripttorun)

The only thing to mention here is that you can't pass any data back to your service add-on with this strategy.  OK, you can, but you'd have to use window properties to do it. So it isn't the most straightforward thing.

Hi, thanks. But unfortunately I need to get the response of my function to interact with the addon after running.
Nevertheless do you have an example for me to implement RunScript and how to parse it in the receiving addon?

What I have tried:
python:
       
arg1=1
arg2=2
scripttorun = "RunScript(storage/.kodi/addons/plugin1/addon.py, arg1 arg2)"
xbmc.executebuiltin(scripttorun, True)

In the addon.py I have tried the following:
python:

if len(sys.argv) is None:
    myfunction()
else:
    myfunction(sys.argv[0],sys.argv[1])



RE: Call function of another kodi addon - pkscout - 2021-05-12

(2021-05-11, 21:44)M4tRiX Wrote:
(2021-05-09, 20:59)pkscout Wrote: That Stack Overflow description is if you want to import a function from another add-on, which is certainly an option, although I've never tried that.  The other option is to have your service add-on actually call the script with some parameters that would tell it to run and what chunk of your other add-on to run.  You can use xbmc.executebuiltin to run a RunScript command something like this:

python:

scripttorun = "RunScript(script.addon, function)"
xbmc.executebuiltin(scripttorun)

You'd have to build some logic into the receiving addon to parse that arg and figure out what to run. The information on the argument you pass is in sys.argv.

You can also pass a bunch of parameters, and as before, you responsible for parsing them in the receiving add-on:

python:

scripttorun = "RunScript(script.addon, foo=bar&another=thing)"
xbmc.executebuiltin(scripttorun)

The only thing to mention here is that you can't pass any data back to your service add-on with this strategy.  OK, you can, but you'd have to use window properties to do it. So it isn't the most straightforward thing.

Hi, thanks. But unfortunately I need to get the response of my function to interact with the addon after running.
Nevertheless do you have an example for me to implement RunScript and how to parse it in the receiving addon?

What I have tried:
python:
       
arg1=1
arg2=2
scripttorun = "RunScript(storage/.kodi/addons/plugin1/addon.py, arg1 arg2)"
xbmc.executebuiltin(scripttorun, True)

In the addon.py I have tried the following:
python:

if len(sys.argv) is None:
    myfunction()
else:
    myfunction(sys.argv[0],sys.argv[1])

Check out Artist Slideshow (it’s one of mine). I’m not in front of my computer, so I can’t point you to where, but do a search for sys.argv and the parsing code should be near there.


RE: Call function of another kodi addon - fierygreywind - 2023-12-17

(2021-05-09, 20:59)pkscout Wrote: That Stack Overflow description is if you want to import a function from another add-on, which is certainly an option, although I've never tried that.  The other option is to have your service add-on actually call the script with some parameters that would tell it to run and what chunk of your other add-on to run.  You can use xbmc.executebuiltin to run a RunScript command something like this:

python:

scripttorun = "RunScript(script.addon, function)"
xbmc.executebuiltin(scripttorun)

You'd have to build some logic into the receiving addon to parse that arg and figure out what to run. The information on the argument you pass is in sys.argv.

You can also pass a bunch of parameters, and as before, you responsible for parsing them in the receiving add-on:

python:

scripttorun = "RunScript(script.addon, foo=bar&another=thing)"
xbmc.executebuiltin(scripttorun)

The only thing to mention here is that you can't pass any data back to your service add-on with this strategy.  OK, you can, but you'd have to use window properties to do it. So it isn't the most straightforward thing.

can you elaborate more on "but you'd have to use window properties to do it"? How can I pass data back to the service addon when using `RunScript`


RE: Call function of another kodi addon - scott967 - 2023-12-18

(2023-12-17, 04:13)fierygreywind Wrote: can you elaborate more on "but you'd have to use window properties to do it"? How can I pass data back to the service addon when using `RunScript`

This is something I have considered, never came up with a good answer.  Only way I could figure out is to have the "runscript" addon set a home window property but then the service has to poll the property.  I couldn't see an easy to send a notification to the service, unless the addon can do something that would trigger the Kodi Monitor class.

scott s.
.


RE: Call function of another kodi addon - izprtxqkft - 2023-12-18

if it is a service then use an xbmc monitor and a json rpc notifyall

explanation
if a service implements a monitor and listens to onNotification https://alwinesch.github.io/group__python__monitor.html#ga0e49e1b7c38dd22ef51276ad4efbd7e9

then any calls to NotifyAll will be sent to the monitoring service to trigger actions or receive messages https://kodi.wiki/view/JSON-RPC_API/v13#JSONRPC.NotifyAll

you can setup the sender as your own addon so the service knows you sent it and it can ignore everything else

i have even been able to send json payloads as the message so it's quite versatile

payload={"json": "payload"}
rpc={"jsonrpc":"2.0","method":"JSONRPC.NotifyAll","params":{"message":"MyService","sender":"Test.Client","data":payload},"id":1}

send(json.dumps(rpc).encode('utf-8'))

i use a socket to send the payload but there are integrated methods for sending jsonrpc


RE: Call function of another kodi addon - Roman_V_M - 2023-12-19

Maybe before trying to implement more complex ways of inter-process communication, a simpler approach is possible? If you control both addons, then you can use <extension point="xbmc.python.module" library="foo" /> in your addon A and <import addon="script.foo.a" /> in your addon B. And then you can import functions and classes of your addon A into B and use them as necessary. An addon can have multiple extension points, so the same addon can be a script and a module library, for example.


RE: Call function of another kodi addon - izprtxqkft - 2023-12-19

(2023-12-19, 22:58)Roman_V_M Wrote: Maybe before trying to implement more complex ways of inter-process communication, a simpler approach is possible? If you control both addons, then you can use <extension point="xbmc.python.module" library="foo" /> in your addon A and <import addon="script.foo.a" /> in your addon B. And then you can import functions and classes of your addon A into B and use them as necessary. An addon can have multiple extension points, so the same addon can be a script and a module library, for example.

that's actually the same solution from the stack exchange link in post #1 but it didn't work for the OP (for whatever reason)


RE: Call function of another kodi addon - scott967 - 2023-12-23

(2023-12-18, 23:33)izprtxqkft Wrote: if it is a service then use an xbmc monitor and a json rpc notifyall

explanation
if a service implements a monitor and listens to onNotification https://alwinesch.github.io/group__python__monitor.html#ga0e49e1b7c38dd22ef51276ad4efbd7e9

then any calls to NotifyAll will be sent to the monitoring service to trigger actions or receive messages https://kodi.wiki/view/JSON-RPC_API/v13#JSONRPC.NotifyAll

you can setup the sender as your own addon so the service knows you sent it and it can ignore everything else

i have even been able to send json payloads as the message so it's quite versatile

payload={"json": "payload"}
rpc={"jsonrpc":"2.0","method":"JSONRPC.NotifyAll","params":{"message":"MyService","sender":"Test.Client","data":payload},"id":1}

send(json.dumps(rpc).encode('utf-8'))

i use a socket to send the payload but there are integrated methods for sending jsonrpc

Thanks for the tip.  I was thinking it would only work on http or raw sockets transport, not via python xbmc.Monitor.

scott s.
.