I have the following code:

import arcpy.mapping as mapping

mxd = mapping.MapDocument("current")

print mxd.title

df = mapping.ListDataFrames(mxd)[0]
print df.name
layer = mapping.Layer(r"C:\path\clippedCountries.lyr")
print layer.name
mapping.AddLayer(df, layer,"TOP")

# Refresh things

And it works fine. However, if I try to use the absolute filepath to set the mapping variable (instead of "current") it doesn't work. Any ideas? I''m running the script through the ArcGIS 10.2 python window.

* update *

Interestingly if I use the full path name and add mxd.save() it works, so long as the mxd is not currently open in ArcGIS. Next time I open the mxd the layer is there.

If I try to do this with the mxd referenced in the filepath OPEN at the time I get

IOError: MapDocObject: Unable to save.  Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.

Which I believe is due to a lock.

If I switch it back to "current" the lock vanishes and it will save and show the layer added in real time.

  • Are you positive you're getting the path correct? If you have the mxd open and you aren't using "CURRENT", it may have some type of lock on the mxd and the layer can't be added. – ianbroad Apr 10 '14 at 5:21
  • @ian Thanks Ian, I've triple checked the path, saved the .mxd and triple checked that one. Plus the print statements still get everything right, the layer just isn't added. You might be right about the lock... I'll try running it through catalog instead and saving it. – Oliver Burdekin Apr 10 '14 at 5:29
  • Is there a specific error message being thrown? If so, can you edit it into your Question, please? – PolyGeo Apr 10 '14 at 6:56
  • @PolyGeo - no error message being thrown. And ian - with an incorrect filepath there is an error message (I checked just quadruple check the path) – Oliver Burdekin Apr 10 '14 at 15:25

arcpy.RefreshActiveView and arcpy.RefreshTOC are only used with the "current" map of ArcMap. Creating a MapDocument object by specifying a full path to an MXD does not cause it to be loaded into ArcMap, it just makes its properties and methods available to ArcPy.

| improve this answer | |
  • But if I'm referencing the full path to the mxd that is already open in ArcGIS through the python window shouldn't the layer be added regardless? – Oliver Burdekin Apr 10 '14 at 4:55
  • If you reference the full path to the mxd, you should add mxd.save() at the end of your script. ArcGIS adds a layer to your mxd on the disk, not to the open one (even though they're the same, basically). – GISGe Apr 10 '14 at 6:00
  • @OliverBurdekin No - think of it being like you are adding that layer to a separate "ArcMap" process which has (and needs) no Data or Layout View to draw on. – PolyGeo Apr 10 '14 at 6:54

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.