5

Referencing this question, Adding new group layer with ArcPy?, I'm trying to use python to add a empty group within a group layer. So far I'm able to add a empty group within the map document, however I'm getting an error when trying to add another empty group within the existing group layer. Here is the error message:

Runtime error Traceback (most recent call last): File "", line 1, in File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\mapping.py", line 88, in AddLayerToGroup assert isinstance(target_group_layer, Layer) and target_group_layer._arc_object.isGroupLayer AssertionError

Here is my code:

        mxd = arcpy.mapping.MapDocument(r"C:\Temp\test.mxd")
        df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
        groupLayer = arcpy.mapping.Layer(r"C:\Temp\Group.lyr")
        arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
        targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group", df)
        addLayer = arcpy.mapping.Layer(r"C:\Temp\Group2.lyr")
        arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM") # error here
        mxd.save()
        del mxd,df

Can you add empty group layers within an existing group (using v10.1 SP1)? The ESRI documentation (see image below) states that you can add .lyr to groups (maybe not a .lyr group layer?).

enter image description here


Here is the working code (thanks to Jason Scheirer):

    mxd = arcpy.mapping.MapDocument(r"C:\Temp\test.mxd")
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    groupLayer = arcpy.mapping.Layer(r"C:\Temp\Group.lyr")
    arcpy.mapping.AddLayer(df, groupLayer, "BOTTOM")
    targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group", df)[0]
    addLayer = arcpy.mapping.Layer(r"C:\Temp\Group2.lyr")
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
    mxd.save()
    del mxd,df
1
  • Your supposed working code will not work, and it does not address the issue that your referenced solution gives.
    – Tom
    Commented Jan 30, 2016 at 13:51

2 Answers 2

3

Typo in your code. Should be:

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group", df)[0]

otherwise targetGroupLayer is a list.

1
  • i was wondering if someone could expand on how the group layer target layer in the code is defined. From the documentation its the group layer that the empty layer is going to be attached to. If it is within an MXD... does it have a physical string path. In the example above its groupLayer = arcpy.mapping.Layer(r"C:\Temp\Group.lyr") . How do you reference a layer that only exists inside a map ? Commented Jul 31, 2015 at 8:41
1

I think in your answer, if you change these two lines, it'll work better:

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group", df) --> include [0]
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")[0]  --> take off [0]

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.