<?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: Layer On/Off Visibility using a Python list in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668367#M51879</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Actually I see another error, but this only points out the code is a little difficult to read... More succinct code is at the blog link I sent on the previous post.&amp;nbsp; I'm not certain 'if item in layer.name' is executing properly...maybe do this instead:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(after 'item in list' line):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr = arcpy.mapping.ListLayers(mxd, item, df)[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.visible = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&amp;nbsp; I thought about this again, and if you want to toggle on what's in the list; off what isn't, then I don't see why you don't do it the following way? (ListLayers returns layer objects in a list, so simply loop the list, get the layer name, and compare the string to that in your 'list' of strings):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for lyr in arcpy.mapping.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = False&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:14:52 GMT</pubDate>
    <dc:creator>T__WayneWhitley</dc:creator>
    <dc:date>2021-12-12T04:14:52Z</dc:date>
    <item>
      <title>Layer On/Off Visibility using a Python list</title>
      <link>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668365#M51877</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am trying to export multiple maps through one mxd, toggling on/off specific layers for specific maps.&amp;nbsp; My thought is that if I create a list with the string layer names in it, I should be able to cycle through the TOC and turn on those layers, otherwise turn it off.&amp;nbsp; There seems to be a problem once the for loop terminates checking the first list item.&amp;nbsp; Here is the error I am getting:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;AttributeError: LayerObject: Error in accessing Visible property&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;referencing this line from the code below: lyr.visible = False&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that the mxd and df variables have been defined in code not included.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;list = ["Layer1", "Layer2", "Layer3", "Layer4", "Layer5", "Layer6"]
for item in list:
[INDENT]for lyr in arcpy.mapping.ListLayers(mxd, "", df):[/INDENT]
[INDENT][INDENT]if item in lyr.name:[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]lyr.visible = True[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT][INDENT]print lyr.name + " turned ON"[/INDENT][/INDENT][/INDENT]
[INDENT][INDENT]else:[/INDENT][/INDENT]
[INDENT][INDENT][INDENT]lyr.visible = False[/INDENT][/INDENT][/INDENT]

[INDENT][INDENT][INDENT]print lyr.name + " turned OFF"[/INDENT][/INDENT][/INDENT]

[INDENT]arcpy.RefreshActiveView[/INDENT]&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any thoughts?&amp;nbsp; Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Oct 2012 20:11:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668365#M51877</guid>
      <dc:creator>KyleGallagher</dc:creator>
      <dc:date>2012-10-15T20:11:47Z</dc:date>
    </item>
    <item>
      <title>Re: Layer On/Off Visibility using a Python list</title>
      <link>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668366#M51878</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use the 2nd param to your advantage.&amp;nbsp; If you plug in each unique 'item' of your list on looping, the index will always be [0].&amp;nbsp; See this (scroll down or search for 'turn on visibility...' section):&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://blogs.esri.com/esri/arcgis/2010/12/14/combining-data-driven-pages-with-python-and-arcpy-mapping/"&gt;http://blogs.esri.com/esri/arcgis/2010/12/14/combining-data-driven-pages-with-python-and-arcpy-mapping/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 15 Oct 2012 21:50:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668366#M51878</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2012-10-15T21:50:05Z</dc:date>
    </item>
    <item>
      <title>Re: Layer On/Off Visibility using a Python list</title>
      <link>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668367#M51879</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Actually I see another error, but this only points out the code is a little difficult to read... More succinct code is at the blog link I sent on the previous post.&amp;nbsp; I'm not certain 'if item in layer.name' is executing properly...maybe do this instead:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(after 'item in list' line):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr = arcpy.mapping.ListLayers(mxd, item, df)[0]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;lyr.visible = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT:&amp;nbsp; I thought about this again, and if you want to toggle on what's in the list; off what isn't, then I don't see why you don't do it the following way? (ListLayers returns layer objects in a list, so simply loop the list, get the layer name, and compare the string to that in your 'list' of strings):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for lyr in arcpy.mapping.ListLayers(mxd, "", df):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.name in list:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = True
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.visible = False&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:14:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668367#M51879</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T04:14:52Z</dc:date>
    </item>
    <item>
      <title>Re: Layer On/Off Visibility using a Python list</title>
      <link>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668368#M51880</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Worked like a charm! Thanks!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 16 Oct 2012 13:23:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/layer-on-off-visibility-using-a-python-list/m-p/668368#M51880</guid>
      <dc:creator>KyleGallagher</dc:creator>
      <dc:date>2012-10-16T13:23:58Z</dc:date>
    </item>
  </channel>
</rss>

