Class: QgsProcessingModelAlgorithm

Model based algorithm with processing.

Class Hierarchy

Inheritance diagram of qgis.core.QgsProcessingModelAlgorithm

Base classes

QgsProcessingAlgorithm

Abstract base class for processing algorithms.

Methods

activateChildAlgorithm

Attempts to activate the child algorithm with matching id.

addChildAlgorithm

Adds a new child algorithm to the model.

addGroupBox

Adds a new group box to the model.

addModelParameter

Adds a new parameter to the model, with the specified definition and graphical component.

asPythonCode

Attempts to convert the model to executable Python code, and returns the generated lines of code.

availableDependenciesForChildAlgorithm

Returns details of available dependencies for the child algorithm with matching id.

availableSourcesForChild

Returns a list of possible sources which can be used for the parameter param for a child childId algorithm in the model

changeParameterName

Changes a model parameter's internal name from oldName to newName.

childAlgorithm

Returns the child algorithm with matching id.

childAlgorithms

Returns the map of child algorithms contained in the model.

childAlgorithmsDependOnParameter

Returns True if any child algorithms depend on the model parameter with the specified name.

createExpressionContextScopeForChildAlgorithm

Creates a new expression context scope for a child algorithm within the model.

deactivateChildAlgorithm

Deactivates the child algorithm with matching id.

dependentChildAlgorithms

Returns a list of the child algorithm IDs depending on the child algorithm with the specified childId.

dependsOnChildAlgorithms

Returns a list of the child algorithm IDs on which the child algorithm with the specified childId depends upon.

designerParameterValues

Returns the parameter values to use as default values when running this model through the designer dialog.

fromFile

Reads the model from a file, at the specified path.

groupBoxes

Returns a list of the group boxes within the model.

helpContent

Returns the model's help contents (a free-form map of values describing the algorithm's use and metadata).

loadVariant

Loads this model from a QVariantMap, wrapped in a QVariant variant.

modelNameMatchesFilePath

Returns True if the model name matches the current model sourceFilePath().

modelParameterFromChildIdAndOutputName

Given a child algorithm ID and output name, attempts to match it to a parameter definition from the overall model.

orderedOutputs

Returns an ordered list of outputs for the model.

orderedParameters

Returns an ordered list of parameters for the model.

otherParametersDependOnParameter

Returns True if any other model parameters depend on the parameter with the specified name (e.g. field parameters where name is the parent layer parameter).

outputGroup

Returns the destination layer tree group name for outputs created by the model.

parameterComponent

Returns the parameter component with matching name.

parameterComponents

Returns the map of parameter components used by the model.

removeChildAlgorithm

Attempts to remove the child algorithm with matching id.

removeGroupBox

Removes the group box with matching uuid from the model.

removeModelParameter

Removes an existing model parameter by name.

setChildAlgorithm

Sets the child algorithm within the model.

setChildAlgorithms

Sets the map of child algorithms contained in the model.

setDesignerParameterValues

Sets the parameter values to use as default values when running this model through the designer dialog.

setGroup

Sets the model group.

setHelpContent

Sets the model's help contents (a free-form map of values describing the algorithm's use and metadata).

setName

Sets the model name.

setOutputGroup

Sets the destination layer tree group name for outputs created by the model.

setOutputOrder

Sets the order for sorting outputs for the model.

setParameterComponent

Sets a parameter component for the model.

setParameterComponents

Sets the map of parameter components used by the model.

setParameterOrder

Sets the order for showing parameters for the model.

setSourceFilePath

Sets the source file path for the model, if available.

setVariables

Sets the map of custom expression context variables for this model.

sourceFilePath

Returns the source file path for the model, if available.

toFile

Writes the model to a file, at the specified path.

toVariant

Saves this model to a QVariantMap, wrapped in a QVariant.

updateDestinationParameters

Updates the model's parameter definitions to include all relevant destination parameters as required by child algorithm ModelOutputs.

updateModelParameter

Replaces the definition of an existing parameter (by parameter name) with a new definition.

validate

Validates the model, returning True if all child algorithms in the model are valid.

validateChildAlgorithm

Validates the child algorithm with matching ID, returning True if all mandatory inputs to the algorithm have valid values.

variables

Returns the map of custom expression context variables defined for this model.

