Get plugin’s main class instance

Hi! Is it possible to get the instance of the main plugin’s class from outside of it (within the same code)?

class MyPlugin(GeneralPlugin):
   def method(self):
      print("hello")

class AnotherClass():
   def anotherMethod(self):
      myPluginInstance.method() # how to get myPluginInstance?

What do you want to do exactly?

I think you mean to access the main plugin instance, not the class.

You need to give the reference to the plugin instance to the other class

class MyPlugin(GeneralPlugin):
    def setup(self):
        self.otherObject = AnotherClass()
        self.otherObject.pluginInstance = self
    def method(self):
        print("hello")

then you can do:

class AnotherClass():
   def anotherMethod(self):
      self.pluginInstance.method()

Perfect, thank you!

Trying to be object-oriented :smile:

Normally, an object should encapsulate functionally so that it doesn’t need access to outside stuff.
So maybe move def method(self): to the other class.

1 Like