I have a python toolbox with a tool that takes two arguments. First parameter is GPFeatureLayer and the second is Field of Integer type. I would like to set the second parameter to a specific field if it exist within the layer but I am unable to figure out a way to do it correctly.
My code looks like this:
def getParameterInfo(self):
param0 = arcpy.Parameter(
displayName="Input lines",
name="in_lines",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param0.filter.list = ["Polyline"]
param1 = arcpy.Parameter(
displayName="ID source",
name="in_id_source",
datatype="Field",
parameterType="Required",
direction="Input")
param1.filter.list = ["Integer"]
param1.parameterDependencies = [param0.name]
params = [param0, param1]
return params
Later in the code I would like to set parameter 1 to field "OID_SOURCE" as a default value if it exists. However, the following code is not working:
def updateParameters(self, parameters):
if parameters[0].value:
fields = arcpy.Describe(parameters[0].valueAsText).fields
index = None
for i in range(0, len(fields)):
if fields[i].name == "OID_SOURCE":
index = i
parameters[1].value = fields[index]
return
This makes the parameter 1 always empty. I tried using this:
parameters[1].value = fields[index].name
But that yelds error "Invalid field type". I do understand why the version with "fields[index].name" is not working as I am trying to asssing String to a Field.
But I am wondering is there any way preset the the parameter with specific field if it exists?