Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

access procedures / variables of loaded plugin

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
bruinlax
Posts: 26
Joined: Thu Sep 29, 2011 4:31 pm

access procedures / variables of loaded plugin

Post by bruinlax »

what's the syntax to have a python script access a variable / procedure of a loaded plugin base?

for instance to run from a script something like:
print eg.plugins("MyPlugin").strVariable or
eg.plugins("MyPlugin").myProcedure()

where sample plugin base is:

Code: Select all

class MyPlugin(eg.PluginBase):
    strVariable
    def myProcedure(self):
        print "hello world -2"

    def __init__(self):
        pass
    
    def __start__(. . . 
        strVariable = "Hello World"

    def __stop__(self):
        pass
        
    def __close__(self):
. . .
Thanks. . .
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: access procedures / variables of loaded plugin

Post by krambriw »

The way I learned is to use a getter action like the following:

Here is you plugin slightly modified where I have added an action that will return you the content of "strVariable".

Code: Select all

import eg

eg.RegisterPlugin()



class MyPlugin(eg.PluginBase):

    def __init__(self):
        self.strVariable = ''
        self.AddAction(HelloWorld)
    
    def __start__(self):
        self.strVariable = "Hello World"
        print "Started"

    def myProcedure(self):
        print "hello world -2"

    def __stop__(self):
        print "Stopped"
        pass
        
    def __close__(self):
        print "Closed"
        pass



class HelloWorld(eg.ActionBase):

    def __call__(self):
        return self.plugin.strVariable
To use this from a python script, you could typically do like this:

Code: Select all

myMessage = eg.plugins.MyPlugin.HelloWorld()
print myMessage
So in summary, use an action to access a variable in the plugin.

Best regards, Walter
bruinlax
Posts: 26
Joined: Thu Sep 29, 2011 4:31 pm

Re: access procedures / variables of loaded plugin

Post by bruinlax »

cool. i can make that work. thanks.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: access procedures / variables of loaded plugin

Post by Pako »

The way that Krambriw said, is not the only one.
There is also another way to get or set the plugin variables.
You can see it in the following example:
Test_variable_access.png
Test_variable_access.png (18.59 KiB) Viewed 4605 times

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<EventGhost Version="1544">
    <Folder Name="Test Variable" Expanded="True">
        <Macro Name="GetHelloWorld" Expanded="True">
            <Action>
                TestVariable.GetHelloWorld()
            </Action>
            <Action>
                EventGhost.PythonCommand(u'print eg.result')
            </Action>
        </Macro>
        <Macro Name="SetHelloWorld" Expanded="True">
            <Action>
                TestVariable.SetHelloWorld(u'Test value')
            </Action>
        </Macro>
        <Folder Name="Python scripts for access a variable / procedure" Expanded="True">
            <Macro Name="Set variable" Expanded="True">
                <Action>
                    EventGhost.PythonCommand(u'eg.plugins.TestVariable.plugin.strVariable = "New value"')
                </Action>
            </Macro>
            <Macro Name="Get variable" Expanded="True">
                <Action>
                    EventGhost.PythonCommand(u'eg.result = eg.plugins.TestVariable.plugin.strVariable')
                </Action>
                <Action>
                    EventGhost.PythonCommand(u'print eg.result')
                </Action>
            </Macro>
            <Macro Name="Execute procedure" Expanded="True">
                <Action>
                    EventGhost.PythonCommand(u'eg.plugins.TestVariable.plugin.myProcedure()')
                </Action>
            </Macro>
        </Folder>
    </Folder>
</EventGhost>
Excellent toy for similar testing is the Python Shell (Help menu in EventGhost).
Pako

Attached is a modified testing plugin:
Attachments
__init__.py
"TestVariable" plugin
(1.2 KiB) Downloaded 262 times
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: access procedures / variables of loaded plugin

Post by krambriw »

Dear Pako,
This is of course a nicer and very visible way since it is shown in the tree structure. If you prefer to have it more "hidden", and using a more advanced script with additional logic inside, built in calls to actions the way I showed could be the choice. Both ways will of course work.

Best regards, Walter
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: access procedures / variables of loaded plugin

Post by Pako »

krambriw wrote:If you prefer to have it more "hidden", and using a more advanced script with additional logic inside, built in calls to actions the way I showed could be the choice.
Dear Walter !
I do not understand your post. Or you misunderstood my post.
I mainly wanted to point out that access to plugin variables is possible even without getter action.
This can be useful especially in case of an existing plugin, which is inappropriate to intervene.

Best regards, Pako
krambriw
Plugin Developer
Posts: 2570
Joined: Sat Jun 30, 2007 2:51 pm
Location: Stockholm, Sweden
Contact:

Re: access procedures / variables of loaded plugin

Post by krambriw »

Dear Pako,
You are absolutely correct, no misunderstanding, your way is the best.

Best regards, Walter
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: access procedures / variables of loaded plugin

Post by Pako »

krambriw wrote:You are absolutely correct, ..., your way is the best.
Dear Walter,
no, I do not think it. We can not say that variant using the eg.plugins.<PluginClassName>.plugin.<PluginVariable> is the best.
It is only one variant, suitable in certain cases. Likewise, a variant with a getter action is again most appropriate in other cases.
I'm thinking in particular cases when you can expect frequent use for less experienced users.

Best regards, Pako
Post Reply