Is there a way to reference a selection in model builder or python so that I can edit my python script to only run if there is a selection. And even better if there is only one row selected?

I made a model to make a selection with python script to ask for variables, make a selection based on those variables, and then zoom to the selection. When the tool does not make a selection for whatever reason it takes forever to finish and zooms to the entire extent of the map or crashes ARCmap entirely.

  • What kind of data are you trying to select in the current map?
    – cndnflyr
    Sep 18, 2013 at 13:07

1 Answer 1


I have done this in the past by creating a separate script tool and adding that to my Model Builder model. The code I used is as follows:

import arcpy

layer = arcpy.GetParameterAsText(0)
desc = arcpy.Describe(layer)
selected = desc.FIDSet
if len(selected) == 0:
    arcpy.AddError("To continue, features must be selected!")

The script tool is set to accept one Feature Layer as an input parameter, and returns a boolean value as an output parameter. If that boolean returns false, you can stop the model. (Or you can do it all in Python without making a script tool and fiddling with input and output parameters.)

Edit: I should note that desc.FIDSet returns a semicolon-delimited string of the ObjectIDs of all selected features in the specified layer, so using len() on it can only tell you if there is a selection, not the number of selected objects.

  • That's really useful. I was curious where you would learn about this if you're not into arcobjects. I had never noticed it before but it's in a Code Snippet on this page, help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//…. It's not exposed in Esri's Desktop Help as a property. Are there other 'hidden' properties that can be used in Python? I've always been confused about what is available through Python.
    – cndnflyr
    Sep 16, 2013 at 20:32
  • Well this pointed me in the right direction. Thanks for your help. After looking at all the data types for the parameters I don't really understand how to reference the selection of the current map when setting up parameters in the script for the GetParameterAsText(0). I think that's why I'm getting error IOError: "" does not exist. Sep 17, 2013 at 23:57
  • 1
    If your input type is a Feature Layer, you should be presented with a drop-down menu before running the model, which will allow you to select any of the layers in the current document by the names they have in the Table of Contents. If you choose one of these, any selection on that layer will be passed to the model. This is why the input must be a Feature Layer and not a Feature Class (which is just a path to the raw data).
    – nmpeterson
    Sep 18, 2013 at 16:20

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.