Select to view content in your preferred language

Explain arcpy.SetParameterAsText to me like a 5th Grader.....

2962
2
06-18-2018 11:28 AM
ThomasColson
MVP Frequent Contributor

So this is something new to me that is manifesting itself in Pro and that is Setting script tool parameters—Geoprocessing and Python | ArcGIS Desktop. In particular, "The tool modifies the schema of the input, ", which, an ESRI employee told me derives from "Your script tools need to use derived outputs to indicate to the multithreaded geoprocessing framework that something was updated within your script code". Since this stuff works in Arc, but I'm seeing the issue in Pro, I'm guessing that's where multithreaded comes into play. 

So here's the problem. With the attached toolbox, run Create Folders -> Create GDB -> Add attributes (check both boxes). What happens is, the folders get created, the GDB get's created, the extended attributes get created, the extended domains get created, but the domains created with the last tool don't appear in right-click GDB -> Domains, not do the domains get assigned to the attributes (because the tool doesn't believe they exist). Close Pro. Go to Arc Cat, the extra two domains created by the 3rd tool (and the assignment to the attributes) are there and correct. Restart Pro. There they are. 

I completely understand that some sort of tool parameter wizardry has to occur here for the 3rd tool to recognize the domains have been added, and let the tool know that so it can continue with the next steps which is assign the domains to the attributes. 

The attached toolbox was edited by the helpful ESRI person in another forum, but I'm not having that "ahhhhh that makes sense" moment when trying to extend it, or, even make it work (for some reason it worked once, but post-many-reboots, the original behavior is occurring, the domains aren't showing up (even though they're really there). 

0 Kudos
2 Replies
KimberlySaballett
Esri Contributor

The arcpy.SetParameterAsText method takes two arguments, a number and a string.
- The number specifies the derived parameter position in the Script Tool parameter list (zero based index). 
- The second argument is a string representing the name of the output (i.e. file location).

Use this method display derived outputs and to set the specified parameter by index using a string value.

The python file for a script tool might look like either of these examples when buffering points and using SetParameterAsText to define the output.


Derived output as the only parameter example:


import arcpy

arcpy.env.overwriteOutput = True
fileToBuffer = "C:/temp/points.shp"
distance = "2 miles"
outputFile = "C:/temp/pointsBuffer.shp"

arcpy.Buffer_analysis(fileToBuffer, outputFile, distance)
arcpy.SetParameterAsText(0, outputFile)

Multiple parameters example with user defined input:


import arcpy, os

arcpy.env.overWriteOutput = True
fileToBuffer = arcpy.GetParameterAsText(0)
distance = arcpy.GetParameterAsText(1)
arcpy.env.workspace = os.path.dirname(fileToBuffer)
outputFile = "C:/temp/pBuffer.shp"

arcpy.Buffer_analysis(fileToBuffer, outputFile, distance)
arcpy.SetParameterAsText(2, outputFile)

For both of these examples you would need to navigate to the Parameters tab in the Tool Properties. The index for the SetParameterAsText method would be the index used for the output parameter in the list. It would also need to be of Derived type which automatically set the Direction as Output. This can be useful when running a model where one tool uses for input the derived output of another tool.

ThomasColson
MVP Frequent Contributor

I get that parameters need to be set in Tool Properties. What is not clear, is, why, in Pro, is a derived parameter needed for something that alters the schema, when this was not needed in Arc Map.