<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Access current MapDocument not working in .pyt Tool background execution: Is there a workaround? in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/access-current-mapdocument-not-working-in-pyt-tool/m-p/1251813#M4270</link>
    <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/mapdocument-class.htm" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt; says nothing about referencing the MapDocument by its full path during background processing. However, when I tried the following&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* the tool absolutely needs these two properties: Not lock the ArcMap main window during execution and add/remove layers from the Map.&lt;/P&gt;</description>
    <pubDate>Thu, 26 Jan 2023 12:06:31 GMT</pubDate>
    <dc:creator>Manu</dc:creator>
    <dc:date>2023-01-26T12:06:31Z</dc:date>
    <item>
      <title>Access current MapDocument not working in .pyt Tool background execution: Is there a workaround?</title>
      <link>https://community.esri.com/t5/arcmap-questions/access-current-mapdocument-not-working-in-pyt-tool/m-p/1251813#M4270</link>
      <description>&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The &lt;A href="https://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/mapdocument-class.htm" target="_blank" rel="noopener"&gt;documentation&lt;/A&gt; says nothing about referencing the MapDocument by its full path during background processing. However, when I tried the following&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;* the tool absolutely needs these two properties: Not lock the ArcMap main window during execution and add/remove layers from the Map.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Jan 2023 12:06:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/access-current-mapdocument-not-working-in-pyt-tool/m-p/1251813#M4270</guid>
      <dc:creator>Manu</dc:creator>
      <dc:date>2023-01-26T12:06:31Z</dc:date>
    </item>
  </channel>
</rss>

