I have the following code:
from arcpy import env
env.overwriteOutput = True
env.workspace = r"PATH\SDE_CONNECTION.sde"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")
subbasinCode = arcpy.GetParameterAsText(0)
pdfName = subbasinCode + ".pdf"
queryString = "AREA_CODE =" + "'" + subbasinCode + "'"
subbasins = "SDE.SEWERMAN.SUBBASIN"
subbasinSymbol = r"PATH\Subbasin_Selection.lyr"
subbasinSelect = arcpy.MakeFeatureLayer_management(subbasins,
arcpy.AddMessage("Symbology applied to subbasin selection.")
When I run this code in the python console in ArcMap (hardcode the subbasinCode variable) it runs fine. It replaces the existing Subbasin_Selection layer with the new selection from the MakeFeatureLayer function then applies the symbology.
When I create the script tool and run it the original Subbasin_Selection layer is removed from the map but the new subbasin isn't added.
The original code was much longer and it would use data driven pages to create a map book and export it based on the subbasin selection. It ran fine a a standalone script. I simplified it to figure out what is going on.
Any ideas on why it won't work in a script tool?
GeoProcessing tools return GeoProcessing result objects which you then query to see if the tool ran OK. So in your code subbasinSelect is actually a result object which you have fed into the ApplySymbologyFromLayer_management tool as a parameter. What you want to put in that tool is the text "Subbasin_Selection" which is the layer name.
I think you need to add an output to the tool. In your case the output is a layer file
Add the following to the end of the script
Then in the tool properties, add another variable of type "Derived" and of type "Feature Layer"
The script tool will return the layer, which will be added to the display.
Thanks for the replies. Sorry it took so long for me to back to the thread. I was able to get this to work with the following:
subbasinLayer = arcpy.mapping.Layer(subbasinSelection)
arcpy.mapping.AddLayer(df, subbasinLayer, "AUTO_ARRANGE")
I spent the good part of 2015 making a whole bunch of custom script tools, some quite complicated. Since then everything I've done with Python has been standalone scripts than run in task scheduler. For some reason I totally forgot all about this.