I would like to automatically populate a parameter in a toolbox with the name of the currently selected objects layer.
Is this possible in Arcpy?
Solved! Go to Solution.
I guess you read minds better than I do.
If your interpretation is correct, which it seems to be given the OPs final code snippet, the results can also be obtained using getSelectionSet() on the layer object itself:
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> sel_lyrs = [lyr.name
... for lyr
... in arcpy.mapping.ListLayers(mxd)
... if lyr.getSelectionSet()]
...
>>>
I believe you'd have to list the layers (map object) and inspect the FIDSet property (layer object), and update the parameter with tool validation.
Layer properties—ArcPy Functions | ArcGIS Desktop
Understanding validation in script tools—Geoprocessing and Python | ArcGIS Desktop
Thank you. See my solution below
I don't have an answer, but it's just cool to reply to a post from Dave Mathews...
This question seems related to Get Currently Selected Layer in ArcMap Table of Contents, which suggests using the pythonaddins module.
Thank you all for the help, especially Darren Wiens for pointing me to fidSet.
I went with this:
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)
sel_layers=[str(i.name) for i in layers if arcpy.Describe(i.name).fidSet]
which returns a list of the selected layers.
Also found this helpful arcgis 10.1 - Getting all results from Selection made by Attribute or Location using ArcPy? - Geogra...
Looking at your original question and the code you provided, I am a bit confused by your terminology. What exactly do you mean by "name of the currently selected objects layer?" Do you mean the layer or layers that a user has highlighted/selected in the table of contents?
I took it to mean "the feature layer with a selected feature" not "the selected feature layer".
I guess you read minds better than I do.
If your interpretation is correct, which it seems to be given the OPs final code snippet, the results can also be obtained using getSelectionSet() on the layer object itself:
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> sel_lyrs = [lyr.name
... for lyr
... in arcpy.mapping.ListLayers(mxd)
... if lyr.getSelectionSet()]
...
>>>
Thanks bixb0012 that is more elegant.
Sorry for not being clearer with my question