I am setting up the parameters in a Python toolbox. I have a parameter that I would like to list fields based on a user input feature class. I am wondering if it is possible to list fields based on two feature classes.
def getParameterInfo(self):
input_data = arcpy.Parameter(
displayName = "Input data to be fuzzed to a grid",
name = "input_data",
datatype = "GPFeatureLayer",
parameterType = "Required",
direction = "Input")
existing_grid = arcpy.Parameter(
displayName = "Existing Grid",
name = "site_desc",
datatype = "GPFeatureLayer",
parameterType = "Optional",
direction = "Input")
output_fields = arcpy.Parameter(
displayName = "Fields to be Included in Output Fuzzed Data",
name = "output_fields",
datatype = "Field",
parameterType = "Required",
direction = "Input",
multiValue = True)
output_fields.parameterDependencies = [input_data.name]
I would like for the user to be able to also choose fields from the existing_grid dataset, in addition to the input_data dataset.
How do I include the fields from both in the field list?
I tried adding it separately and including it in the brackets, but this just caused the tool parameter to be blank.
1 Answer 1
As suggested by @mikewatt in the comments, you'll need to custominze this functionality with arcpy.ListFields
. This will need to be done in the updateParameters
function. Check for your parameters being altered. If they are, list their fields. Add those fields to your parameter filter.
def getParameterInfo(self):
input_data = arcpy.Parameter(
displayName = "Input data",
name = "input",
datatype = "GPFeatureLayer",
parameterType = "Required",
direction = "Input")
existing_grid = arcpy.Parameter(
displayName = "Existing Grid",
name = "site_desc",
datatype = "GPFeatureLayer",
parameterType = "Optional",
direction = "Input")
output_fields = arcpy.Parameter(
displayName = "Fields to be Included",
name = "output_fields",
datatype = "GPString",
parameterType = "Required",
direction = "Input",
multiValue = True)
output_fields.filter.list = []
return [input_data, existing_grid, output_fields]
def updateParameters(self, parameters):
for i in range (2):
#check if parameter has been changed
if not parameters [i].hasBeenValidated:
#empty list to populate with fields
flds = []
#iterate both parameters
for x in range (2):
#get parameter
fc = parameters [x].valueAsText
#check if parameter has been input
if fc:
#get input feature class fields and add to list
flds += [f.name for f in arcpy.ListFields (fc)]
#update field input
parameters [2].filter.list = flds
After one input:
After two inputs: