Listing layers in a group

10217
10
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)
10 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Did you ever throw in the print statements to see what was being returned at each step?

Updating group layer symbology in arcpy

start with little things, like moving the data frame to the top of the table of contents and drop the wildcard or temporarily remove anything other than the group layer from there as well to see what is returned.  Once you get a basic case documented, then you can add other layers back in, and then try your wildcards and indexing

0 Kudos
AndrewGehlot
New Contributor II

Yes, I did. What I was getting returned was just the name of the group layer, and not the layers within it. I was able to make a workaround by using a wildcard because all of my layers began with the same phrase, but I won't be able to make that solution more general, as I'm working with far more data tiles that have different names.

0 Kudos
FC_Basson
MVP Regular Contributor

Try this function:

def listGroupLayer(glayername):
    mxd = arcpy.mapping.MapDocument('CURRENT')
    df = mxd.activeDataFrame
    layers = arcpy.mapping.ListLayers(df)
    for l in layers:
        if l.isGroupLayer and l.name == glayername:
            print "Layers in " + glayername + ":"
            glayers = arcpy.mapping.ListLayers(l)
            for gl in glayers:
                print gl.name
                # apply symbology
                arcpy.ApplySymbologyFromLayer_management(gl,r"C:\GIS\lyrfiles\mylayersymbology.lyr")

# use as 
listGroupLayer("MyGroupLayer")
AndrewGehlot
New Contributor II

Thanks! I'm not in the office today, so I can't test it, but I will as soon as I get back. The ApplySymbologyFromLayer_management tool, however, doesn't work for me because it only applies symbology if there is a polygon within the feature class present; however I'm trying to apply all symbology for when I need to add polygons, so I'm using the UpdateLayer function.

I should also mention that I'm brand new to Python and any programming language, so some of this stuff is a bit over my head.

0 Kudos
Oliver_Burdekin
Occasional Contributor II

I've been trying to get something similar working with Arcpro. I've tried the following:

p = arcpy.mp.ArcGISProject("CURRENT")

m = p.listMaps()[0]

for lyr in m.listLayers():
    if lyr.isGroupLayer:
        glayers = m.listLayers(lyr)
        for gl in glayers:
            print(gl.name)

I only have one group layer for testing (the group layer has 5 layers in it) and there are 2 non-group layers.

The output is just printing the name of the initial group layer rather than those layers within the group. I suspect it's due to the changes in arcpy from arcMap to arcPro, specifically between ListLayers and listLayers

0 Kudos
FC_Basson
MVP Regular Contributor

I think your second listLayers is implemented incorrectly.

This works for me in ArcPro:

proj = arcpy.mp.ArcGISProject('CURRENT')
m = proj.listMaps()[0]
ml = m.listLayers()
for l in ml:
   if l.isGroupLayer:
      print(l)
      lyrs = l.listLayers()
      for lyr in lyrs:
         print(lyr)‍‍‍‍‍‍‍‍‍
Oliver_Burdekin
Occasional Contributor II

Thanks you're right! Got it running now.

0 Kudos
SutanMufti
New Contributor

thanks, this works for me in arcpro too

0 Kudos
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