2

I am about to save copies of all of the layers in my mxd. I only want to save the top level group layers though. I have some group layers within group layers - I dont want to save them individually. So far I can export all group layers like this:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
 if lyr.isGroupLayer == True:
     lyr.saveACopy(lyr.name.replace(":"," -").replace("&","and").replace(">","") + ".lyr")

What I'm looking for is either a property that says what level the layer is on so I could say if layer level = top level... (instread of if lyr.isGroupLayer == True:) or a property that says that the layer is within a group layer so I could say if lyr.isInGroupLayer == False:.

I suppose this could be in my for loop or a change to the mapping.ListLayers

Any ideas?

1 Answer 1

1

You should check, longName property of the layer to see whether it has backslashes or not:

Here is the snippet:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.isGroupLayer == True:
        if lyr.longName.find('\\') == -1:
            print lyr.longName

I hope it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.