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?