0

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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 22, 2014 at 14:17
4
  • 1
    What is the error message? Commented Dec 22, 2014 at 14:35
  • 1
    What version of Arc are you using? Commented Dec 22, 2014 at 14:40
  • Hey YYC - in your 'for df in dfList:' loop, it appears you may be running the ListLayers function with an empty wildcard string, and additionally using the entire returned dfList (list of dataframes) as the dataframe argument in the ListLayers funciton. Maybe re-write this for loop as: for df in dFlist: for lyr in arcpy.mappping.ListLayers(mxd, data_frame=df) Commented Dec 22, 2014 at 16:29
  • i work with arcview 10.3. i get en error: TypeError: <type 'list'> Commented Dec 23, 2014 at 6:42

2 Answers 2

2

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)
answered Dec 22, 2014 at 23:26
1

finely i used this code:

import arcpy,os,sys,string
import arcpy.mapping
from arcpy import env
# UpdateLayers for all df
env.workspace = r"C:\Project"
sourceLayer = arcpy.mapping.Layer(r"C:\Project\layers\ways.lyr")
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]
dfList = arcpy.mapping.ListDataFrames(mxd, "*")
for df in dfList:
 for lyr in arcpy.mapping.ListLayers(mxd, "", df): 
 if lyr.name == "ways":
 arcpy.mapping.UpdateLayer(df, lyr, sourceLayer, True)
 print 'UpdateLayer' 
mxd.save()
del mxd 

thanks to Emil

answered Jan 11, 2015 at 7:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.