[GIS] Arcpy ListLayers function returns only the first sub layer in a group layer (as opposed to the group layer AND all sub layers of that group)

arcgis-10.1arcpy

I thought the arcpy.mapping.ListLayers function in ArcGIS 10.1 was supposed to return group layers AND all of their sub-layers, but my script is only outputting the first sub-layer in a group layer. Can anyone see why by looking at my code? Thanks! (p.s. the purpose of my code is to output a text file – code omitted – that lists all of the layers in a specified directory and the layers' properties).

In ArcGIS 10.1 Desktop Help it says:

Group layers are treated just like layers. The index values are simply generated from top to bottom as they appear in the table of contents or the way they would appear in a layer file.

Code:

layerList = arcpy.ListFiles("*.lyr")
for layer in layerList:
    path = os.path.join(directory, layer)
    lyrFile = arcpy.mapping.Layer(path)

    for lyr in arcpy.mapping.ListLayers(lyrFile):

        try:
            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and lyr.supports("DEFINITIONQUERY") and lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, str(lyr.transparency), str(lyr.visible), lyr.definitionQuery, str(lyr.showLabels), str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, str(lyr.transparency), str(lyr.visible), lyr.definitionQuery, " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if lyr.supports("DATASETNAME") and not lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, lyr.datasetName, " ", " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

            if not lyr.supports("DATASETNAME") and not lyr.supports("TRANSPARENCY") and not lyr.supports("DEFINITIONQUERY") and not lyr.supports("SHOWLABELS"):
                doc.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}"
                          .format(lyr.name, " ", " ", str(lyr.visible), " ", " ", str(lyr.minScale), str(lyr.maxScale)))
                doc.write("\n")
                break

        except KeyError:
            pass

Best Answer

Your loop is apparently terminating after the first pass. What if you remove the 'break's and replace the 'if's with 'elif's?

Related Question