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?
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:
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> sel_lyrs <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>getSelectionSet<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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
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 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> layers <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN> sel_layers<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">[</SPAN>str<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> layers <SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Describe<SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>fidSet<SPAN class="punctuation token">]</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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? - Geographic Information Sys…
Thank you. See my solution below
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".
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.