Hello,
It might be a simple question/solution, but I can't find the way to make my arcpy code works....
I have a layer that I need to add inside a group, that is inside a group already:
-Group1
-SubGroup1
-NEW LAYER
-SubGroup1.2
-Group 2.
My code is more and less like this:
for layer in layers:
if layer.longName == Group1+ "\\" + SubGroup1:
print("------ layer goes here-------")
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group1\SubGroup1", df)[0]
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, spatialLayer, "AUTO_ARRANGE")
else:
print ("not here")
Where I wrote "Group1\SubGroup1" I tried: "Group1\\SubGroup1"; "Group1/SubGroup1", but I'm always getting the same error:
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Group1\SubGroup1", df)[0]
IndexError: list index out of range
Any ideas how to get the subgroup inside the group??
Many thanks!!!
Solved! Go to Solution.
Sorry for wasting your time.... I just found a solution... (How to reference a specific layer in a dataframe with Arcpy - Geographic Information Systems Stack E...
Basically I just have to change "Group1\SubGroup1" to layer...
Final code would be:
if layer.longName == Group1+ "\\" + SubGroup1:
print("------ layer goes here-------")
targetGroupLayer = arcpy.mapping.ListLayers(mxd, layer, df)[0]
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, spatialLayer, "AUTO_ARRANGE")
else:
print ("not here")
Sorry for wasting your time.... I just found a solution... (How to reference a specific layer in a dataframe with Arcpy - Geographic Information Systems Stack E...
Basically I just have to change "Group1\SubGroup1" to layer...
Final code would be:
if layer.longName == Group1+ "\\" + SubGroup1:
print("------ layer goes here-------")
targetGroupLayer = arcpy.mapping.ListLayers(mxd, layer, df)[0]
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, spatialLayer, "AUTO_ARRANGE")
else:
print ("not here")