[GIS] UpdateLayer with arcpy

arcgis-10.3arcpy

I am trying to use updatelayer in three mxd's. Each mxd has 3 data frames. I am getting an error, can anyone help?

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname 
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    updateLayer = arcpy.mapping.ListLayers(mxd, "ways", df)[0]
    sourceLayer = arcpy.mapping.Layer(r"C:\Project\layers\ways.lyr")
    dfList = arcpy.mapping.ListDataFrames(mxd, "*")
    for df in dfList:
        for lyr in arcpy.mapping.ListLayers(mxd, "", dfList):                                   
            if lyr.name == "ways": 
                arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)
                print 'UpdateLayer'     
    mxd.save()
del mxd

Best Answer

Your arcpy.mapping.UpdateLayer is referencing the same layer (updateLayer) in the same data frame for each iteration of your for loop. Thus it is most likely throwing an error because you are trying to update a layer from one map data frame but indicating it is located in a different data frame.

Replace with this:

arcpy.mapping.UpdateLayer(df, lyr, sourceLayer, True)
Related Question