Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Referencing this question, Add a new group layer with python ArcGIS 10, 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

Thanks

UPDATE

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)
    addLayer = arcpy.mapping.Layer(r"C:\Temp\Group2.lyr")
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")[0]
    mxd.save()
    del mxd,df
share|improve this question
    
In your update I think you have placed @JasonScheirer's correct advice of "[0]" on wrong line - I suspect it was correct in your revised script but typed in wrong place here. –  PolyGeo Jan 27 '13 at 8:26
add comment

1 Answer 1

up vote 2 down vote accepted

Typo in your code. Should be:

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

otherwise targetGroupLayer is a list.

share|improve this answer
    
That was it, thx! –  artwork21 Jan 24 '13 at 20:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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