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:
And I want to have:
The code below only moves the last layer from the TopGroup:
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> os
MapMainFolder <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"Z:\Workspace"</SPAN> <SPAN class="comment token"># topmost folder</SPAN>
<SPAN class="keyword token">for</SPAN> <SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> dirs<SPAN class="punctuation token">,</SPAN> files<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">in</SPAN> os<SPAN class="punctuation token">.</SPAN>walk <SPAN class="punctuation token">(</SPAN>MapMainFolder<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> fileName <SPAN class="keyword token">in</SPAN> files<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>splitext <SPAN class="punctuation token">(</SPAN>fileName<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">".mxd"</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage <SPAN class="punctuation token">(</SPAN>fileName<SPAN class="punctuation token">)</SPAN>
fullPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join <SPAN class="punctuation token">(</SPAN>root<SPAN class="punctuation token">,</SPAN> fileName<SPAN class="punctuation token">)</SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument <SPAN class="punctuation token">(</SPAN>fullPath<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> fileName
df <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"Layers"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="comment token">#Layers to be moved !!! NEEDS TO LOOP through all layers !!!</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">!=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>longName<SPAN class="punctuation token">:</SPAN>
moveLayer <SPAN class="operator token">=</SPAN> lyr
<SPAN class="keyword token">print</SPAN> moveLayer
<SPAN class="comment token">#Topmost group layer, the content of this group layer should be moved above this layer and the group can remain empty </SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="operator token">==</SPAN> lyr<SPAN class="punctuation token">.</SPAN>longName<SPAN class="punctuation token">:</SPAN>
refLayer <SPAN class="operator token">=</SPAN> lyr
<SPAN class="keyword token">print</SPAN> refLayer
arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MoveLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> refLayer<SPAN class="punctuation token">,</SPAN> moveLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BEFORE"</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RefreshTOC<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>RefreshActiveView<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Anyone has any idea on:
- how to loop through the layer list?
- 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!