2

I am attempting to provide the user a drop down list of field values based on a predefined shapefile or table. The string value users choose then needs to be taken by the script tool for use later in the script.

I have the 'drop down' value list dynamically populated using the following validation code:

def updateParameters(self):
   self.params[2].filter.list = [str(val) for val in sorted(set(row.getValue('FC_field') for row in arcpy.SearchCursor("FC_path",None, None, 'FC_field')))]
   return

I then have the following script tool code which attempts to retrieve and store that value:

#---Import system modules.---
import arcpy

ProjectName = arcpy.GetParameterAsText(0)           
ProjectBoundaries_FC = arcpy.GetParameterAsText(1)  
Airport = arcpy.GetParameterAsText(2)

arcpy.AddMessage("INPUT: Project Name (0): <" + ProjectName + ">.")
arcpy.AddMessage("INPUT: Project boundaries (1): <" + ProjectBoundaries_FC + ">.")
arcpy.AddMessage("INPUT: Project Airport (2): <" + Airport + ">.")

The tool Parameters are set according to the image: enter image description here

When the script tool is executed the text editor window pops up (illustrated below). Once I close it the tool processing bar stops and the rest of the code which would utilize that value never runs. In other words, when I close the editor window the script tool reports processing completed without errors, but never processes the "AddMessage" code (illustrated in the following image).

enter image description here enter image description here

I suspect this is largely due to my unfamiliarity with using validation code.

As requested, here is a screenshot of the tool window showing the tool parameter options in use.

enter image description here

I am using ArcGIS v10.1 for Desktop - Advanced (ArcInfo) License

I think the most interesting thing here is why the tools script pops up in the text pad editor window at all?

8
  • 1
    I'd simplify what it's doing, because ~17 minutes of waiting to verify your validation sounds painful.
    – Paul
    Feb 11, 2017 at 0:19
  • Please edit your question to include a screenshot of your tool window showing your tool parameter options (the window that pops up as soon as you double-click the tool, before you click OK to run it).
    – Midavalo
    Feb 11, 2017 at 0:29
  • What version of ArcGIS are you running?
    – Midavalo
    Feb 11, 2017 at 0:34
  • Appears validation may be causing a failure in accessing the data type defined as 'feature class'....I would try defining it as 'feature layer' and providing the custom internal validation so you can capture any error (what you aren't seeing before the tool dialog opens) Feb 11, 2017 at 0:39
  • In my experience, validation should have finished running before you hit OK on the tool. Once you've got the output dialog up it should be onto your actual code and past the tool validation
    – Midavalo
    Feb 11, 2017 at 0:48

2 Answers 2

1

A couple of things might be happening that may slow things down, or their may be ways to speed it up. Although, validation in my experience is always slow.

For one, since you are using ArcPy 10.1, switch to the da module for your search cursor. This module tends to process data faster than the original SearchCursor (arcpy.da.SearchCursor). You will need to change your code slightly as for this.

Also, you may want to add an if statement to check that the path to your feature class has been altered if not self.params[0].altered:....It might be trying to update your list of values anytime you click the tool with your mouse. You could also add a check to see if the feature class exists (arcpy.exists()), and that the field you are searching for exists.

for the da module: http://pro.arcgis.com/en/pro-app/arcpy/data-access/searchcursor-class.htm

The arcpy.da cursors (arcpy.da.SearchCursor, arcpy.da.UpdateCursor, and arcpy.da.InsertCursor) were introduced with ArcGIS 10.1 to provide significantly faster performance over the previously existing set of cursor functions (arcpy.SearchCursor, arcpy.UpdateCursor, and arcpy.InsertCursor). The original cursors are provided only for continuing backward compatibility.

2
  • The da module doesn't make a difference. To account for your suggestion about constant updating I moved the code to initializeParameters rather than the updateParamerters function with the same result. The feature class and the fields exist because the drop down list is populating with the correct values
    – pyRsq
    Feb 13, 2017 at 17:45
  • Maybe try and do the sorting after all the values have been passed to the list. self.params[2].filter.list = sorted([val[0] for val in arcpy.da.SearchCursor("FC_path",['FC_field'])])
    – dslamb
    Feb 13, 2017 at 18:39
0

Ok, so the solution is quite silly in hindsight! The problem was never with validation or the script itself. Recently I've been just coding in a text pad because I've been in and out of the office. I forgot to change the file extension to .py from a .txt. The .txt was what was being referenced by the script tool.

Your Answer

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

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