+ Reply to Thread
Results 1 to 4 of 4

Thread: Layer On/Off Visibility using a Python list

  1. #1
    Kyle Gallagher
    Join Date
    Apr 2010
    Posts
    32
    Points
    6
    Answers Provided
    0


    0

    Default Layer On/Off Visibility using a Python list

    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.

    Code:
    list = ["Layer1", "Layer2", "Layer3", "Layer4", "Layer5", "Layer6"]
    for item in list:
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if item in lyr.name:
    lyr.visible = True
    print lyr.name + " turned ON"
    else:
    lyr.visible = False
    print lyr.name + " turned OFF"
    arcpy.RefreshActiveView
    Any thoughts? Thanks!
    Last edited by boston1685; 10-15-2012 at 01:24 PM.

  2. #2
    T. Wayne Whitley

    Join Date
    Apr 2010
    Posts
    1,026
    Points
    842
    Answers Provided
    118


    0

    Default Re: Layer On/Off Visibility using a Python list

    Use the 2nd param to your advantage. If you plug in each unique 'item' of your list on looping, the index will always be [0]. See this (scroll down or search for 'turn on visibility...' section):
    http://blogs.esri.com/esri/arcgis/20...arcpy-mapping/

  3. #3
    T. Wayne Whitley

    Join Date
    Apr 2010
    Posts
    1,026
    Points
    842
    Answers Provided
    118


    0

    Default Re: Layer On/Off Visibility using a Python list

    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. I'm not certain 'if item in layer.name' is executing properly...maybe do this instead:

    (after 'item in list' line):
    lyr = arcpy.mapping.ListLayers(mxd, item, df)[0]
    lyr.visible = True


    EDIT: 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):

    Code:
    for lyr in arcpy.mapping.ListLayers(mxd, "", df):
           if lyr.name in list:
                lyr.visible = True
           else:
                lyr.visible = False
    Last edited by Wayne_Whitley; 10-15-2012 at 04:32 PM.

  4. #4
    Kyle Gallagher
    Join Date
    Apr 2010
    Posts
    32
    Points
    6
    Answers Provided
    0


    0

    Default Re: Layer On/Off Visibility using a Python list

    Worked like a charm! Thanks!

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts