4

I am trying to create a plugin using plugin builder. I have to combine three plugins in order to get the layer I want to work with.

1st plugin: I need to use the 'tools' it provides and its GUI.

2nd plugin: I want to use the previous results(layer) and provide a new layer.

I downloaded and put all the plugins I needed in the same folder. I followed this steps. How can I make the first plugin's GUI visible? Where should I pass a command in my plugin (in initGui/- run?)? Is that thing possible?

1
  • Somehow but I found it too complicated
    – Annie S.
    Aug 18 at 13:04
9

Nice question!

Let's say plugin1 uses (or depends on) plugin2. Then:

In plugin1:

  • In metadata.txt, you need to add plugin2 as prerequisite. You can either set any version of a plugin as prerequisite (you do that if you omit the plugin2's version) or set a specific version of plugin2 as prerequisite.

    Have a look at this GIF by Alessandro Pasotti to see how it works.

    Basically, you would add a line like this to your metadata.txt:

         plugin_dependencies=plugin2 name==v1.4.2
    

    Which says that your plugin depends on version 1.4.2 of plugin2. Note that plugin2 name should come from plugin2's metadata.txt file (name field). You can add more dependencies separating them using a comma, like here.

  • Before accessing plugin2, you should test if plugin2 is still accessible. After all, a user could uninstall plugin2 at any time. For that you could test, for example:

        from qgis.utils import plugins
        if 'plugin2_dirname' not in plugins:
            # Let the user know that plugin2 is required
            return
    
  • You can call plugin2 in several ways.

    1. You might want to use the instance that is already initialized by QGIS. That is:

        from qgis.utils import plugins
        plugin2_instance = plugins['plugin2_dirname']
        plugin2_instance.the_method_you_need()
        # Or: plugin2_instance.get_another_object_from_plugin2()
      
    2. Depending on plugin2 code's structure, you won't be able to access classes or methods from plugin2_instance if it does not expose them. If that's the case, you can still access those classes and create your own instances in this way:

        from plugin2.utils import SampleClass
        sample = SampleClass(required_params)
        sample.the_method_you_need()
      

Now, where to call plugin2 from plugin1?

That depends. Have a look at What is the purpose of some functions and files in QGIS Python plugins? to realize why you shouldn't call plugin2's dialog from plugin1.initGui().

You probably want to call plugin2's dialog from your plugin1.run(), which is called when the user tells your plugin to do something.

How to call plugin2's dialog?

That depends. You would need to explore plugin2 source code to see how it shows its dialog (of course, a plugin could have a number of dialogs). Chances are, you can simply do:

plugin2_instance.run()

and if the plugin2_instance.run() returns you anything, you could do:

results = plugin2_instance.run()
if results...:
   # Do something with the plugin2.run() results

On QGIS plugin interaction

Plugin interaction in QGIS is something that depends on how each plugin is structured, and how well it offers its classes/methods to other plugins. For instance, some plugins separate GUI and core stuff, so that you can use such plugins as libraries and avoid the GUI part. Other plugins could have a proper API, exposing you only "public" properties and methods.

In summary, it depends on your use cases and on the base plugin's code. But in general, it should be possible!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.