Select to view content in your preferred language

Arcpy get the name of a selected feature's layer

6412
9
Jump to solution
01-30-2017 09:11 PM
DavidMatthews2
Occasional Contributor

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? 

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

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()]
...             
>>> 

View solution in original post

9 Replies
DarrenWiens2
MVP Honored Contributor

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.

Map—ArcPy | ArcGIS Desktop 

Layer properties—ArcPy Functions | ArcGIS Desktop 

Understanding validation in script tools—Geoprocessing and Python | ArcGIS Desktop 

DavidMatthews2
Occasional Contributor

Thank you. See my solution below

0 Kudos
JoeBorgione
MVP Emeritus

I don't have an answer, but it's just cool to reply to a post from Dave Mathews... 

Image result for dave matthews band

That should just about do it....
JoshuaBixby
MVP Esteemed Contributor

This question seems related to Get Currently Selected Layer in ArcMap Table of Contents‌, which suggests using the pythonaddins module.

DavidMatthews2
Occasional Contributor

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... 

JoshuaBixby
MVP Esteemed Contributor

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? 

0 Kudos
DarrenWiens2
MVP Honored Contributor

I took it to mean "the feature layer with a selected feature" not "the selected feature layer".

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

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()]
...             
>>> 
DavidMatthews2
Occasional Contributor

Thanks bixb0012‌ that is more elegant.

Sorry for not being clearer with my question 

0 Kudos