variablesForChildAlgorithm

Returns a map of variable name to variable definition for expression context variables which are available for use by child algorithm during model execution.

Static Methods

safeName

Makes a name "safe", by replacing any non-alphanumeric characters with underscores.

class qgis.core.QgsProcessingModelAlgorithm[source]

Bases: QgsProcessingAlgorithm

__init__(name: str | None = '', group: str | None = '', groupId: str | None = '')

Constructor for QgsProcessingModelAlgorithm.

Parameters:
  • name (Optional[str] = '')

  • group (Optional[str] = '')

  • groupId (Optional[str] = '')

class VariableDefinition[source]

Bases: object

Definition of a expression context variable available during model execution.

QgsProcessingModelAlgorithm.VariableDefinition(value: Any = None, source: QgsProcessingModelChildParameterSource = QgsProcessingModelChildParameterSource.fromStaticValue(QVariant()), description: Optional[str] = ‘’) Constructor for a new VariableDefinition with the specified value and original parameter source, and description.

QgsProcessingModelAlgorithm.VariableDefinition(a0: QgsProcessingModelAlgorithm.VariableDefinition)

description: str

Translated description of variable

source: QgsProcessingModelChildParameterSource

Original source of variable’s value

value: object

Value of variable

activateChildAlgorithm(self, id: str | None) bool[source]

Attempts to activate the child algorithm with matching id. If any child algorithms on which the child depends are not active, then the child will not be activated and False will be returned.

Parameters:

id (Optional[str])

Return type:

bool

addChildAlgorithm(self, algorithm: QgsProcessingModelChildAlgorithm) str[source]

Adds a new child algorithm to the model. If a child algorithm already exists in the model with the same child ID then algorithm will be assigned a new autogenerated unique ID. The assigned child ID will be returned.

See also

childAlgorithm()

Parameters:

algorithm (QgsProcessingModelChildAlgorithm)

Return type:

str

addGroupBox(self, groupBox: QgsProcessingModelGroupBox)[source]

Adds a new group box to the model.

If an existing group box with the same uuid already exists then its definition will be replaced.

See also

groupBoxes()

Added in version 3.14.

Parameters:

groupBox (QgsProcessingModelGroupBox)

addModelParameter(self, definition: QgsProcessingParameterDefinition | None, component: QgsProcessingModelParameter)[source]

Adds a new parameter to the model, with the specified definition and graphical component. Ownership of definition is transferred to the model.

Parameters:
asPythonCode(self, outputType: QgsProcessing.PythonOutputType, indentSize: int) List[str][source]

Attempts to convert the model to executable Python code, and returns the generated lines of code.

The outputType argument dictates the desired script type.

The indentSize arguments specifies the size of indented lines.

Parameters:
Return type:

List[str]

availableDependenciesForChildAlgorithm(self, childId: str | None) List[QgsProcessingModelChildDependency]

Returns details of available dependencies for the child algorithm with matching id.

Added in version 3.14.

Parameters:

childId (Optional[str])

Return type:

List[QgsProcessingModelChildDependency]

availableSourcesForChild(self, childId: str | None, param: QgsProcessingParameterDefinition | None) List[QgsProcessingModelChildParameterSource]

Returns a list of possible sources which can be used for the parameter param for a child childId algorithm in the model

The source compatibilibity is deducted from the parameter type

Added in version 3.44.

Parameters:
Return type:

List[QgsProcessingModelChildParameterSource]

availableSourcesForChild(self, childId: str | None, parameterTypes: Iterable[str | None] = [], outputTypes: Iterable[str | None] = [], dataTypes: Iterable[int] = []) List[QgsProcessingModelChildParameterSource]

Returns a list of possible sources which can be used for the parameters for a child algorithm in the model. Returned sources are those which match either one of the specified parameterTypes (see QgsProcessingParameterDefinition.type() ) or one of the specified outputTypes (see QgsProcessingOutputDefinition.type() ). If specified, an optional list of dataTypes can be used to filter the returned sources to those with compatible data types for the parameter/outputs.

Use it when you need fine grained support for the requested compatible sources

Parameters:
  • childId (Optional[str])

  • parameterTypes (Iterable[Optional[str]] = [])

  • outputTypes (Iterable[Optional[str]] = [])

  • dataTypes (Iterable[int] = [])

