HOW TO
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.
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.
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()
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")
Get help from ArcGIS experts
Download the Esri Support App