2

I have this parameters for my python script:

import arcpy

Layer1 = arcpy.GetParameterAsText(0) 
Tabela1 = arcpy.GetParameterAsText(1) 
Estado = arcpy.GetParameterAsText(2)
Ano = arcpy.GetParameterAsText(3).split(';')  
mxd = arcpy.mapping.MapDocument("CURRENT")   
Saida = arcpy.GetParameterAsText(4)
stats = []

Parameter 'Ano' is a multivalue field selection and this parameter obtained the value list from the feature class 'Tabela1'. There is many fields in this FC and all of them are listed in 'Ano' but I only want to show the fields with a prefix ACH*. The script works well, But don`t want to get the user confusing with too many fields in the 'Ano' field.

4

Unfortunately, the only filter you can set on the parameter of Field data type is the field type, not its name.

In order to achieve the name filtering, you would need to use a custom validator.

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    if self.params[0].value:
      self.params[2].filter.list = [f.name for f in arcpy.ListFields(self.params[0].value,
                                                                    wild_card='POP*')]
    return

This code would populate the third script tool parameter (of String type, Multivalue) with the names that start with POP. The second parameter shows all the fields (Field type, Multivalue), the third one only the filtered:

enter image description here

If you would like to keep your business logic code (think source code of the script tool) and the validation code (what happens to parameters and messages when user interacts with it) in one single file, then look at Python toolboxes. One single text .pyt file which can contain one or more script tools.

  • Alex, I put in the Validation in the Script Properties and works well. Just in case...there is any possible to insert into the python code directly? – Vinicius Cruvinel Jul 17 '17 at 12:23
  • @ViniciusCruvinel, sure look at Python toolboxes - I've added this to my answer – Alex Tereshenkov Jul 17 '17 at 16:38
  • Alex, thanks. I will read and implement in my code. – Vinicius Cruvinel Jul 17 '17 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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