Return type:

List[QgsProcessingModelChildParameterSource]

changeParameterName(self, oldName: str | None, newName: str | None)[source]

Changes a model parameter’s internal name from oldName to newName.

This method will automatically update all model components to relink using the new name.

Added in version 3.26.

Parameters:
  • oldName (Optional[str])

  • newName (Optional[str])

childAlgorithm(self, id: str | None) QgsProcessingModelChildAlgorithm

Returns the child algorithm with matching id. If no child algorithm exists with this ID a new algorithm will be added to the model and returned.

Parameters:

id (Optional[str])

Return type:

QgsProcessingModelChildAlgorithm

childAlgorithms(self) Dict[str, QgsProcessingModelChildAlgorithm]

Returns the map of child algorithms contained in the model. The keys are the child algorithm ids (see QgsProcessingModelAlgorithm.ChildAlgorithm.childId()).

See also

childAlgorithm()

Return type:

Dict[str, QgsProcessingModelChildAlgorithm]

childAlgorithmsDependOnParameter(self, name: str | None) bool[source]

Returns True if any child algorithms depend on the model parameter with the specified name.

Parameters:

name (Optional[str])

Return type:

bool

createExpressionContextScopeForChildAlgorithm(self, childId: str | None, context: QgsProcessingContext, modelParameters: Dict[str, Any] = {}, results: Dict[str, Any] = {}) QgsExpressionContextScope | None[source]

Creates a new expression context scope for a child algorithm within the model.

Parameters:
  • childId (Optional[str])

  • context (QgsProcessingContext)

  • modelParameters (Dict[str, Any] = {})

  • results (Dict[str, Any] = {})

Return type:

Optional[QgsExpressionContextScope]

deactivateChildAlgorithm(self, id: str | None)[source]

Deactivates the child algorithm with matching id. All other child algorithms which depend on the child algorithm will also be deactivated.

Parameters:

id (Optional[str])

dependentChildAlgorithms(self, childId: str | None, conditionalBranch: str | None = '') Set[str]

Returns a list of the child algorithm IDs depending on the child algorithm with the specified childId.

Optionally, a specific conditional branch which is created by the child algorithm can be specified in order to restrict the returned list to algorithms which depend on this specific branch.

Parameters:
  • childId (Optional[str])

  • conditionalBranch (Optional[str] = '')

Return type:

Set[str]

dependsOnChildAlgorithms(self, childId: str | None) Set[str]

Returns a list of the child algorithm IDs on which the child algorithm with the specified childId depends upon.

Parameters:

childId (Optional[str])

Return type:

Set[str]

designerParameterValues(self) Dict[str, Any][source]

Returns the parameter values to use as default values when running this model through the designer dialog.

This usually corresponds to the last set of parameter values used when the model was run through the designer.

Added in version 3.14.

Return type:

Dict[str, Any]

fromFile(self, path: str | None) bool[source]

Reads the model from a file, at the specified path.

See also

toFile()

Parameters:

path (Optional[str])

Return type:

bool

groupBoxes(self) List[QgsProcessingModelGroupBox]

Returns a list of the group boxes within the model.

See also

addGroupBox()

Added in version 3.14.

Return type:

List[QgsProcessingModelGroupBox]

helpContent(self) Dict[str, Any]

Returns the model’s help contents (a free-form map of values describing the algorithm’s use and metadata).

See also

setHelpContent()

Return type:

Dict[str, Any]

loadVariant(self, variant: Any) bool[source]

Loads this model from a QVariantMap, wrapped in a QVariant variant. You can use QgsXmlUtils.readVariant to load it from an XML document.

See also

toVariant()

Added in version 3.4.

Parameters:

variant (Any)

Return type:

bool

modelNameMatchesFilePath(self) bool[source]

Returns True if the model name matches the current model sourceFilePath().

Specifically, this method will return true if the complete base name of sourceFilePath() is identical (case-insensitive) to the model name.

Added in version 3.24.

Return type:

bool

modelParameterFromChildIdAndOutputName(self, childId: str | None, childOutputName: str | None) QgsProcessingParameterDefinition | None[source]

Given a child algorithm ID and output name, attempts to match it to a parameter definition from the overall model.

