Recently I saw a script that moves layers according to their names in TOC. I wanted to make a complete script for my work in which all needed layers are listed and probably should be sorted in a way I want.
Here it is:
import arcpy
mxd=arcpy.mapping.MapDocument("CURRENT")
df= arcpy.mapping.ListDataFrames(mxd,"")[0]
from arcpy import mapping
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "ramka":
moveLayer = lyr
if lyr.name == "address":
refLayer = lyr
mapping.MoveLayer(df,refLayer,moveLayer,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "address":
moveLayer1 = lyr
if lyr.name == "river":
refLayer1 = lyr
mapping.MoveLayer(df,refLayer1,moveLayer1,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "river":
moveLayer2 = lyr
if lyr.name == "allhouse":
refLayer2 = lyr
mapping.MoveLayer(df,refLayer2,moveLayer2,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "allhouse":
moveLayer3 = lyr
if lyr.name == "topo":
refLayer3 = lyr
mapping.MoveLayer(df,refLayer3,moveLayer3,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "topo":
moveLayer4 = lyr
if lyr.name == "village":
refLayer4 = lyr
mapping.MoveLayer(df,refLayer4,moveLayer4,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "village":
moveLayer5 = lyr
if lyr.name == "quarter":
refLayer5 = lyr
mapping.MoveLayer(df,refLayer5,moveLayer5,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "quarter":
moveLayer6 = lyr
if lyr.name == "town":
refLayer6 = lyr
mapping.MoveLayer(df,refLayer6,moveLayer6,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "town":
moveLayer7 = lyr
if lyr.name == "district_t":
refLayer7 = lyr
mapping.MoveLayer(df,refLayer7,moveLayer7,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "district_t":
moveLayer8 = lyr
if lyr.name == "district_a1":
refLayer8 = lyr
mapping.MoveLayer(df,refLayer8,moveLayer8,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "district_a1":
moveLayer9 = lyr
if lyr.name == "district_a":
refLayer9 = lyr
mapping.MoveLayer(df,refLayer9,moveLayer9,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "district_a":
moveLayer10 = lyr
if lyr.name == "admin2":
refLayer10 = lyr
mapping.MoveLayer(df,refLayer10,moveLayer10,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "admin2":
moveLayer11 = lyr
if lyr.name == "admin1":
refLayer11 = lyr
mapping.MoveLayer(df,refLayer11,moveLayer11,"BEFORE")
for lyr in mapping.ListLayers(mxd,"",df):
if lyr.name == "admin1":
moveLayer12 = lyr
if lyr.name == "admin":
refLayer12 = lyr
mapping.MoveLayer(df,refLayer12,moveLayer12,"BEFORE")
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
Unfortunately, testing of this script in other databases throws an error like
Runtime error
Traceback (most recent call last):
File "<string>", line 39, in <module>
NameError: name 'refLayer4' is not defined
It was because of one of the layers did not exist in TOC. So what would you recommend if it is needed to have an order in TOC like in script despite the absence of some layers. Or is there any other script sample that sorts layers?