QGIS Default Layer Inputfield – Default Layer in the Input Field in QGIS Processing Script

defaultdropdownlistinputlayersqgis

I am writing a script and I would like to let the user choose the layer but i would like to default a layer (when it is open in the project) in the input field and if the user changes it he can choose another one from the list.
I can specify a the drop down list or a specific layer but then the dropdown option is no longer available. Can I specify a default layer like I can do it with numbers?

This gives me the dropdown list with my layer options from the list

alg_params = {
    'INPUT': parameters['Input_name'],
    'INPUT_FIELDS': [''],
    'OVERLAY': parameters['Overlay_name'],
    'OVERLAY_FIELDS': [''],
    'OVERLAY_FIELDS_PREFIX': '',
    'OUTPUT': parameters['result_name']

In this I choose the layer, but its not a default, it is hard set and my dropdown list disappears.

alg_params = {
    'INPUT': 'Input_name_d9798079_c0d4_4e60_a3f7_ce2ca427d004',
    'INPUT_FIELDS': [''],
    'OVERLAY': parameters['Overlay_name'],
    'OVERLAY_FIELDS': [''],
    'OVERLAY_FIELDS_PREFIX': '',
    'OUTPUT': parameters['result_name']

Best Answer

What you want can be done when you add the parameters to your processing script class in the initAlgorithm function

...
    def initAlgorithm(self, config=None):
        """
        Here we define the inputs and output of the algorithm, along
        with some other properties.
        """

        # We add the input vector features source. It can have any kind of
        # geometry.
        self.addParameter(
            QgsProcessingParameterFeatureSource(
                self.INPUT,
                self.tr('Input layer'),
                [QgsProcessing.TypeVectorAnyGeometry],
                defaultValue="the layer name in legend or the layer.shp file path"  # HERE
            )
        )
 ...

Related Question