So I am trying to add a layer to a group, but running into problems most likely because of the file is a .tif. I have tried quite a few different iterations of this and looked at a few solutions but nothing seems to work, any ideas? I have also tried entering a [0] behind the listlayers command
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "15MinMaps", df)
for row in cursor:
Sel = (row.getValue(field))
for dirpath, dirnames, filenames in arcpy.da.Walk(r"Z:/IMAGES/MAPS/15_Min_Thematic_Maps",topdown=True, followlinks=True, type="TIF"):
for filename in filenames:
if Sel in filename.upper():
print filename
fullpath = (dirpath + "/" + filename)
print fullpath
Fname = (dirpath[36:] + " " + filename)
result = arcpy.MakeRasterLayer_management(fullpath, Fname)
layer = result.getOutput(0)
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, layer, "BOTTOM")
else:
pass
I keep on receiving this error.
Traceback (most recent call last): File "C:\Users\sorrell\AppData\Local\ESRI\Desktop10.2\AssemblyCache{DE572FA2-3589-48E1-8D3E-6E2C0CB31129}\NewOMSIUA_addin.py", line 117, in onMouseDownMap arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, layer, "BOTTOM")
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 88, in AddLayerToGroup assert isinstance(target_group_layer, Layer) and target_group_layer._arc_object.isGroupLayer
AssertionError
It will add in the layer but will not add the layer into the group. Edit: I just sliced off the .tif at the end of the name just to make sure the .tif was not messing anthing up in the name. It did not work
targetGroupLayer
is currently a list object, not a layer object. It needs to be a group layer object.targetGroupLayer = arcpy.mapping.ListLayers(mxd, "15MinMaps", df)[0]
should work if the 15MinMaps wildcard only finds your group layer, and mxd and df are set correctly.