Select to view content in your preferred language

Listing layers in a group

15147
11
06-21-2016 08:27 PM
AndrewGehlot
New Contributor II

Hello.

I am trying to use arcpy to list all of the layers in a group to apply symbology to all of them at once. When I use the arcpy.mapping.ListLayers(mxd, "Group Layer", df), it only returns the name of the group layer and not the layers within the group, so I am unable to iterate through them. I've tried several different queries, but all I seem to get it to do is list every single layer in the document (not what I want) or just the group layer name (also not what I want).

Does anyone know how to specify it to list all of the layers in the group? The ArcGIS help tells me it should be able to do that, listing first the name of the group layer followed by each layer within the group, but it is not working for me.

Tags (2)
11 Replies
andrewj_ca
Occasional Contributor II

Hey Andrew,

Here's a snippet from some python that I use to export all of the layer files from an MXD.  I'm sure you can modify for your own purpose.

for df in arcpy.mapping.ListDataFrames(mxd, ""):

  for lyr in arcpy.mapping.ListLayers(mxd, "", df):

       if lyr.isFeatureLayer or lyr.isRasterLayer or lyr.isGroupLayer:

            print "Layer: " + lyr.name

            try:

                 lyr.saveACopy(lyr_dir+lyr.name+".lyr")

            except:

                 print lyr.name + " had a little issue"

0 Kudos
AndrewGehlot
New Contributor II

Thanks! I'll play with it when I'm back in the office.

0 Kudos