Added in version 3.26.

Parameters:
  • childId (Optional[str])

  • childOutputName (Optional[str])

Return type:

Optional[QgsProcessingParameterDefinition]

orderedOutputs(self) List[QgsProcessingModelOutput]

Returns an ordered list of outputs for the model.

See also

setOutputOrder()

Added in version 3.32.

Return type:

List[QgsProcessingModelOutput]

orderedParameters(self) List[QgsProcessingModelParameter]

Returns an ordered list of parameters for the model.

Added in version 3.14.

Return type:

List[QgsProcessingModelParameter]

otherParametersDependOnParameter(self, name: str | None) bool[source]

Returns True if any other model parameters depend on the parameter with the specified name (e.g. field parameters where name is the parent layer parameter).

Parameters:

name (Optional[str])

Return type:

bool

outputGroup(self) str[source]

Returns the destination layer tree group name for outputs created by the model.

See also

setOutputGroup()

Added in version 3.32.

Return type:

str

parameterComponent(self, name: str | None) QgsProcessingModelParameter

Returns the parameter component with matching name. If no parameter component exists with this name a new component will be added to the model and returned.

Parameters:

name (Optional[str])

Return type:

QgsProcessingModelParameter

parameterComponents(self) Dict[str, QgsProcessingModelParameter]

Returns the map of parameter components used by the model. The keys should match the algorithm’s parameter names (see parameterDefinitions() ).

Return type:

Dict[str, QgsProcessingModelParameter]

removeChildAlgorithm(self, id: str | None) bool[source]

Attempts to remove the child algorithm with matching id. Returns True if the algorithm could be removed, or False if the algorithm could not be removed (e.g. due to other child algorithms depending on it).

Parameters:

id (Optional[str])

Return type:

bool

removeGroupBox(self, uuid: str | None)[source]

Removes the group box with matching uuid from the model.

See also

addGroupBox()

See also

groupBoxes()

Added in version 3.14.

Parameters:

uuid (Optional[str])

removeModelParameter(self, name: str | None)[source]

Removes an existing model parameter by name. The definition of the matching parameter is deleted.

Parameters:

name (Optional[str])

static safeName(name: str | None, capitalize: bool = False) str[source]

Makes a name “safe”, by replacing any non-alphanumeric characters with underscores.

If capitalize is True then the string will be converted to a camel case string.

Added in version 3.26.

Parameters:
  • name (Optional[str])

  • capitalize (bool = False)

Return type:

str

setChildAlgorithm(self, algorithm: QgsProcessingModelChildAlgorithm)[source]

Sets the child algorithm within the model. If a child algorithm already exists in the model with the same child ID then that algorithm will be replaced.

Parameters:

algorithm (QgsProcessingModelChildAlgorithm)

setChildAlgorithms(self, childAlgorithms: Dict[str | None, QgsProcessingModelChildAlgorithm])[source]

Sets the map of child algorithms contained in the model. The keys are the child algorithm ids (see QgsProcessingModelAlgorithm.ChildAlgorithm.childId()). All existing child algorithms will be replaced.

See also

childAlgorithm()

Parameters:

childAlgorithms (Dict[Optional[str], QgsProcessingModelChildAlgorithm])

setDesignerParameterValues(self, values: Dict[str, Any])[source]

Sets the parameter values to use as default values when running this model through the designer dialog.

This usually corresponds to the last set of parameter values used when the model was run through the designer.

Added in version 3.14.

Parameters:

values (Dict[str, Any])

setGroup(self, group: str | None)[source]

Sets the model group.

See also

group()

Parameters:

group (Optional[str])

setHelpContent(self, contents: Dict[str, Any])[source]

Sets the model’s help contents (a free-form map of values describing the algorithm’s use and metadata).

See also

helpContent()

Parameters:

contents (Dict[str, Any])

setName(self, name: str | None)[source]

Sets the model name.

See also

name()

Parameters:

name (Optional[str])

setOutputGroup(self, group: str | None)[source]

Sets the destination layer tree group name for outputs created by the model.

See also

outputGroup()

Added in version 3.32.

Parameters:

group (Optional[str])

setOutputOrder(self, order: Iterable[str | None])[source]

Sets the order for sorting outputs for the model.

