I have a process that adds a layer from a lyrx file to a group layer on the map. I need to add something into my script to check to see if it already exists so it doesn't add it again but I'm struggling to find anything online about this for ArcGIS Pro. Here's what I currently have (sorry I'm new at this):
LyrDir <SPAN class="operator token">=</SPAN> <SPAN class="string token">"C:\\mydir\\"</SPAN>
off_pk <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Offline group layer\\"</SPAN>
aprx <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>ArcGISProject<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN>
aprxMap <SPAN class="operator token">=</SPAN> aprx<SPAN class="punctuation token">.</SPAN>listMaps<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Map"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
offGrp <SPAN class="operator token">=</SPAN> aprxMap<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Offline group layer"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
my_lyr <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>LayerFile<SPAN class="punctuation token">(</SPAN>LyrDir <SPAN class="operator token">+</SPAN> <SPAN class="string token">"myfile.lyrx"</SPAN><SPAN class="punctuation token">)</SPAN>
aprxMap<SPAN class="punctuation token">.</SPAN>addLayerToGroup<SPAN class="punctuation token">(</SPAN>offGrp<SPAN class="punctuation token">,</SPAN>my_lyr<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
offlyr <SPAN class="operator token">=</SPAN> aprxMap<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN>off_pk <SPAN class="operator token">+</SPAN> <SPAN class="string token">"my layer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> layer <SPAN class="keyword token">in</SPAN> offlyr<SPAN class="punctuation token">:</SPAN>
layer<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>I was hoping it would be as simple as typing something like:
<SPAN class="keyword token">if</SPAN> layer<SPAN class="punctuation token">.</SPAN>exists<SPAN class="punctuation token">(</SPAN>mylayer<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">:</SPAN>
aprxMap<SPAN class="punctuation token">.</SPAN>addLayerToGroup<SPAN class="punctuation token">(</SPAN>offGrp<SPAN class="punctuation token">,</SPAN>my_lyr<SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
offlyr <SPAN class="operator token">=</SPAN> aprxMap<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN>off_pk <SPAN class="operator token">+</SPAN> <SPAN class="string token">"my layer"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> layer <SPAN class="keyword token">in</SPAN> offlyr<SPAN class="punctuation token">:</SPAN>
layer<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
If anyone can help, thanks in advance.