1

I've been using ArcGIS 10.0 to create arcpy scripts. Now that I'm using ArcGIS 10.2, I try to run those scripts in the new environment but I get some errors. One of them is the following one, that I don't understand the reason why:

My script does the following process:

1- Add a .lyr file that corresponds to a Group Layer using AddLayer.

2- Add a layer to the layer group using AddLayertoGroup.

Reference mxd and dataframe:

> mxd = arcpy.mapping.MapDocument("CURRENT") df =
> arcpy.mapping.ListDataFrames(mxd)[0]

Reference the .lyr file:

lyrGr = arcpy.mapping.Layer("C:\\path\\newLyrGr.lyr")

Add the layer group:

arcpy.mapping.AddLayer(df, lyrGr)

Reference a layer to be added to the layergroup:

feat_layer = arcpy.mapping.Layer("C:\\path\\fcgdb.gdb\\fc")

Add layer to layergroup:

arcpy.mapping.AddLayerToGroup(df, lyrGr, feat_layer)

This process worked with ArcGIS10.0. However, in ArcGIS10.2, the AddtoLayerGroup method fails and the following error is thrown:

*

Runtime error 
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 98, in AddLayerToGroup
    data_frame._arc_object.InsertLayer(target_group_layer._arc_object, my_copy)
ValueError: DataFrameObject: Unexpected error

*

To make it work, I need to reference the layer_group as following:

newlyrGr = arcpy.mapping.ListLayers(df)[0]

Then, all works!

arcpy.mapping.AddLayerToGroup(df, newlyrGr, feat_layer)

The difference between both methods is that the first references a layer file and the second references a layer in the TOC. However, when calling the isGroupLayer method, both return True. My qüestion is, why does it happen?

1 Answer 1

3

The group layer you try to access with AddLayerToGroup is supposed to be in the TOC of your mxd, not on your disk... Refering to it as the .lyr file on your disk shouldn't work, it's weird that you could do it with 10.0.

The AddLayerToGroup help page says (same in 10.0 and 10.2):

AddLayerToGroup does not allow you to add layers to group layers within a layer file. Layer files should be managed and authored using ArcMap.]1

1
  • In both 10.0 and 10.2 I have the layer in the TOC. However, referencing it as a layer file in the disk it is enough to point the layer in the TOC in 10.0 to use AddLayerToGroup.
    – Maik
    Mar 23, 2015 at 13:28

Your Answer

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

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