Select to view content in your preferred language

ArcGIS Pro Get Active Layer

04-08-2021 11:10 AM
New Contributor II

In Python ArcGIS Pro 2.7, I'm trying to get the active layer in a Map TOC.  I can get the CURRENT project and active map, but I've met my threshold to get the active layer in the map.  I thought it might be a property of listlayers or even the active map, but not the case.

Here is a part of my code.  Ultimately, I'm trying to get the active layer and pass a Definition Query to it.  Below I have the layer in question (County) hardcoded, but I want to replace this with the active layer variable. After I get the lyr, then I build a Def Query using pre-defined input.

aprx ="CURRENT")
currentmap = aprx.activeMap
lyr = currentmap.listLayers("County")[0]

qry = '\"%s\" = \'%s\'' %(in1,in2)
lyr.definitionQuery = qry

4 Replies
Esri Contributor

Could you explain what you mean by "active layer"? Do you mean the layer that was last clicked by the user in the table of content and that is highlighted in blue? Or are you looking for layers that are visible (checkbox checked)?

0 Kudos
New Contributor II

Yes it the blue highlighted layer in the Table of Contents.

0 Kudos
MVP Notable Contributor

I don't believe this is possible in the current release of ArcPro. What you see in the TOC is a node in a tree view, which you are interpreting as a layer in the map. I assume its a treeview, I don't actually know, but either way its a "thing" you interact with on the main application interface. So being selected is not a property of a layer that you can query or use in python/arcpy.

If you were using ArcMap you could get to that vital bit of information, this is discussed here.

If you have the skills and were willing to create an add in using say VB or c# you can also get to that information using code as discussed here.

It's not even exposed using the CIM module.

So suggest it as an idea, I would vote for it!

Tags (1)
0 Kudos
New Contributor II

That's unfortunate.  I used to do this quite often in VBA with ArcMap.  It really opens up your options for scripting.  It's interesting how the fundamentals of Pro (from the contextual menu perspective) must work regarding the Tree View?  This applies even down to the Geoprocessing tool level as well.  For example the Select by Location tool will automatically know which layer is TOC highlighted/selected and pre-populate the input.