English

How To: Toggle the visibility of a specific sublayer of a group layer using Python script

Summary

Users are unable to view the desired sublayer in a set of group layers after toggling off the visibility for the group layer using Python.

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")

Related Information

Last Published: 1/11/2021

Article ID: 000013577