I need to define a required field parameter, and I'd like it to work with both existing and new fields, i.e. regardless of whether the field already exists.
The problem is, if I enter a new (non-existing field), I get error 000728: Field ... does not exist within table.
This must be due to the parameterDependencies property, but without it I would not get a dropdown allowing to select an existing field. The direction parameter does not seem to matter (Input or Output).
Basically, I'd like to be able to use the drop-down field selection when the field exists, and enter the name directly for a new field. This sounds pretty basic, I must be missing something simple. Could this be done?
Here is a sample *.pyt code to illustrate what I am trying to do.
import arcpy
class Toolbox(object):
def __init__(self):
self.label = "[Test] Select existing or new field"
# List of tool classes associated with this toolbox
self.tools = [Test_SelectExistingOrNewField]
class Test_SelectExistingOrNewField(object):
def __init__(self):
self.label = "Select existing or new field"
self.description = ""
def getParameterInfo(self):
# Feature class where an existing field resides, or a new field will be added to
p0 = arcpy.Parameter(
displayName = "Input feature class",
name = "fc",
datatype = "DEFeatureClass",
parameterType = "Required",
direction = "Input")
# Name of the output field in the `normals` feature class which will contain the final (conflated) Z values.
p1 = arcpy.Parameter(
displayName = "Existing or new field name in the input feature class",
name = "fld",
datatype = "Field",
parameterType = "Required",
direction = "Output")
p1.parameterDependencies = [p0.name]
p1.value = 'AField'
return [p0, p1]
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""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, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
pass