4

I'm working on a script in ArcMap 10.1 that takes a screenshot and I need to know how to extinguish (turn off) a layer in my table of contents.

17

The Layer object has a read/write property, visible, that can be used to turn on/off layers.

This is some basic code that should get you started:

#A list of layer names that you want to be turned off.
names = [x,y,z,etc]

mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)

for layer in layers:
  if layer.name in names:
    layer.visible = False

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
3

I wanted to add something I hope is helpful from my experience. The above answer works great, though if someone does further analysis with the layer than this example, order of use of the .visible property becomes important.

For example, trying to add many "turned off" layers to dataframe as follows:

for lyr in layerlist:
     addlayer = arcpy.mapping.Layer(os.path.join(basepath, lyr))
     arcpy.mapping.AddLayer(your_dataframe, addlayer, "BOTTOM")
     addlayer.visible = False #Puts the property after the .AddLayer analysis- won't turn off the layer
# Do further analysis

Won't turn off the layers as you add them. But, the following will:

for lyr in layerlist:
     addlayer = arcpy.mapping.Layer(os.path.join(basepath, lyr))
     addlayer.visible = False # Moves the property before the .AddLayer analysis turns off layer
     arcpy.mapping.AddLayer(your_dataframe, addlayer, "BOTTOM")
# Do further analysis

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.