Get Currently Selected Layer in ArcMap Table of Contents

3540
6
02-12-2014 06:12 AM
by Anonymous User
Not applicable
I am developing a Python plugin that needs to get information about the currently selected layer in the ArcMap table of contents. I've looked through the arcpy.mapping module for an appropriate function and searched the internet but to no avail. Is there a way to get the name (or index) of the currently selected layer using arcpy? With either piece of information, I could then use arcpy.mapping.ListLayers() to get the appropriate layer object. In the sample below, I would like the python script to get either u'Nodes' (name) or 0 (index) from the selected layer.

[ATTACH=CONFIG]31347[/ATTACH]

Thanks for your help,

-Dave

ArcGIS 10.1 SP1
Tags (2)
6 Replies
JohnDye
Occasional Contributor III
Hi Dave,
The Python Addins module has a function specifically for this purpose.

import pythonaddins
SelectedLayer = pythonaddins.GetSelectedTOCLayerOrDataFrame()
print SelectedLayer

That should return the name of the layer the user has selected from the TOC, as it appears in the TOC.
0 Kudos
by Anonymous User
Not applicable
Sweet! Thanks, John. That works. I should have thought to look for the function in the pythonaddins module.
0 Kudos
MichaelVolz
Esteemed Contributor
How long has the python addins module been around for?
0 Kudos
by Anonymous User
Not applicable
The pythonaddins module was introduced with ArcGIS 10.1 when Python was added to the list of languages supported for desktop Add-In development. In addition to the pythonaddins module, there's a Python Add-In Wizard that (largely) automates the creation of Python classes for use in Python add-in projects. Check out the documentation for more.

-Dave
0 Kudos
MichaelVolz
Esteemed Contributor
Would you say that python Add-Ins and python scripts perform different operations?  If not, which option would be the easiest to deploy?
0 Kudos
JohnDye
Occasional Contributor III
Would you say that python Add-Ins and python scripts perform different operations?  If not, which option would be the easiest to deploy?


Python Addins are mostly for when you need to interact with the Map or Table of Contents in a dynamic manner (click on a point in the map and get the coordinates) or build some sort of custom GUI element (toolbar, button, combobox, tool, or tool palette) or listen for events within the session (item added to TOC, item removed from TOC, start/end an editing session, ect) and perform some sort of custom logic when those events occur.

I've found they're also really useful for neatly packaging and distributing Toolboxes as well as you can ship toolboxes and supporting datasets with it and then raise those toolboxes through a button you create on a python addin toolbar. This has very much simplified deploying tools for me as I no longer have to keep track of who has what version of the tool. I just edit the tool, sign it, post to to a common share and the next time they restart, ArcGIS automatically installs the update on their system. Everyone is on the same page of music (for good or bad) and they can get to the tools through a simple button that raises them instead of the convoluted 'My Toolboxes'  menu buried way way way down in the Catalog Window. I think the end result is a much more professional and polished presentation and simplified deployment.

Python addins are definately not for building custom script tools or python toolboxes (that's what script tools and python toolboxes are for, duh), but it definately makes deployment a whole lot easier. The GUI Functions are still kind of limited for now. No dockable windows (no ability to get user text input except through a combobox, no way to create custom gui elements outside of the methods they provide, limited interrogation of existing gui elements). I am hopeful that in time it will become much more robust with a form builder and ability to create dockable windows

Hope that helps.
0 Kudos