Issue with script in model

1012
7
08-02-2016 09:07 AM
ChrisHolmes
Occasional Contributor III

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:

  1. add a layer named "site_polygon"
  2. rename the layer to "PROPOSED - site_polygon"
  3. 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!

Tags (2)
0 Kudos
7 Replies
DanPatterson_Retired
MVP Esteemed Contributor
ChrisHolmes
Occasional Contributor III

Thanks Dan, I'll check.

0 Kudos
ChrisHolmes
Occasional Contributor III

No, they are not.

0 Kudos
ChrisDonohue__GISP
MVP Frequent Contributor

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

ChrisHolmes
Occasional Contributor III

Good point. Thanks Chris. I had previously read that a while ago.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor
0 Kudos
ChrisHolmes
Occasional Contributor III

Thanks Dan. I’ll give this a try.

0 Kudos