Select to view content in your preferred language

Access current MapDocument not working in .pyt Tool background execution: Is there a workaround?

450
0
01-26-2023 04:06 AM
Labels (1)
Manu
by
Regular Contributor

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.

0 Kudos
0 Replies