Just wanted to copy and paste all layers into new MXD file. Unfortunately order of layers in TOC is not retained. So I googled for script and found one - but MoveLayer failed with error message "assert refe_c is not None, 'Did not find reference layer.'". The post did not mention anything like that.
Solution in my case : for the reference layer a fresh reference is required.
old: refLayer=moveLayer
new: refLayer = arcpy.mapping.ListLayers(mxdT, moveLayer.name, dfT)[0]
ArcMap 10.3.1
Here is the script:
# -*- coding: utf-8 -*-
# source code initially taken from: https://community.esri.com/thread/28042
# assumption : no layer groups
# my use case : copy and paste all layers into new MXD, layer order is not retained - run script to restore layer order
import arcpy
mxd_source = r"C:\temp\source1.mxd"
mxd_target = r"C:\temp\target.mxd"
mxdT = arcpy.mapping.MapDocument(mxd_target)
# assumption: only one DF
dfT = arcpy.mapping.ListDataFrames(mxdT, "")[0]
mxdS = arcpy.mapping.MapDocument(mxd_source)
# assumption: only one DF
dfS = arcpy.mapping.ListDataFrames(mxdS, "")[0]
#initialize refLayer with layer
refLayer = arcpy.mapping.ListLayers(mxdT,"",dfT)[0]
for layerSource in arcpy.mapping.ListLayers(mxdS,"",dfS):
c=layerSource.longName
d=layerSource.description
sourceLayer=c+d
print("processing layer in source: '" + sourceLayer+"'.")
for layerTarget in arcpy.mapping.ListLayers(mxdT,"",dfT):
a=layerTarget.longName
b=layerTarget.description
moveLayer= a+b
if sourceLayer==moveLayer:
print("moving layer:'"+moveLayer+"', reference layer: '"+refLayer.name+"'.")
moveLayer=layerTarget
arcpy.mapping.MoveLayer(dfT,refLayer,moveLayer, "After")
#get fresh layer reference - otherwise MoveLayer will fail next time:
refLayer = arcpy.mapping.ListLayers(mxdT, moveLayer.name, dfT)[0]
break
mxdT.save()
#arcpy.RefreshActiveView()
#arcpy.RefreshTOC()