0

I am making a python toolbox for ArcGIS Pro 2.9.8 with arcpy in Python 3.7 I have a workspace parameter defined as

param1 = arcpy.Parameter(
        displayName="Feature Dataset or Geodatabase containing featureclasses",
        name="in_fcs",
        datatype="DEWorkspace",
        parameterType="Required",
        direction="Input", )

Parameter 3 this the input field name; all the featureclasses should have the same fields with the same meaning so I just want to return a list of fields based on the first featureclass in the input workspace which is accomplished with this

param3 = arcpy.Parameter(
        displayName="Weight Field",
        name="weightField",
        datatype="GPString",
        parameterType="Required",
        direction="Input", )

and...

    def updateParameters(self, parameters):
        if not parameters[1].altered:
            parameters[3].filter.list = None
            parameters[3].value = None
        elif parameters[1].altered and not parameters[1].hasBeenValidated:
            parameters[3].filter.type = "ValueList"
            parameters[3].filter.list = [i.name for i in 
            arcpy.ListFields(arcpy.ListFeatureClasses(parameters[1].value)[0])]
            parameters[3].value = parameters[3].filter.list[0]

This works, when I add a Workspace in the tool it gives me a dropdown list of the fields of the first featureclass but it doesn't update when I change the Workspace to something else with different featureclasses and I'm not sure why. Where am I going wrong?

1 Answer 1

1

With some help from a colleague we discovered that it is not recommended to use arcpy.ListFeatureClasses() or arcpy.ListFields() in a pythonToolBox and that the Describe method is prefered. My first solution was to simply add arcpy.env.workspace = parameters[1].value but I didn't like this. With some help I came up with the following code works.

def updateParameters(self, parameters):
    if not parameters[1].altered:
        parameters[3].filter.list = None
        parameters[3].value = None
    elif parameters[1].altered and not parameters[1].hasBeenValidated:
        parameters[3].filter.type = "ValueList"
        parameters[3].filter.list = [i.name for i in arcpy.Describe(parameters[1].value).children[0].fields]
        parameters[3].value = parameters[3].filter.list[0]

I'm moving forward with this but if you have a better answer, perhaps something cleaner let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.