AnsweredAssumed Answered

Ungroup a Group Layer

Question asked by jan.rykr on Oct 22, 2017
Latest reply on Oct 23, 2017 by jan.rykr

Hi All,

 

I've been trying to ungroup a group layer in TOC using Arcpy with no luck. There is no such function in arcpy so the approach I am trying to do is moving layer by layer out of the group and place them all on top of TOC.

However I can't get the loop through layers working.

The mxd structure is:

  • Topmost Group
    • Layer1
    • Layer2
    • Layer3
    • ...

And I want to have: 

  • Layer1
  • Layer2
  • Layer3
  • ...

The code below only moves the last layer from the TopGroup:

import arcpy, os

MapMainFolder = r"Z:\Workspace"  # topmost folder

for (root, dirs, files) in os.walk (MapMainFolder):
  for fileName in files:
    if os.path.splitext (fileName)[1] == ".mxd":
      arcpy.AddMessage (fileName)
      fullPath = os.path.join (root, fileName)
      mxd = arcpy.mapping.MapDocument (fullPath)
      print fileName
      df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
     
#Layers to be moved !!! NEEDS TO LOOP through all layers !!!
      for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.name != lyr.longName:
                  moveLayer = lyr
                  print moveLayer
#Topmost group layer, the content of this group layer should be moved above this layer and the group can remain empty                 
      for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.name == lyr.longName:
                  refLayer = lyr
                  print refLayer      
     
       
      arcpy.mapping.MoveLayer(df, refLayer, moveLayer, "BEFORE")

      arcpy.RefreshTOC() 
      arcpy.RefreshActiveView()

     

      mxd.save()

 

 

Anyone has any idea on:

  1. how to loop through the layer list?
  2. how use a different approach? 

 

The reason I need to ungroup the layers is to avoid having all layers rasterized when exporting to an Illustrator file.

 

Thanks for help!

Outcomes