2

I was successful in triggering a model with the Python editor with this one-liner:

    processing.runAndLoadResults('model:my_model', {'param1':sParam1,'param2': sParam2,'coordinatesystem':QgsCoordinateReferenceSystem('EPSG:25832')),'qgis:createpointslayerfromtable_1:Trees':'memory:'})

It only works, when the model is already added to the QGIS project. In my scenario this is not the case, which is why I need to replace the part 'model:my_model' with a windows path to the model.

I found some helpful info here:
Using processing.runandload in QGIS 3
The example is unfortunately for QgsRasterLayer . I assumed something like "QgsModel" would serve my purpose, so I searched for a class containing "model" in the QGIS API Documentation, but without success.

How do you use processing.runAndLoadResults() with a model stored on disk?

  • I am just thinking maybe you have to save your model file/script to a specific qgis folder and use a concrete naming? I am checking this – Ruben FV Jul 19 at 13:18
  • Did you save your model as a 'project model' or as a *.model3 file? – Ruben FV Jul 19 at 13:26
  • As a .model3 file – GISme Jul 19 at 13:55
  • Can you try to save it as part of the project and load the project when executing your python code? – Ruben FV Jul 19 at 14:41
  • I am not sure if this helps, did you create your model with the grphical modeler? check my answer – Ruben FV Jul 19 at 14:46
2

Not a definite solution but one possible method could be to load the model dialog and insert this into the processing call function. The issue here is that the only parameters the user defines would be the input and output layers, all other parameters would use the same values stored in the actual model.


Example:

I have a simple model which takes an input layer, buffers it accordingly to some parameters and outputs the result:

Modeler parameters

This model is saved on my desktop. If I want to now run the model using processing, I could use:

from processing.modeler.ModelerDialog import ModelerDialog
dlg = ModelerDialog()
dlg.loadModel('C:/Users/Me/Desktop/test.model3')
processing.runAndLoadResults(dlg.model, {'layer':'C:/Users/Me/Desktop/Point_example.shp', 'native:buffer_1:result':'memory:'})

Note that the input parameter is the same name as the parameter in the model (layer) and the output is named after the algorithm + the output parameter name (native:buffer_1:result). If you include any other parameters, they are ignored as they only use the values from the model. There probably is a method to run the model and define the parameter values using python so hopefully others can find it.

1

Make sure your model .model3 file is the default QGIS folder: user folder + \processing\models

You can find this user folder by clicking on Settings / User profiles / Open Active Profile folder

(For example, in my Windows computer)

C:\Users\user\AppData\Roaming\QGIS\QGIS3\profiles\default\processing\models


Maybe you can also try to save the model file as part of a specific project (the option is given in the graphical modeler).

Then, load the project before calling your process with python.


from qgis.core import QgsProject
from qgis.processing import runAndLoadResults

project = QgsProject.instance()
project.read('/home/user/projects/your project.qgs')
processing.runAndLoadResults('model:my_model', {'param1':sParam1,'param2': sParam2,'coordinatesystem':QgsCoordinateReferenceSystem('EPSG:25832')),'qgis:createpointslayerfromtable_1:Trees':'memory:'})
  • Ruben, thanks for the work! I have to distribute my code to other users, where I don't have access to their default QGIS folders. Every user has their own customized QGIS project, so I can't add it their project either. My only option is to find a way to launch the model from a *.model file. – GISme Jul 19 at 15:05

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.