How? bring custom python-script tool's output back to modelBuilder ArcGIS Pro

490
2
Jump to solution
04-27-2020 05:45 AM
LessieOC
New Contributor III

I created a python script tool. The user runs the script after setting the inputs. I would like to bring the generated ouput in the code variable outFeatures (polygons) back into the model as the script's output.

      . . .

      outFeatures = os.path.join(shp_output_folder,"cont_hs.shp")

   . . .

I get a blank output in the model as shown in pic, no matter what. Nothing I tried works:

(1) setting the script's outFeatures output parameter Data Type as "Shapefile" or "feature class" or "file" or "string" and as a "Derived" Type, yield a blank output.

(2) Within the python code, I tried adding the result object and using the result[0] object as an alternate output variable even though the string is the same as in outFeatures, anyway

      . . .

      result = arcpy.DeleteField_management ( outFeatures,["ht_v_OBJEC", "ht_v_index"] )

      output = result[0]

...using ArcGIS Pro 2.5, Any advise ?

0 Kudos
1 Solution

Accepted Solutions
MarcoBoeringa
MVP Regular Contributor

It is not enough to create an output parameter in the ModelBuilder interface for your script. This just tells ModelBuilder the script should generate output, and how many parameters and what type, but you still need to explicitly set that output using arcpy.SetParameter() or arcpy.SetParameterAsText(), see the example below. Note that script parameters are called by index number, starting at 0 and upwards. So in your case, I see "outFeatures" as the third parameter, which has index number 2. So you need to set index number 2.

By the way, the script below can both act as a ModelBuilder tool, and be called as function of an imported Python module, by importing the script in another script module, and then calling <YOUR_SCRIPT_NAME>.main().

import arcpy

def main(inputVariable):

   # Do something

   return <YOUR_SCRIPT_RESULT>

if __name__ == '__main__':

  

   # Get *input* for script

   inputVariable = arcpy.GetParameter(0)

   # Call main function

   returnValue = main(inputVariable)

  

   # Set *output* for script, NOTICE the parameter index number of the parameter before the variable to return.

   arcpy.SetParameter(1,returnValue)

View solution in original post

2 Replies
MarcoBoeringa
MVP Regular Contributor

It is not enough to create an output parameter in the ModelBuilder interface for your script. This just tells ModelBuilder the script should generate output, and how many parameters and what type, but you still need to explicitly set that output using arcpy.SetParameter() or arcpy.SetParameterAsText(), see the example below. Note that script parameters are called by index number, starting at 0 and upwards. So in your case, I see "outFeatures" as the third parameter, which has index number 2. So you need to set index number 2.

By the way, the script below can both act as a ModelBuilder tool, and be called as function of an imported Python module, by importing the script in another script module, and then calling <YOUR_SCRIPT_NAME>.main().

import arcpy

def main(inputVariable):

   # Do something

   return <YOUR_SCRIPT_RESULT>

if __name__ == '__main__':

  

   # Get *input* for script

   inputVariable = arcpy.GetParameter(0)

   # Call main function

   returnValue = main(inputVariable)

  

   # Set *output* for script, NOTICE the parameter index number of the parameter before the variable to return.

   arcpy.SetParameter(1,returnValue)

LessieOC
New Contributor III

Oh, of course!  I already get the input with GetParameterAsText(#) functions! 

Just add this line: arcpy.SetParameter(2, outFeatures). 

0 Kudos