JohnWallX

Add Dataframe or Group Layer and populate

Discussion created by JohnWallX on Dec 4, 2011
Latest reply on Jan 23, 2013 by graeme
I am trying to run a script which will add all created layers to the current .mxd, but grouped together by a common attribute. I have found the following script elsewhere on this forum:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "County Maps")[0]
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "24000 Scale Data", df)[0]
addLayer = arcpy.mapping.Layer(r"C:\Project\Data\StreetsWithLabels.lyr")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
mxd.saveACopy(r"C:\Project\Project2.mxd")
del mxd, addLayer

I have modified it as follows:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:/gis/gis540/Project/test.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]
targetGroupLayer = [0]
addLayer = arcpy.mapping.Layer(r"C:/gis/gis540/Project/firehydrants/add6.shp")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")
mxd.saveCopy(r"C:/gis/gis540/Project/test2.mxd")
del mxd, addLayer

However, I keep getting an Assertion Error. I cannot figure out what the problem is with the modified code.

Outcomes