Problem with GetParameterAsText

1782
4
12-15-2016 09:53 AM
BradJones2
New Contributor

I have the following code:

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = r"PATH\SDE_CONNECTION.sde"

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "")[0]

# Variables
subbasinCode = arcpy.GetParameterAsText(0)
pdfName = subbasinCode + ".pdf"
queryString = "AREA_CODE =" + "'" + subbasinCode + "'"
subbasins = "SDE.SEWERMAN.SUBBASIN"
subbasinSymbol = r"PATH\Subbasin_Selection.lyr"

arcpy.AddMessage(subbasinCode)
arcpy.AddMessage(queryString)
subbasinSelect = arcpy.MakeFeatureLayer_management(subbasins,
                                                   "Subbasin_Selection",
                                                   queryString)
arcpy.AddMessage(subbasinSelect)
# arcpy.GetMessages()
arcpy.ApplySymbologyFromLayer_management(subbasinSelect, subbasinSymbol)
arcpy.AddMessage("Symbology applied to subbasin selection.")
# arcpy.GetMessages()‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

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?

0 Kudos
4 Replies
JoshuaBixby
MVP Esteemed Contributor

Although I can't remember the specific conditions at the moment, sometimes AddLayer is needed when working with script tools.  Try adding the layer after you make the new feature layer.

DuncanHornby
MVP Notable Contributor

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.

BenNadler
Esri Contributor

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

arcpy.setParameterAsText(1, subbasinSelect)

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.

0 Kudos
BradJones2
New Contributor

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.  

Thanks again.

0 Kudos