1

The script below works fine and produces the correct output, but after my output is being added to ArcMap, which is what I want it to do, it then disappears as the script is finishing up. I'd like for my data to stay visible on the map once the script is finished and not disappear.

Here is what's happening:

  1. Buffer output "TestFC_buf" gets added to the map.
  2. "Script Finished." and "Completed script TestAddToMap..." prints to the script dialog window.
  3. "TestFC_buf" disappears from the map.
  4. "Succeeded at Thu Aug 06..." prints to the script dialog window.

Here is the script dialog:

enter image description here

And here is the code:

import arcpy, os

arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = True

arcpy.env.workspace = r"C:\Users\kgrady\Desktop\Temp\AddOutputToMap_Test\Test.gdb"
workspace = arcpy.env.workspace

fc = "TestFC"
output = os.path.join(workspace, "TestFC_buf") 
arcpy.Buffer_analysis(fc, output, "1 Mile")

arcpy.AddMessage("Script Finished.")

Any ideas as to why the output is disappearing from the map?

  • There is nothing in your script that looks like it would cause the symptoms that you describe. Those symptoms sound bizarre to me so if you have not already done so, I think you should consider a restart of ArcMap, and perhaps a reboot of your laptop/PC too. If it persists then I think you should use the code you have here to start adding bits back in until the problem appears in a code snippet suitable for posting as a focussed question. – PolyGeo Aug 5 '15 at 22:13
  • 1
    As a second thought you are opening a map document object on the currently open map, adding things to it and then saving the map document object. When the tool completes I suspect that your map is refreshing using its original state. If you have ArcMap open do you need to save the map document object, when you have the option to hit the Save button to do that? – PolyGeo Aug 5 '15 at 22:20
  • Good suggestions. I just tried them all. I removed the mxd/df variables and mxd.save() leaving only arcpy.env.addOutputsToMap = True (and False). It still adds the data I want to the map, but then after it prints out a message that the script is finished, both data sets still disappear from the map document, one by one. I'm going to write a super short script that does something similar to see if I can pinpoint why this is happening... will check back in after. – Kristen G. Aug 6 '15 at 18:35
  • I've completely updated the question to reflect a full script that is producing the same problem. – Kristen G. Aug 6 '15 at 19:32
0

You could set addOutputsToMap to False, and instead turn the buffer output into a Layer object and add it to the current MXD using the AddLayer function.

import arcpy, os

arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False

workspace = r"C:\Users\kgrady\Desktop\Temp\AddOutputToMap_Test\Test.gdb"
arcpy.env.workspace = workspace

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

fc = "TestFC"

output = os.path.join(workspace, "TestFC_buf")
arcpy.Buffer_analysis(fc, output, "1 Mile")

layer = arcpy.mapping.Layer(output)
arcpy.mapping.AddLayer(df, layer)

arcpy.AddMessage("Script Finished.")
  • I pasted a very simplified version of my code for the sake of asking the question about why the output disappears, but my actual code is trying to add an output text file to ArcMap, not a feature class. What you wrote may work for a feature class, but it doesn't work for adding a text file :( Any ideas on how to add a text file at the end of a script? I've tried numerous iterations of "AddLayer" and none of that seems to work for adding a text file. "arcpy.env.addOutputsToMap = True" works, but then my text file disappears. – Kristen G. Aug 7 '15 at 14:31
  • @KristenG. oh I remember your question from last time: gis.stackexchange.com/questions/154723/… that worked for me. Where did that script fail for you? – ianbroad Aug 7 '15 at 15:02
  • 1
    So sorry for not responding to your answer, I was away for a while, and when I came back I was trying another route (the addOutputsToMap = True route). I was running into an error using what you posted on my other post, but I think after a bajillion iterations it was because I had misspelled TableToTable as TabletoTable. So it's working now!! – Kristen G. Aug 7 '15 at 16:23
  • 1
    I'm marking your answer as correct because it does work, although for anyone interested in how to resolve the adding a text (csv) file to arcmap, see my other post here: gis.stackexchange.com/questions/154723/… – Kristen G. Aug 7 '15 at 16:24

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.