I want to perform a selection on tool startup in order to obtain a choice list for parameter derived from selected features. Code kind of works, as I get my choice list with correct values, however it works in a strange way:
- After pasting code into validation window and clicking OK, selection starts and is performed several times.
- On the first tool startup, selection is done again several times.
- Choice list gets remembered as Value List and doesn't change with further tool startups (in fact on next startups, nothing new happens - no selection or any other actions from initializeParameters).
What am I doing wrong?
Here is a code:
import arcpy
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
arcpy.SelectLayerByLocation_management(in_layer="OBR", overlap_type="CONTAINS", select_features="POW")
choice_list = []
with arcpy.da.SearchCursor("OBR", ["OBR", "NAZWA"]) as cursor:
for row in cursor:
choice_list.append(row[0])
arcpy.SelectLayerByAttribute_management("OBR", "CLEAR_SELECTION")
self.params[0].filter.list = choice_list
return
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return