crispincooper

checking for existence of input data in toolvalidator crashes arcmap

Discussion created by crispincooper on Aug 25, 2011
Latest reply on Nov 3, 2011 by crispincooper
I'm trying to write a toolvalidator which auto-fills various parameters with field names from the selected input feature layer, if the relevant fields exist in the feature layer.

Trouble is, if the user enters the name of a non-existant input feature layer, it crashes all of arcmap!

How can I safely check that the data exists?
I have tried variants on arcpy.Exists and arcpy.Describe (surrounded by try/catch) - all lead to the crash.

    defaultnames = {1:"default_name_1",
                    2:"default_name_2"}

    fcname = self.params[0].value
    if fcname and arcpy.Exists(fcname):
      desc = arcpy.Describe(fcname)
      datatype = desc.dataType
      if datatype=="FeatureClass" or datatype=="FeatureLayer":
        fieldnames = [f.name for f in desc.fields]
        for param_index,name in defaultnames.iteritems():
          if name in fieldnames:
            self.params[param_index].value = name

Outcomes