Hello,
I am working on a model and one of the steps executes a script (script is called AddLayerSitePolygon(2)). The purpose of the script is to:
- add a layer named "site_polygon"
- rename the layer to "PROPOSED - site_polygon"
- add a new layer named "site_polygon"
The end result being that 2 new layers are added to the TOC ("site_polygon" and "PROPOSED - site_polygon").
When I watch the model execute I can see that the script works as I see the 2 layers get added to the TOC, but then right after they are removed from the TOC.
Afterwards if I go ahead and run the script on it's own (outside of the model) it works as expected, the layers are added to the TOC and they stay there.
I cannot figure out why it is that when the script is run within the model that the layers get removed from the TOC?
Here is what the script looks like:
import arcpy
##Reference the Current map document.
#Add site_polygon
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
NewLayer = arcpy.mapping.Layer("site_polygon.lyr")
arcpy.mapping.AddLayer(df, NewLayer, "AUTO_ARRANGE")
AddLayerSitePolygonResult = True
mxd.save()
#Rename site_polygon to "PROPOSED - site_polygon"
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name in ("site_polygon"):
lyr.name = "PROPOSED - site_polygon"
mxd.save()
#Add site_polygon
df2 = arcpy.mapping.ListDataFrames(mxd)[0]
NewLayer2 = arcpy.mapping.Layer("site_polygon.lyr")
arcpy.mapping.AddLayer(df2, NewLayer2, "AUTO_ARRANGE")
AddLayerRenameSitePolygonResult = True
arcpy.SetParameter(0, AddLayerRenameSitePolygonResult)
mxd.save()
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
And here is a picture of where the script is in the model:
Thanks for any help!