I'm trying to understand how to build a script tool that has multiple optional input parameters in conjunction with parameter validation. Basically, I give the user two options to input a location that gets fed into a buffer tool. The first input option is to use an interactive map click using a feature set on the back end. The second option is to allow the user to manually enter coordinates as the input location for the buffer tool. When I set up parameter validation to disable the option not being used, the code behind still looks for the parameter/s even though the input has been disabled. I can't figure out how to get it to ignore the parameters that are not being entered by the disabled input options. Any advice would be much appreciated.
Here's the main script behind the tool:
import arcpy
arcpy.overwriteOutput = True
output_SR = arcpy.SpatialReference(4326)
output_gdb = "%scratchGDB%"
location_by_featureset = arcpy.GetParameter(0)
if location_by_featureset:
Buffer = arcpy.analysis.MultipleRingBuffer(location_by_featureset, output_gdb+"\Output_Buffer", "20", "Miles")
else:
Lat_Int = int(arcpy.GetParameterAsText(1))
Long_Int = int(arcpy.GetParameterAsText(2))
point_location = arcpy.management.CreateFeatureclass(output_gdb, "Point_Loc", "Point", "", "Enabled", "Enabled", output_SR)
point_loc = arcpy.Point(Long_Int, Lat_Int)
with arcpy.da.InsertCursor(point_location, ["SHAPE@XY"]) as cursor:
cursor.insertRow([point_loc])
del cursor
Buffer = arcpy.analysis.MultipleRingBuffer(point_location, output_gdb+"\Output_Buffer", "20", "Miles")
arcpy.SetParameter(3, Buffer)
arcpy.AddMessage("Process Complete")
Here's the parameter validation code:
class ToolValidator:
# Class to add custom behavior and properties to the tool and tool parameters.
def __init__(self):
# Set self.params for use in other validation methods.
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
# Customize parameter properties. This method gets called when the
# tool is opened.
return
def updateParameters(self):
# Modify the values and properties of parameters before internal
# validation is performed.
if self.params[0].altered:
self.params[1].enabled = False
self.params[2].enabled = False
if self.params[1].altered:
self.params[0].enabled = False
return
def updateMessages(self):
# Modify the messages created by internal validation for each tool
# parameter. This method is called after internal validation.
return
# def isLicensed(self):
# # Set whether the tool is licensed to execute.
# return True
# def postExecute(self):
# # This method takes place after outputs are processed and
# # added to the display.
# return
Solved! Go to Solution.
I can't find the page that documents it, but you can make parameters conditionally required in updateMessages()
if (SOUR_TABL.value == "This is bad"):
INPUT_TBL.setIDMessage('ERROR', 735)
Just make the parameter optional and then do this.