rayjaw

Add Layer Button Problem

Discussion created by rayjaw on Aug 4, 2011
Latest reply on Dec 5, 2013 by tmlandrum
I am attempting to create a button to click on where an arcpy script runs and adds a layer to the current mxd.  When I run this script in the Python Window inside of ArcMap it works fine, and the layer displays properly.  When I add a script to a toolbox however, it does not work.  One strange thing which happens is that while the script runs (it only takes a second or two) the layer appears for a split second then disappears before the script completes (it claims to complete successfully).  Any help would be much appreciated.  Anyway here is the script:

    import arcpy
    arcpy.env.workspace = r'C:\Example\Location'
    mxd = arcpy.mapping.MapDocument("CURRENT")
    df = arcpy.mapping.ListDataFrames(mxd,'*')[0]
    addLayer = arcpy.mapping.Layer(r'C:\Example\Location\Counties.lyr')
    arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    del mxd, addLayer

Outcomes