0

This question has been asked multiple times with proper answers:

but I still cannot get it right.

I am using ArcGIS 10.2.2.

I want to add via a Python standalone script a Featureclass from a file-gdb to the TOC. If I try it in the ArcGIS Python window it works fine. However in a standalone script nothing happens. There is no error message or anything. I already thought it might be a bug, but it seems to work for everyone else. I also tried it with a .lyr and it didn´t work either. Someone got a hint for me?

Here is the code (I also tried the one from the ArcGIS help site, and from all the links above).

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

I also tried :

addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Potenziale_singlepart_final.lyr")

What am I doing wrong? I am trying for a week now. I`m getting crazy with this one. Seems so easy.

Sebastian
  • 85
  • 10
  • 3
    You need to save the mxd afterward to make the changes permanent. Use mxd.save() or mxd.saveACopy (file_name, {version}) ... see http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000000n000000 – Michael Stimson Sep 09 '15 at 23:40
  • @MichaelMiles-Stimson by "nothing happens" I think Sebastian is saying that the layer isn't added to the map at all (not that the change isn't permanent)? – Stephen Lead Sep 10 '15 at 00:03
  • yes thats right – Sebastian Sep 10 '15 at 00:07
  • 2
    As you're creating a mapdocument object from a static (on disc) map and not "Current" the changes are not permanent, when you open the map document it appears that nothing has happened because the changes are discarded as soon as the mapdocument goes out of scope - at least that's my understanding of how that object works. Try saving it and see if the layer is added. The code shown is sound, it should work, so unless there's some code that isn't shown that is problematic the only logical conclusion is that the changes are made but not committed. – Michael Stimson Sep 10 '15 at 00:15
  • Michael this isnt the probem. However you gave me the solution. It seems there is a problem related to the mxd path. I am completely sure I`m using the right path, but I get an error when Im trying to use the save() routine. If I use "CURRENT" instead of the path, it works. – Sebastian Sep 10 '15 at 00:27
  • 1
    You cannot use "CURRENT" in a standalone script, only when you have a map open in ArcMap. If that works for you then I think you are doing something slightly different to what you have presented in your code snippet and question description. – PolyGeo Sep 10 '15 at 01:15
  • I let it run in a toolbox just to try it out, thats why I could use CURRENT – Sebastian Sep 10 '15 at 01:19
  • My guess is that you had a map open when you ran that tool i.e. it was within the scope of ArcMap and not standalone when you ran it. – PolyGeo Sep 10 '15 at 01:26
  • yes ok, from this side thats true. But thats just something I tried now. Before it was standalone. However it still doesnt run, when I am giving the full mxd-path instead. Any ideas? – Sebastian Sep 10 '15 at 01:33
  • I think we need you to be more precise about what you are trying to do. It is still unclear to me whether you are writing a Python script tool (to run from a toolbox) or a Python script to run standalone from an IDE like IDLE. Please use the [edit] button beneath your question to revise it rather than creating a comment trail: http://meta.gis.stackexchange.com/a/3353 – PolyGeo Sep 10 '15 at 02:06

1 Answers1

2

On the code snippet and symptoms presented in the question:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")

it seems to me, like @MichaelMilesStimson has commented, that you are missing the line of code at the end which is necessary to persist any changes made to the MXD you open as a Map Document object:

mxd.save()

or, if you prefer:

mxd.saveACopy (file_name, {version}) 
PolyGeo
  • 65,136
  • 29
  • 109
  • 338