This is related to my question ArcPy replaceDataSource, but can anyone explain to me why the following code fails to add a valid layer from a .lyr file, and I get the same problems as in that question (broken data source):

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)

del mxd

I added in the delay because I wondered whether the layer wasn't fully added before the MXD was saved, but this doesn't seem to work consistently.

My geodatabases do seem to take a long time to open, but it's crazy not to be able to undertake such a simple scripting operation. I guess the same problem is affecting both operations, but any ideas why?

  • I don't think the delay is needed. As a test have you tried changing the UNC path to a drive mapped locally to see if that enables your code to work? – PolyGeo Nov 9 '12 at 3:23
  • thanks @PolyGeo. It seems to be an issue with relative paths, as per answer – Stev_k Nov 9 '12 at 9:20

This seemed to be an issue with relative paths. The mxd that I was trying to update was stored as Relative Paths in Document Properties. Once this was unchecked, the script worked as expected (without the delay).

So you can check each mxd manually, but given that this is a script, that slightly defeats the purpose. Unfortunately though ArcPy doesn't expose an API to change this particular setting (at least in 10.0).

So I have created a python script that uses ArcObjects to do it, with a lot of help from other people on the forum and outside. It needs an external library installed, but it is conceptually very simple. It can be found at https://gis.stackexchange.com/a/40666/217

| improve this answer | |
  • It would be good if this information were added to the excellent Paths Explained document and if ArcPy could be used to unset Relative Paths. – PolyGeo Nov 9 '12 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.