I need to run an ArcMap 10.8 Python Toolbox tool in background*. However, I need to add a layer to the MapDocument. In foreground execution, the current map can be accessed through the "CURRENT" keyword like this:
mxd = arcpy.mapping.MapDocument("CURRENT")
The documentation says nothing about referencing the MapDocument by its full path during background processing. However, when I tried the following
import arcpy
mxd = ap.mapping.MapDocument("CURRENT")
global path
path = mxd.filePath
del mxd
class Toolbox(...):
[...]
class SomeTool(...):
[...]
def execute(...):
global path
mxd = ap.mapping.MapDocument(path)
return
the tool fails with a severe error during background processing, but I cannot find out why. Also, I need to find some work-around to solve this issue...
* the tool absolutely needs these two properties: Not lock the ArcMap main window during execution and add/remove layers from the Map.