4


Recently, I started making a model on QGIS and now I want to transform it into a plug-in, but it seems that I have to change the code to do it, otherwise it won't work.

What changes are required to perform this conversion?

1 Answer 1

8

To create a plugin from a model, use the "Plugin Builder" plugin. By following THIS tutorial, you can build a basic structure.

On the third screen for plug-in creation, define the template as Process Provider. The model conversion is easier in this mode. You will not need to use QT Creator or compile.bat described in the above link tutorial.

After you create your empty plugin, export your model as a Python script using QGIS version 3.6 or higher. Save the created script.

enter image description here

In the folder where it was created the empty plugin, there are 5 basic files that should be edited. Keep the others as they are. If your plugin has only one function, edit only the fourth file, use the default lines as examples:

This is a example of a plugin with 2 functions.

1- PluginNAME.py You need to create a class for each function (model) that will be displayed, simply change the class name and self.provider in initProcessing.

class NewModelPlugin(object):

    def __init__(self):
        self.provider = None

    def initProcessing(self):
        self.provider = NewModelProvider()
        QgsApplication.processingRegistry().addProvider(self.provider)

    def initGui(self):
        self.initProcessing()

    def unload(self):
        QgsApplication.processingRegistry().removeProvider(self.provider)

class Model2Plugin(object):

    def __init__(self):
        self.provider = None

    def initProcessing(self):
        self.provider = Model2Provider()
        QgsApplication.processingRegistry().addProvider(self.provider)

    def initGui(self):
        self.initProcessing()

    def unload(self):
        QgsApplication.processingRegistry().removeProvider(self.provider)

For example, the Buffer by Percentage plugin has two functions, so in it's code there are two classes.

enter image description here

2- __init__.py This file references all Classes created in the previous file, create a new block for each function.

    from .PluginNAME import NewModelPlugin
    return NewModelPlugin()

    from .PluginNAME import Model2Plugin
    return Model2Plugin()

3- PluginNAME_provider.py Indicates which algorithms will be loaded. In import, list all comma-separated algorithms. In loadAlgorithm, enter all, one on each line.

from qgis.core import QgsProcessingProvider
from .PluginNAME_algorithm import (NewModelAlgorithm, Model2Algorithm)

class PluginNAMEProvider(QgsProcessingProvider):

    def __init__(self):
        QgsProcessingProvider.__init__(self)

    def unload(self):
        pass

    def loadAlgorithms(self):
        self.addAlgorithm(NewModelAlgorithm())
        self.addAlgorithm(Model2Algorithm())

4- PluginNAME_algorithm.py Here will be the code of each algorithm. Initially you need to import all libraries that all algorithms will use. QGIS automatic conversion creates almost all, you will know which ones are missing when trying to run.

Create a class (QgsProcessingAlgorithm) for each desired algorithm.They are independent of each other, so each must have its own variables and parameters.
It's in this file, in each class, that you define the displayed name, system name, description, and icon displayed in QGIS. Replace everything from Class (line 41 by default) with the Class contents of your script generated from your model.
Change the name of this class and the last return to the same name defined in the previous file.

class NewModelAlgorithm(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
    .
    .
    .

    def createInstance(self):
        return NewModelAlgorithm()

class Model2Algorithm(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
    .
    .
    .

    def createInstance(self):
        return Model2Algorithm()

5- Metadata.txt In this file is the information that will be displayed on the plugin manager screen such as name, version, changelog.

In order for your plugin to appear in QGIS, move the full folder created by PuginBuilder to your user's python folder, usually in C:\Users\User\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins.
In QGIS, go to Plugins > Manage and Install plugins and enable it in the Installed tab. Your plugin should appear inside your toolbox.

Using IF.

If you need one model to be executed by another via an if structure in the _algorithm.py file, it will be inserted into processAlgorithm. After the if, put the required code with one more tab.

class NewModelAlgorithm(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
    .
    .
    .

    def processAlgorithm(self, parameters, context, model_feedback):
        feedback = QgsProcessingMultiStepFeedback(55, model_feedback)
        results = {}
        outputs = {}
        if SOMETHING is None: 
            PLACE YOUR CODE HERE
        elif OTHERSTUFF is not None:
            ANOTHER CODE HERE
        else:
            ONE MORE OPTION

All input and output parameters that will be used within the if must exist in the initAlgorithm section.

4
  • Hi Herbert Santos, How is the exported script from qgis model is linked to the plugin files(5), tried to follow the steps but failing to get output. Any clarification would be much appreciated Commented Jul 9, 2020 at 14:16
  • @MathewPetro when exporting a model to a script, copy the code generated starting in the class line, and paste it into the file 4 _algorithm.py creating new classes in the plugin. Did I clarify? Commented Jul 10, 2020 at 15:28
  • Hello Herbert Santos. Thank you a lot, it has worked, I have one extra question, maybe it is not described here, iI would like to make this plugin to appear to the toolbar as well, I tried and end up with the only icon, and button in the toolbar but doesn't start when i click it from the toolbar kindly help me if you have any idea. Thanks Commented Jul 11, 2020 at 14:55
  • @MathewPetro As far as I understand plugins, it seems to me that they work differently when they are on the tollbar and on the tollbox. At the beginning of my answer it is written to define as a process provider. It is necessary to choose another option to work at the tollbar, but the step by step I mentioned here will not work in the same way. This is a method for plugins in which I have no knowledge yet. Commented Jul 13, 2020 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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