<?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 Re: Understanding arcpy.mp's addLayerToGroup in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410230#M70325</link>
    <description>&lt;P&gt;Don't want to edit the original post because of this forum's added-line bug.&lt;/P&gt;&lt;P&gt;Here's the tool side of that parameter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_0-1713275907815.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/101016iB9F432D22FF502E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_0-1713275907815.png" alt="MErikReedAugusta_0-1713275907815.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Apr 2024 13:58:36 GMT</pubDate>
    <dc:creator>MErikReedAugusta</dc:creator>
    <dc:date>2024-04-16T13:58:36Z</dc:date>
    <item>
      <title>Understanding arcpy.mp's addLayerToGroup</title>
      <link>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410228#M70324</link>
      <description>&lt;P&gt;So, I'm genuinely confused by this.&lt;/P&gt;&lt;P&gt;I have a python script tool that needs to go find a SID file in our directory, pull it into the map, and add it to an already-existing Group Layer.&lt;/P&gt;&lt;P&gt;The Group Layer is&amp;nbsp;&lt;EM&gt;provided to the tool by the user&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;Simplified code below:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;aprx = arcpy.mp.ArcGISProject('CURRENT')
mxd = aprx.activeMap

recordLayerGrp = arcpy.GetParameter(7) # Note that I'm not pulling as Text;
                                       #   this comes through as an
                                       #   arcpy.mp.Layer object

# [... intermediate code where I work with this and other layer objects just fine...]

mxd.addLayerToGroup(recordLayerGrp,  # At the point the function gets this 
                                     #   variable, it's unchanged from input;
                                     #   I pass it directly from the input
                                     #   function to this function.
                    fileLayerObj,    # This is also an arcpy.mp.Layer object
                    'TOP',
                   )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This&amp;nbsp;&lt;EM&gt;consistently&lt;/EM&gt; fails with a ValueError.&amp;nbsp; After a bunch of headache, I managed to determine that said error is referencing the arcpy.mp.Layer object behind recordLayerGrp.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I went and did quite a bit of spelunking on this issue yesterday and today, and finally added a single line to the code (line 2, below):&lt;/P&gt;&lt;LI-CODE lang="python"&gt;recordLayerGrp = arcpy.GetParameter(7)
recordLayerGrp = [lyr for lyr in mxd.listLayers() if lyr.name == recordLayerGrp.name][0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;The result of this is&amp;nbsp;still an arcpy.mp.Layer object, but &lt;EM&gt;it's a&amp;nbsp;different instance&lt;/EM&gt;&lt;/STRONG&gt;.&amp;nbsp; And this different one suddenly magically works.&amp;nbsp; I even added debug statements before and after line 2, to see what was in recordLayerGrp at each step:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_0-1713275709137.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/101015i4A0C89D61C06ED80/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_0-1713275709137.png" alt="MErikReedAugusta_0-1713275709137.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The tool inputs directly ask for the Group Layer and the user selects it.&amp;nbsp; Why did I have to "convert" the arcpy.mp.Layer object and go look for that Group Layer again?&amp;nbsp; Why doesn't the tool pull the "correct" instance of that object?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 13:56:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410228#M70324</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2024-04-16T13:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding arcpy.mp's addLayerToGroup</title>
      <link>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410230#M70325</link>
      <description>&lt;P&gt;Don't want to edit the original post because of this forum's added-line bug.&lt;/P&gt;&lt;P&gt;Here's the tool side of that parameter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MErikReedAugusta_0-1713275907815.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/101016iB9F432D22FF502E4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MErikReedAugusta_0-1713275907815.png" alt="MErikReedAugusta_0-1713275907815.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 13:58:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410230#M70325</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2024-04-16T13:58:36Z</dc:date>
    </item>
    <item>
      <title>Re: Understanding arcpy.mp's addLayerToGroup</title>
      <link>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410232#M70326</link>
      <description>&lt;P&gt;Your &lt;FONT face="courier new,courier"&gt;mxd&lt;/FONT&gt; variable name is messing with me! What version of Pro are you using?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2024 14:12:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/understanding-arcpy-mp-s-addlayertogroup/m-p/1410232#M70326</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2024-04-16T14:12:12Z</dc:date>
    </item>
  </channel>
</rss>

