3

I'd like to know how can I create a QGIS Plugin starting from a model built with the graphical modeler. Is it possible or i have to use necessarily scripts python made in the console of QGIS?

Any suggestions? I have to use "Plugin Builder"?

3

Yes you sure can.

Check here if you are using an older version of Python. Converting models from QGIS to Python scripts?

I am using 3.14, and inside of the Model Designer, there is a button at the top called 'Export as Script Algorithm'. Press that.

0
2

In practice it comes down to exporting your model as python script, building a plugin using PluginBuilder and copying your code into the created plugin code. Although doing this in a single click is a nice idea for a new plugin :). Everything is explained in this post Converting Model in Plug-in in QGIS

1

You can actually package models in plugins without the need to convert them to scripts.

To do so:

  1. Use the Plugin Builder (itself a QGIS Plugin) to create a starter processing plugin

  2. Open the provider file (*provider.py) in a text editor

  3. Find the line:

from qgis.core import QgsProcessingProvider

and underneath it add the lines:

from qgis.core import QgsProcessingModelAlgorithm

import os

  1. Under the the loadAlgorythms module (def loadAlgorithms(self):) add the following lines of code:
for dirpath, dirnames, files in os.walk(os.path.dirname(__file__)):
    for file_name in files:
        if file_name.lower().endswith('.model3'):
            alg = QgsProcessingModelAlgorithm()
            alg.fromFile(os.path.join(dirpath, file_name))
            self.addAlgorithm(alg)

The above code searches through the plugin folder and any subfolders and if it finds model files it references them. So, from this point forward all you need to do to add models to this plugin is to drop them into the plugin directory.

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.