I created a model and I would like to have it as a python script.

this is what i see when i run my model

This is what I see when I run my model and I can select a layer from this window. I don't necessarily need to select a layer, it can just process the active layer.

When I export this model as python code this is the corresponding part that select myLayer parameter.

from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
import processing

class myModel(QgsProcessingAlgorithm):

    def initAlgorithm(self, config=None):
        self.addParameter(QgsProcessingParameterVectorLayer('addlayer', 'myLayer', types=[QgsProcessing.TypeVectorAnyGeometry], defaultValue=None))
        self.addParameter(QgsProcessingParameterFeatureSink('Result', 'Result', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))

Is it possible to edit this code in order to make it target the active layer to process the model?


1 Answer 1

  • Add from qgis.utils import iface line to import section.
  • Remove self.addParameter(QgsProcessingParameterVectorLayer('addlayer', 'myLayer', ...) line.
  • And change all parameters['addlayer'] into iface.activeLayer().

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.