Select to view content in your preferred language

Add layer inside group layer that is inside a group layer

2884
1
Jump to solution
08-07-2015 04:03 AM
AntonioCalatayud
New Contributor II

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!!!

0 Kudos
1 Solution

Accepted Solutions
AntonioCalatayud
New Contributor II

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")

View solution in original post

1 Reply
AntonioCalatayud
New Contributor II

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")