3 Replies Latest reply on Oct 16, 2012 6:23 AM by boston1685

    Layer On/Off Visibility using a Python list

    boston1685
      I am trying to export multiple maps through one mxd, toggling on/off specific layers for specific maps.  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.  There seems to be a problem once the for loop terminates checking the first list item.  Here is the error I am getting:

      AttributeError: LayerObject: Error in accessing Visible property
      referencing this line from the code below: lyr.visible = False

      Note that the mxd and df variables have been defined in code not included.

      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]


      Any thoughts?  Thanks!