I am using ArcGIS 10.1 and am using Python to try to add a layer (.lyr file) to a group layer. What I have is this:
mxd = arcpy.mapping.MapDocument(NEW_MXD)
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]
# Add group layer
groupLayer = arcpy.mapping.Layer(r"EmptyGroupLayer.lyr")
arcpy.mapping.AddLayer(dataFrame, groupLayer, "TOP")
# Add layer to group layer
tmpLayer = arcpy.mapping.Layer(r"zipcodes.lyr")
arcpy.mapping.AddLayerToGroup(dataFrame, groupLayer, tmpLayer, "BOTTOM")
I verified that the "groupLayer" is a group layer and tmpLayer is not. I get the following error from a stack trace when the AddLayerToGroup is executed:
---------------------------------------------
Error: DataFrameObject: Unexpected error
Traceback (most recent call last):
File "main.py", line 78, in <module>
main()
File "main.py", line 46, in main
arcpy.mapping.AddLayerToGroup(dataFrame, groupLayer, tmpLayer, "BOTTOM")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\utils.py", line 18
1, in fn_
return fn(*args, **kw)
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\mapping.py", line
93, in AddLayerToGroup
data_frame._arc_object.InsertLayer(target_group_layer._arc_object, my_copy,
len(lyrlist)+1)
ValueError: DataFrameObject: Unexpected error
---------------------------------------------
Not sure what I am missing as I feel that I am following the same process as other examples I have seen on the web.
Any thoughts on what the value error for the data frame might be?
Thanks - Peter
where is New_MXD defined?
The NEW_MXD is just a variable assigned earlier that has the name of an MXD file:
NEW_MXD = "temp.mxd"
If I simply add the tmpLayer via the arcpy.mapping.AddLayer to add it as a layer to the data frame, all is good. Both the group layer and the tmp layer are displayed properly in the MXD. Something is just happening when I try to add the tmpLayer via the AddLayerToGroup.
Unless your environments are set, via arcpy, I thought the path to the project needed to be set explicitly as to the folder and filename...could be wrong
The arcpy.env.workspace was set earlier to the path where all of the files are. Because everything else is working fine with regards to the MXD, the dataFrame, and adding layers, it would seem like a mute point even if it wasn't set. This may be because the script is being run in the same directory as where all of the other files exist. I am going to run a test where I read in an MXD that already contains a group layer and then see if I can add a layer to that group layer. My thought is maybe after I initially added that group layer to the dataFrame, maybe I needed to do something like a save or a refresh or something because maybe the AddLayerToGroup didn't see that the group layer was added. That doesn't make sense but who knows.
Okay, so I think I found what was going on. The AddLayerToGroup takes a DataFrame as its first argument. The groupLayer:
groupLayer = arcpy.mapping.Layer(r"EmptyGroupLayer.lyr")
was never part of the main DataFrame (from the MXD) even after I added it:
arcpy.mapping.AddLayer(dataFrame, groupLayer)
I guess the error was thrown because the groupLayer was not recognized as being part of the dataFrame that was sent in as an argument to the AddLayerToGroup:
arcpy.mapping.AddLayerToGroup(dataFrame, groupLayer, tmpLayer)
In order to get it to work, all I did was retrieve the groupLayer from the dataFrame after I added it:
groupLayer = arcpy.mapping.Layer(r"EmptyGroupLayer.lyr")
groupLayer.name = "AAAA"
arcpy.mapping.AddLayer(dataFrame, groupLayer)
groupLayer = arcpy.mapping.ListLayers(mxd, "AAAA", dataFrame)[0]
Then when I did the AddLayerToGroup, all was well. Why I had to retrieve the layer from the dataFrame after adding it to it is a bit odd, but everything seems to work when I do that.
Grief...at least it works
Yeah, thanks for your replies Dan. This may all have been avoided if the arcpy library allowed you to create a Group Layer object outright, but apparently it doesn't and that is why people had used alternatives like reading in a .lyr file like I was doing.