1

ArcPy has a method of defining an active map regardless of its filename or path:

mxd = arcpy.mapping.MapDocument("CURRENT")

I wonder if there is a similar way to define an active layer. I need it so I can use it in a labeling script. I specifically want to avoid hard-coding the layer’s name.

1
  • 2
    The active layer isn't like the QGIS Active Layer, there isn't such a thing in ArcMap... there's focusmap (the active map) and activeview (the active view) but no such thing as an active layer. In the ArcObjects API you can address the table of contents and find (if any) layer is currently highlighted but that doesn't appear to be exposed to the arcpy interface so you'd have to go searching through the COM interface. – Michael Stimson Aug 18 '14 at 3:35
1

If you are wanting to do this as part of a Python AddIn for ArcMap then the pythonaddins module provides the means to do this using its GetSelectedTOCLayerOrDataFrame() function:

Returns the selected layer or data frame from the table of contents.

5
  • That seems to be a badly named function, since there may be more than one layer selected. The sample code also implies that the result will be a data frame ("df") – Stephen Lead Aug 18 '14 at 4:31
  • @Stephen "Active layer" has a long history in Arc* line of projects, as you know, being used to represent the layer selected in the table of contents (ie ArcView 2/3.x and beyond). Arc-speak generally uses "selected layer(s) now. Either term is appropriate if your intent is to focus operations to a particular layer or layers. Its only other common useage of this term is to represent the layer of ground that undergoes seasonal freeze/thaw cycles...(permafrost and GIS can be a confusing combination :) ) – user681 Aug 18 '14 at 4:52
  • myLyr = pythonaddins.GetSelectedTOCLayerOrDataFrame() - It seems like it may do the trick. Thanks! – Aleksandr Zhuk Aug 18 '14 at 5:03
  • @StephenLead I'm not sure if I have tested it with multiple items selected but my guess is that in that event it would take the first. The code sample in the doc works well to distinguish whether it is dealing with a data frame or layer. – PolyGeo Aug 18 '14 at 5:04
  • @DanPatterson it must be especially confusing when someone in the arctic circle says "ArcGIS has frozen" – Stephen Lead Aug 18 '14 at 5:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.