Select to view content in your preferred language

get active map from ArcGIS Pro project

388
9
07-11-2024 08:28 AM
nadja
by
Occasional Contributor III

Acually, I found various posts to how to get the active map of the current ArcGIS Pro project. they all start with this: 

aprx = arcpy.mp.ArcGISProject("CURRENT")
active_map = aprx.listMaps(aprx.activeMap.name)[0]

I always get the exception "CURRENT". I already tried "current", then I get the exception "current". any idea whats wrong or how to make it right?

Tags (2)
0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

See example 2, it needs to be run from an open project within Pro

ArcGISProject—ArcGIS Pro | Documentation

otherwise specify the project's full pathname

also don't forget

import arcpy


... sort of retired...
nadja
by
Occasional Contributor III

Hi, 

I have "import arcpy" and I copy-pasted the second example, but I still get Exception: CURRENT. Any idea why?

what means "sort of retired"?

0 Kudos
HaydenWelch
Occasional Contributor II

The second line is actually redundant as the activeMap parameter returns a map object, and the listMaps(name)[0] is just getting the same map object you're feeding into it.

0 Kudos
TonyAlmeida
Occasional Contributor III

 

 

import arcpy

# Get the current project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Get the active map
map = aprx.activeMap

# Print active map.

print("Active Map:", map.name) 

 

 

or

 

import arcpy

# Get the current project
aprx = arcpy.mp.ArcGISProject("CURRENT")

# Get the active map
active_map = aprx.listMaps(aprx.activeMap.name)[0]

# print active map.
print("Active Map:", active_map.name)

 

 

0 Kudos
HaydenWelch
Occasional Contributor II

Be careful overwriting builtins like 'map' with variable names, this can have unintended side effects if you have a long script and forget you did it.

 

If you absolutely have to use a built-in, try and mask it as an instance parameter so it's shielded by "self."/"instance." or if you need it in a larger scope, prefix it with an '_' ("_map")

0 Kudos
nadja
by
Occasional Contributor III

if I understand you correctly, you would recommend the second line to be this:

map = aprx.listMaps(aprx.activeMap.name)[0]
0 Kudos
HaydenWelch
Occasional Contributor II

You can simplify that by just saying:

_map = aprx.activeMap

As the activeMap attribute of an ArcGISProject object is a Map object (same as what's returned by listMaps)

If you keep getting the error, it means your project either doesn't have an active map, or your project is not open ("CURRENT" only works with *open* project files). If you need to do this on projects that are closed, you'll need to replace "CURRENT" with a full path to the .aprx file and replace .activeMap with either .listMaps()[0] for the first map in the project file (by order in contents). Or .listMaps('<mapName>')[0] for the first map that matches the '<mapName>' string.

 

Note: I prefixed your map variable with '_' for the reasons stated in my above comment. Shadowing builtin functions can have unintended side effects if you're doing it in the wrong scope. If you were to call 'map()' now it would raise a 'arcgis.Map object not Callable' error.

0 Kudos
nadja
by
Occasional Contributor III

Thanks, but I still get the same error message 😞

0 Kudos
mody_buchbinder
Occasional Contributor III

I always use 

map1 = aprx.activeMap

You should try to open a project using full path (of different aprx then the one you are open with), just for checking.

Are other arcpy command works?

Note that if you open a project by path you do not always have activeMap