I have a shapefile and I want to calculate multiple fields using the GP ValueTable parameter.
The script loads field names from a layer, then I should delete rows I don ́t want to calculate and then I should write values to the second column to be filled in selected fields. Then it should arcpy.CalculateField
.
But I am stuck at parameter settings. The script lies in the Python toolbox.
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = parameter("in_layer", "Vrstva", "GPFeatureLayer")
param1 = parameter("field_values", "Atributova pole a hodnoty k vyplneni", "GPValueTable")
param1.parameterDependencies = [param0.name]
param1.columns = [["String", "Pole"], ["String", "Hodnota"]]
params = [param0, param1]
return params
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if parameters[0].value:
v_list = []
fld_names = [f.name for f in arcpy.ListFields(parameters[0].value)]
for fld in fld_names:
v_list.append([fld, ""])
del fld
parameters[1].value = v_list
del fld_names
del v_list
return
When I select a layer in the tool, it populates field names (great!) and it looks like this: Tool parameters
But...
- If I want to delete some rows using the button on the right side, it does nothing, it does not delete the row. It just seems that it somehow "refreshes" the value table.
- If I want to write a value to the second column, it lets me but after hitting enter or clicking anywhere else it erases the written value and sets nothing, empty value again. It does not hold what I am writing there.
How do I make the ValueTable work correctly?
1 Answer 1
The gist of your answer can be found in Python Toolbox - Only update parameter when specific parameter changes. You need to use altered
and hasBeenValidated
properties of arcpy.Parameter
.
In this specific case, you need to change if parameters[0].value
to if parameters[0].altered and not parameters[0].hasBeenValidated
.
Explore related questions
See similar questions with these tags.