I have the following PyQGIS script with QGIS 3 that tries to ask for a layer, add a new field in that layer and modify the value of this field. But the code doesn't work. I get either Seems there is no valid script in the file when I use layer = QgsVectorLayer(Layer) or AttributeError: 'NoneType' object has no attribute 'isValid' when I use QgsProcessingUtils.mapLayerFromString :
##Layer=vector
context = QgsProcessingContext()
layer= QgsProcessingUtils.mapLayerFromString(Layer, context)
layer.startEditing()
if layer.dataProvider().fieldNameIndex("new_col_name") == -1:
layer.dataProvider().addAttributes([QgsField("new_col_name", QVariant.String)])
layer.updateFields()
id_new_col= feuilles.dataProvider().fieldNameIndex("new_col_name")
for feature in layer.getFeatures():
column_value= feature["column"]
... some operations ...
layer.changeAttributeValue(feature.id(), id_new_col, new_column_value)
layer.commitChanges()