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:
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!
are results temporary by default?
Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop
picture??
Thanks Dan, I'll check.
No, they are not.
I don't have an answer for why this is not adding the layers to the Table of Contents as expected, but wanted to mention something that may trip things up in other regards. The filename chosen for the rename has a dash in it and spaces, both of which can trip up ESRI GIS processes. I'd avoid using the dash altogether and use underscores for any spaces.
lyr.name = "PROPOSED - site_polygon"
Chris Donohue, GISP
Good point. Thanks Chris. I had previously read that a while ago.
You may have to persist the layer by using Copy Features—Help | ArcGIS for Desktop or Save To Layer File—Help | ArcGIS for Desktop
Thanks Dan. I’ll give this a try.