stephenknox

AddLayer() breaks my datasource

Discussion created by stephenknox on Nov 8, 2012
Latest reply on Nov 8, 2012 by mimiller
Using 10.0

Can anyone explain to me why the following simple bit of code fails to work correctly? It is adding a valid layer from a .lyr file, but I get a broken data source at the end. It's fine when I add the .lyr file manually or through within ArcMap python window using "CURRENT".

import time
import arcpy

fullpath = r'\\mxdPath'
mxd = arcpy.mapping.MapDocument(fullpath) 
for df in arcpy.mapping.ListDataFrames(mxd):
    if df.name == 'Overview' or df.name == 'Keyplan':
        temp = r'C:\TMP\xxx.lyr'
        lyrNew = arcpy.mapping.Layer(temp)
        arcpy.mapping.AddLayer(data_frame,lyrNew,"TOP")

time.sleep(15)            
mxd.save()
del mxd


I think it's something to do with the AddLayer function not completing before the map is saved (hence why I added the delay). However, this doesn't seem to work consistently. Does AddLayer not return anything. Since some of these operations can take a long time, it seems crazy that arcpy could try to save the map before this is completed. Or have I got completely the wrong end of the stick. I just can't think of any other reason.

Outcomes