I only know how to turn off the entire current layer. But I want off only some of the layers specified

#turn all layers in current map doc on:
print "Button2--turn on layers"
mxd = arcpy.mapping.MapDocument("Current")
list_layers = arcpy.mapping.ListDataFrames()
for layer in list_layers:
    layer.visible = True

1 Answer 1


Just check for the name of the layers you want to turn off:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, '')[0]

layersOff = ['layer2', 'layer3', 'layer5'] # Names of the layers you want turned off

list_layers = arcpy.mapping.ListLayers(mxd, '', df)
for layer in list_layers:
    layer.visible = True
    if layer.name in layersOff:
        layer.visible = False


In this example, any layer names that match a name in layersOff are turned off, everything else is turned on.

Your list_layers was actually trying to get a list of Data Frames not layers, so I've added a arcpy.ListDataFrames() and corrected your arcpy.ListLayers().

  • Thank you for your answer. But what to do with the following error ?
    – jaehee
    Mar 27, 2016 at 17:13
  • df = arcpy.mapping.ListDataFrames(mxd, "layersOff")[0] IndexError: list index out of range
    – jaehee
    Mar 27, 2016 at 17:13
  • take layersOff out of that line. Should be df = arcpy.mapping.ListDataFrames(mxd, "")[0]
    – Midavalo
    Mar 27, 2016 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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