[Qgis-developer] How to provide a Processing model in a plugin ?

Victor Olaya volayaf at gmail.com
Tue Jun 23 06:20:24 PDT 2015


Etienne

Good to see that it works :-)

For the record, and in case you are interested in checking it, it
might be worth having a look at the "add model from file" action. It
is a different approach, just adding the file to the model folder and
refreshing the model provider, but just in case it is useful for you.
It will also make the model available for the user, outside of your
plugin context

https://github.com/qgis/QGIS/blob/master/python/plugins/processing/modeler/AddModelFromFileAction.py

Regarding the progress indication, once you have your algorithm with
parameters set, you can execute it using the AlgorithmExecutor class,
which accepts a progress object as parameter. That should let you
redirect the output messages and progress indications. Notice that
this might change soon, since there is a multithreading implementation
of that class being written at the moment.

Let me know if you have more questions

Cheers



2015-06-23 14:28 GMT+02:00 Etienne Trimaille <etienne.trimaille at gmail.com>:
> Hi,
> I don't know exactly what happened, but it works now. I can launch my own
> model from the plugin.
>
> This is the final code :
>
> model = ModelerAlgorithm.ModelerAlgorithm.fromFile(file_path)
> model.provider = Processing.modeler
> Processing.modeler.algs.append(model)
> command_line = model.commandLineName()
> Processing.algs['model'][command_line] = model
> return command_line
>
> runalg shows by default the QgsMessageBar. We will see if we need it or if
> we need to change processing.
> Thanks again Victor.
>
>
> 2015-06-22 17:28 GMT+02:00 Etienne Trimaille <etienne.trimaille at gmail.com>:
>>
>> Hi Victor,
>>
>> Thanks for the answer. I maybe the first one, but I need to launch a model
>> in the InaSAFE plugin and I don't want to write by hand the equivalent in
>> python :)
>> I tried your code above :
>>
>> model = ModelerAlgorithm.fromFile("Path/to/your/model/file")
>> model.provider = Processing.modeler
>> Processing.modeler.algs.append(model)
>>
>> But I can't launch it with runalg, I got 'Algorithm not found', so I add
>> this :
>> Processing.algs['model'][model.commandLineName()] = model
>>
>> I can see it with processing.alglist now.
>>
>> But I can't launch it with processing.runalg :
>>
>> Traceback (most recent call last):
>>   File
>> "/home/etienne/.qgis2/python/plugins/inasafe/safe/routing/gui/routing_dialog.py",
>> line 210, in accept
>>     None)
>>   File "/home/etienne/.qgis2/python/plugins/processing/tools/general.py",
>> line 71, in runalg
>>     alg = Processing.runAlgorithm(algOrName, None, *args)
>>   File
>> "/home/etienne/.qgis2/python/plugins/processing/core/Processing.py", line
>> 277, in runAlgorithm
>>     alg = alg.getCopy()
>>   File
>> "/home/etienne/.qgis2/python/plugins/processing/modeler/ModelerAlgorithm.py",
>> line 169, in getCopy
>>     newone.defineCharacteristics()
>>   File
>> "/home/etienne/.qgis2/python/plugins/processing/modeler/ModelerAlgorithm.py",
>> line 208, in defineCharacteristics
>>     modelOutput = copy.deepcopy(alg.algorithm.getOutputFromName(out))
>>   File
>> "/home/etienne/.qgis2/python/plugins/processing/modeler/ModelerAlgorithm.py",
>> line 110, in algorithm
>>     self._algInstance =
>> ModelerUtils.getAlgorithm(self.consoleName).getCopy()
>> AttributeError: 'NoneType' object has no attribute 'getCopy'
>>
>>
>> Do you have an idea ?
>>
>> Regards,
>> Etienne
>>
>>
>>
>>
>> 2015-06-08 16:28 GMT+02:00 Victor Olaya <volayaf at gmail.com>:
>>>
>>> You can use the instance of ModelerAlgorithmProvider that is stored in
>>> the Processing object.
>>>
>>> Like this:
>>>
>>> model = ModelerAlgorithm.fromFile("Path/to/your/model/file")
>>> model.provider = Processing.modeler
>>> Processing.modeler.algs.append(model)
>>>
>>> That will add your model to the list of available ones
>>>
>>> I guess you are the first one asking for this...and this is the best
>>> solution i can think of so far :-)
>>>
>>> Let me know if that works...
>>>
>>> I hope it helps
>>>
>>>
>>>
>>> 2015-06-08 15:35 GMT+02:00 Etienne Trimaille
>>> <etienne.trimaille at gmail.com>:
>>> > Hi,
>>> >
>>> > I'm working on a plugin and I need to use my own model inside.
>>> > I know how to add a new algorithm in processing but I didn't find
>>> > something
>>> > on how to add a model to processing.
>>> >
>>> > Is there a way to ship my model with the plugin ?
>>> > Or do I need to copy manually my model into the processing models
>>> > folder
>>> > with Python ?
>>> >
>>> > I saw the ModelerAlgorithmProvider (like the AlgorithmProvider) but I
>>> > not
>>> > sure if I can do what I want with it.
>>> >
>>> >
>>> > Thanks,
>>> > Etienne
>>> >
>>> > _______________________________________________
>>> > Qgis-developer mailing list
>>> > Qgis-developer at lists.osgeo.org
>>> > http://lists.osgeo.org/mailman/listinfo/qgis-developer
>>
>>
>


More information about the Qgis-developer mailing list