ArcGIS Pro Get Active Layer

214
4
04-08-2021 11:10 AM
Darrin_Smith
New Contributor

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 = arcpy.mp.ArcGISProject("CURRENT")
currentmap = aprx.activeMap
lyr = currentmap.listLayers("County")[0]

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

0 Kudos
4 Replies
DavidBlanchard
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
Darrin_Smith
New Contributor

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

0 Kudos
DuncanHornby
MVP Frequent 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!

0 Kudos
Darrin_Smith
New Contributor

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.  

0 Kudos