arcpy.mapping.MoveLayer not working

07-15-2012 06:05 PM
New Contributor II
Hi friends,

I am lucky to have found a solution to my previous problem, only through your kind help.

However, I have come across another one that leaves me just as dumbfounded.

Basically, I can't get arcpy.mapping.MoveLayer ( to work at all.

It seems like very simply syntax, yet it just refuses to move my layers.

At first, my own simple adaptation did not work,
arcpy.mapping.MoveLayer(df, "Contours 10m - MARANOA REGIONAL", "SPOT 2009 Image Date", "BEFORE")

so I tried based on their example

    for lyr in layerlist:
        if == "Contours 10m - MARANOA REGIONAL":
            moveLayer = lyr
        if == "SPOT 2009 Image Date":
            refLayer = lyr
        arcpy.mapping.MoveLayer(df, refLayer, moveLayer, "BEFORE")

and still the outcome is the same. The layer that is supposed to be moved just stays where it was introduced.

Has anyone come across this before or have any good tips for me?

5 Replies
Esri Esteemed Contributor
Try adding arcpy.RefreshTOC() and arcpy.RefreshActiveView to your script.  Ex:

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.mapping.MoveLayer(df, "Contours 10m - MARANOA REGIONAL", "SPOT 2009 Image Date", "BEFORE")


del mxd

Also, are you executing the python script within ArcMap?
0 Kudos
New Contributor II
Hi Jake,

Well I have those in my script towards the end already, but your suggestion was helpful in that it works better if I move that section of code after the refresh.

Unfortunately, I can't get it to move layers that I create in the script, it will only move layers already existing in the template. If I attempt to move a layer I created within the script beforehand, I get

Traceback (most recent call last):
  File "U:\Tools\Toolbox\InProgress\GeoPDF_Maker\", line 410, in <module>
    arcpy.mapping.MoveLayer(df, refLayer, moveLayer, "BEFORE")
NameError: name 'moveLayer' is not defined

No matter how I define 'moveLayer', either as direct quotes of the layer name or using the variable name == "Contours 10m - MARANOA REGIONAL"
or == outContours

I still get the same result....

Thanks for the suggestion, I will keep fiddling with this.
0 Kudos
New Contributor II
Hi Jake,

I come to you with news of great success.

You were definitley on to something...

My problem was that I was not re-defining my layer list after I made the layers I needed, and thus that's why it couldn't find the layer to move.

So I just re-defined the layer list again and it works!!!

Thanks friend!
0 Kudos
New Contributor
I've just run into a similar problem with MoveLayer (10.1 SP1) - getting the error message  'Did not find reference layer.' It may only occur after something new is added to the TOC. I'm not sure what gismarty meant by his solution of "re-defining the layer list"?

From the interactive window, I tested that the parameters point to layer objects, but MoveLayer still can't seem to find the reference layer.

>>> lastlyr
<map layer u'masterLayer'>
>>> lyr
<map layer u'WSCOND_S'>
>>> df
<DataFrame object at 0x28565dd0[0x2857cea8]>
>>> arcpy.mapping.MoveLayer(df, lastlyr, lyr, "AFTER")
Runtime error
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\arcgis\desktop10.1\arcpy\arcpy\", line 181, in fn_
    return fn(*args, **kw)
  File "c:\arcgis\desktop10.1\arcpy\arcpy\", line 1659, in MoveLayer
    assert refe_c is not None, 'Did not find reference layer.'
AssertionError: Did not find reference layer.

Thanks for any ideas - Sean
0 Kudos
New Contributor

Though I realise by this time you're probably well past this problem, in case it's helpful to anyone else I'm guessing re-defining the layer list is running arcpy.mapping.ListLayers() again - that's certainly what worked for me.

I was having this trouble myself - I was adding a new layer in my script and then wanted to reposition it, but it wasn't working. My new layer replaced an old layer of the same name. To save processing, I had defined the layer list once and was re-using it to pull out the layer object that I needed to move. So my script was returning a layer object to me, but it was the old version of the layer, which of course didn't exist any more, thus the error message. Using arcpy.mapping.ListLayers() again to pick up the new layer solved the problem. 

0 Kudos