I am trying to move TOC layers with the following python code, seems simple enough. Basically i am trying to move TaxParcels1 layer above the Sections layer of my current mxd but i am not sure why i keep getting the error is it because my layers start with an upper case? Also do i have to save to a new mxd for the layer move to happen?
import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Split MXD.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers") for lyr in arcpy.mapping.ListLayers(mxd, "", df): if lyr.name.lower() == "Taxparcels1": moveLayer = lyr if lyr.name.lower() == "Sections": refLayer = lyr arcpy.mapping.MoveLayer(df, refLayer, moveLayer, "BEFORE") #mxd.saveACopy(r"C:\Temp\Split MXD2.mxd") del mxd
Runtime error Traceback (most recent call last): File "<string>", line 12, in <module> File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\mapping.py", line 1677, in MoveLayer assert move_c is not None, 'Did not find move layer.' AssertionError: Did not find move layer.
I would have to rename my TOC layer to taxparcels1 & sections. Which i done, i didn't want to do that but that got me passed the error but nothing happened.
So that brings me to the other question, do i have to use mxd.saveACopy in order for the taxparcels1 layer to be on top of sections?
I think you're thinking about this backwards:
if lyr.name.lower() == "Taxparcels1":
The layer you're inspecting is "lyr". It has a name, maybe "TaXpArCeLs1". The .lower() representation would be "taxparcels1". In any case, no lyr.name.lower() will ever equal "Taxparcels1" because it has an uppercase letter.
You should not have to rename your TOC layers. The "lyr.name.lower()" converts the layer name to lower case letters, and then it is compared to a specified lower case string.
If you want to save the changes you will need to use either:
# save the current mxd mxd.save() # or save a copy mxd.saveACopy(r"C:\Temp\Split MXD2.mxd")