Page 1 of 1

Programmatically installing a plugin

Posted: Wed Apr 12, 2017 5:07 am
by kgschlosser
here is some code that you can add to your plugin if it relies on another plugin being installed to work

for this example I am going to use the Broadcaster plugin. you can use either the plugin's GUID or the name of the plugin will also work. but this will use the class name to check and see if the plugin is installed

Code: Select all


BROADCASTER_GUID = "{5E8DA56B-24AC-4092-9521-169343C5171C}"
BROADCASTER_CLASS_NAME = "BroadcastListener"

def InstallPlugin(plugInfo):
    eg.AsTasklet(eg.UndoHandler.NewPlugin(eg.document).Do)(plugInfo)
        
if not hasattr(eg.plugins, BROADCASTER_CLASS_NAME):
    pluginInfo = eg.pluginManager.GetPluginInfo(BROADCASTER_GUID)
    wx.CallAfter(InstallPlugin, pluginInfo)


Re: Programmatically installing a plugin

Posted: Wed Apr 12, 2017 5:55 am
by kgschlosser
and here is some code if you wanted to set the plugins startup parameters programmatically. if and only if the plugins default parameters will not throw a traceback. this closes the plugins config dialog and then sets the new parameters.

the only way to do this if it does throw a traceback is to use dialog.panel.GetChildren() and set the values for each of the specific widgets then call wx.CallAfter(dialog.DispatchEvent, None, wx.ID_OK) this will set the values then close the dialog.. and you can then remove the wx.CallAfter(treeItem.SetArguments, STARTUP_PARAMETERS)

Code: Select all

import wx
import threading


BROADCASTER_GUID = "{5E8DA56B-24AC-4092-9521-169343C5171C}"
BROADCASTER_CLASS_NAME = "BroadcastListener"
BROADCASTER_NAME = "Broadcaster"
STARTUP_PARAMETERS = ("TestBroadcaster", "255.255.255.255", 33333, False, "&&", "")
EVENT = threading.Event()

def InstallPlugin(plugInfo):
    eg.UndoHandler.NewPlugin(eg.document).Do(plugInfo)
    EVENT.set()

def CloseDialog():
    while not EVENT.isSet():
        if eg.document.frame.openDialogs:
            dialog = eg.document.frame.openDialogs[-1]
            if hasattr(dialog.treeItem, 'info') and dialog.treeItem.info.guid == BROADCASTER_GUID:
                EVENT.set()
                treeItem = dialog.treeItem
                wx.CallAfter(dialog.DispatchEvent, None, wx.ID_OK)
                wx.CallAfter(treeItem.SetArguments, STARTUP_PARAMETERS)
        
if not hasattr(eg.plugins, BROADCASTER_CLASS_NAME):
    pluginInfo = eg.pluginManager.GetPluginInfo(BROADCASTER_GUID)
    wx.CallAfter(eg.AsTasklet(InstallPlugin), pluginInfo)
    threading.Thread(target=CloseDialog).start()

Re: Programmatically installing a plugin

Posted: Wed Apr 12, 2017 6:51 am
by kgschlosser
and here is an example of setting the parameters at the widget level

Code: Select all

import wx
import threading


BROADCASTER_GUID = "{5E8DA56B-24AC-4092-9521-169343C5171C}"
BROADCASTER_CLASS_NAME = "BroadcastListener"
BROADCASTER_NAME = "Broadcaster"
STARTUP_PARAMETERS = ("TestBroadcaster", "255.255.255.255", 0, 33333, False, "&&")
EVENT = threading.Event()

def InstallPlugin(plugInfo):
    eg.UndoHandler.NewPlugin(eg.document).Do(plugInfo)
    EVENT.set()

def CloseDialog():
    while not EVENT.isSet():
        if eg.document.frame.openDialogs:
            dialog = eg.document.frame.openDialogs[-1]
            if hasattr(dialog.treeItem, 'info') and dialog.treeItem.info.guid == BROADCASTER_GUID:
                try:
                    children = dialog.panel.GetChildren()
                    if len(children) < 6:
                        continue
                except:
                    continue
                    
                prefixCtrl, zoneCtrl, portCtrl, addrCtrl, selfbroadCtrl, _ = children
                prefixCtrl.SetValue("TestBroadcaster")
                zoneCtrl.SetValue("255.255.255.255")
                portCtrl.SetValue(12345)
                addrCtrl.SetValue(0)
                selfbroadCtrl.SetValue(True)
                wx.CallAfter(dialog.DispatchEvent, None, wx.ID_OK)
                EVENT.wait()
        
if not hasattr(eg.plugins, BROADCASTER_CLASS_NAME):
    pluginInfo = eg.pluginManager.GetPluginInfo(BROADCASTER_GUID)
    wx.CallAfter(eg.AsTasklet(InstallPlugin), pluginInfo)
    threading.Thread(target=CloseDialog).start()