HOW TO

Toggle the visibility of a specific sublayer of a group layer in ArcMap using Python script

Last Published: November 30, 2021

Summary

After toggling off the visibility for the group layer using Python, users are unable to view the desired sublayer in a set of group layers.
The sublayer inherits the visibility of the group layer, hence, any attempt to toggle the visibility of the sublayer does not work if the group layer is marked as invisible.
The Python method isGroupLayer does not conventionally allow users to toggle off the visibility of the remaining layers in the group without disabling the visibility of the sub-layer selected.

Procedure

The following code section allows the user to toggle the visibility of the parent group layers without affecting the visibility of the selected sub-layer. By defining the parameter of the method layer.longName as the desired sub-layer, users can retain the visibility of the selected sub-layer.

  1. In ArcMap, click Geoprocessing > Python.
  2. Enter the following code block:
import arcpy
mxd = arcpy.mapping.MapDocument("current")
data_frame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd)

for layer in layers:
  if layer.isGroupLayer:
    layer.visible = True
  if layer.longName =="Group Name\SubLayer Name":
    layer.visible = True

arcpy.RefreshTOC()
arcpy.RefreshActiveView()
  1. Press Enter.

The same code block can also be saved and executed through a standalone (.py file) script. However, it is advisable to start ArcMap prior to executing the script, to be able to analyze the results.

To use the above code block as a standalone script, replace:

mxd = arcpy.mapping.MapDocument("current")

with the Windows directory location of the MXD:

mxd = arcpy.mapping.MapDocument("C:\ C:\Users\UserName\Documents\TestFiles")

Article ID: 000013577

Software:
  • ArcMap 10 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options