I am trying to script some behaviour into the ArcGIS Toolbox validator class. Specifically, I am creating a dropdown parameter so the user can choose a predefined option and then a second parameter will update a list of items. The issue I am having is, when I change the option in the first parameter, I can get the options to change in the second parameter, but when I toggle the 'Unselect All' button in the second parameter, the items will not uncheck (they remained checked). I have a feeling it has to do with my script in the Validator.
Here are the details (see image below as well):
- First parameter is a string type. It is required, Input Direction, Has a default value. Multivalue = No and uses a value list so that I can have a dropdown with 3 options to choose from
- Second parameter is a string type. It is required. Contains Default values. Multivalue = Yes and uses a value list so that the items populated in this parameter (a list of items) can be toggled on or off.
So, in the Validator, my script is as follows. Can anyone see something obvious I may be missing here to get the 'Select All' and 'Unselect All' buttons to work?
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."""
self.params[1].filter.list = [1,2,3,4]
self.params[1].values = self.params[1].filter.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."""
if self.params[0].altered == True:
if self.params[0].value == 'Option 1':
self.params[1].filter.list = [1,2,3,4]
self.params[1].values = self.params[1].filter.list
elif self.params[0].value == 'Option 2':
self.params[1].filter.list = ['a', 'b', 'c']
self.params[1].values = self.params[1].filter.list
elif self.params[0].value == 'Option 3':
self.params[1].filter.list = ['dr', 'bht', 'cjjjyy']
self.params[1].values = self.params[1].filter.list
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
-
@user2856 Thanks for that. That fixed it! Appreciate your time. if you're interested in submitting that as an answer, I will mark it as the correct answer and up vote. Cheers.Mike– Mike2020年08月20日 17:14:23 +00:00Commented Aug 20, 2020 at 17:14
1 Answer 1
The "arcpy way TM" is to check if parameters[0].altered and not parameters[0].hasBeenValidated:
According to the [documentation][1]:
altered
altered
is true if the value of a parameter is changed... Once a parameter has been altered, it remains altered until the user empties (blanks out) the value, in which case it returns to being unaltered.
hasBeenValidated
hasBeenValidated
is false if a parameter's value has been modified by the user since the last timeupdateParameters
and internal validate were called. Once internal validate has been called, geoprocessing automatically setshasBeenValidated
to true for every parameter.
hasBeenValidated
is used to determine whether the user has changed a value since the last call toupdateParameters
.
So change your updateParameters
to:
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."""
if self.params[0].altered and not self.params[0].hasBeenValidated:
etc...
-
Thanks @user2856. Great clear answer.Mike– Mike2020年08月20日 22:56:47 +00:00Commented Aug 20, 2020 at 22:56