[Qgis-developer] How to provide a Processing model in a plugin ?
Etienne Trimaille
etienne.trimaille at gmail.com
Tue Jun 23 05:28:44 PDT 2015
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
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20150623/fc91dbe2/attachment-0001.html>
More information about the Qgis-developer
mailing list