The order list should consist of “output child algorithm id:output name” formatted strings corresponding to existing model outputs.

See also

orderedOutputs()

Added in version 3.32.

Parameters:

order (Iterable[Optional[str]])

setParameterComponent(self, component: QgsProcessingModelParameter)[source]

Sets a parameter component for the model. If a parameter component already exists in the model with the same parameter name then that component will be replaced.

Parameters:

component (QgsProcessingModelParameter)

setParameterComponents(self, parameterComponents: Dict[str | None, QgsProcessingModelParameter])[source]

Sets the map of parameter components used by the model. The keys should match the algorithm’s parameter names (see parameterDefinitions() ). All existing parameter components will be replaced.

Parameters:

parameterComponents (Dict[Optional[str], QgsProcessingModelParameter])

setParameterOrder(self, order: Iterable[str | None])[source]

Sets the order for showing parameters for the model.

The order list should consist of parameter names corresponding to existing model parameterComponents().

Added in version 3.14.

Parameters:

order (Iterable[Optional[str]])

setSourceFilePath(self, path: str | None)[source]

Sets the source file path for the model, if available.

See also

sourceFilePath()

Parameters:

path (Optional[str])

setVariables(self, variables: Dict[str, Any])[source]

Sets the map of custom expression context variables for this model.

These variables are added to the model’s expression context scope, allowing for preset “constant” expression variables to be utilized within the model.

See also

variables()

Added in version 3.8.

Parameters:

variables (Dict[str, Any])

sourceFilePath(self) str[source]

Returns the source file path for the model, if available.

Return type:

str

toFile(self, path: str | None) bool[source]

Writes the model to a file, at the specified path.

See also

fromFile()

Parameters:

path (Optional[str])

Return type:

bool

toVariant(self) Any[source]

Saves this model to a QVariantMap, wrapped in a QVariant. You can use QgsXmlUtils.writeVariant to save it to an XML document.

See also

loadVariant()

Added in version 3.4.

Return type:

Any

updateDestinationParameters(self)[source]

Updates the model’s parameter definitions to include all relevant destination parameters as required by child algorithm ModelOutputs. Must be called whenever child algorithm ModelOutputs are altered.

updateModelParameter(self, definition: QgsProcessingParameterDefinition | None)[source]

Replaces the definition of an existing parameter (by parameter name) with a new definition. Ownership of definition is transferred to the model, and any existing parameter is deleted.

Parameters:

definition (Optional[QgsProcessingParameterDefinition])

validate(self)[source]

Validates the model, returning True if all child algorithms in the model are valid.

Returns:

  • True if the child is valid

  • issues: a list of issues encountered during the validation

Added in version 3.14.

validateChildAlgorithm(self, childId: str | None)[source]

Validates the child algorithm with matching ID, returning True if all mandatory inputs to the algorithm have valid values.

Parameters:

childId (Optional[str]) -> (bool) – ID for child to validate

Returns:

  • True if the child is valid

  • issues: a list of issues encountered during the validation

Added in version 3.14.

variables(self) Dict[str, Any][source]

Returns the map of custom expression context variables defined for this model.

These variables are added to the model’s expression context scope, allowing for preset “constant” expression variables to be utilized within the model.

See also

setVariables()

Added in version 3.8.

Return type:

Dict[str, Any]

variablesForChildAlgorithm(self, childId: str | None, context: QgsProcessingContext | None = None, modelParameters: Dict[str, Any] = {}, results: Dict[str, Any] = {}) Dict[str, QgsProcessingModelAlgorithm.VariableDefinition]

Returns a map of variable name to variable definition for expression context variables which are available for use by child algorithm during model execution.

The child algorithm childId and processing context are manadatory. If modelParameters and results are not specified, then only the variable names and sources will be returned, but all variable values will be null. This can be used to determine in advance which variables will be available for a specific child algorithm, e.g. for use in expression builder widgets.

In order to calculate the actual variable value, the input model modelParameters and already executed child algorithm results must be passed.

Parameters:
  • childId (Optional[str])

  • context (Optional[QgsProcessingContext] = None)

  • modelParameters (Dict[str, Any] = {})

  • results (Dict[str, Any] = {})

Return type:

Dict[str, QgsProcessingModelAlgorithm.VariableDefinition]