Python Toolbox:  How to set an output parameter

15241
7
08-21-2012 09:03 AM
MarkCederholm
Occasional Contributor III
How do you set an output parameter (e.g. a feature set) in the execute method of a tool in a Python toolbox?  I've tried setting parameters = output and arcpy.SetParameter(n, output) and neither work.  For example, the following script works fine in a regular toolbox:

import arcpy
try:
    arcpy.env.workspace = "in_memory"
    # Get parameters
    Input_Features = arcpy.GetParameterAsText(0)
    Buffers = arcpy.CreateUniqueName("Buffer")
    # Perform buffer
    arcpy.Buffer_analysis(Input_Features, Buffers,
        "100 Feet", "FULL", "ROUND", "NONE", "")
    OutFeatureSet = arcpy.FeatureSet()
    OutFeatureSet.load(Buffers)
    arcpy.SetParameter(1, OutFeatureSet)
except Exception, ErrorDesc:
    sErr = "ERROR:\n" + str(ErrorDesc)
    arcpy.AddError(sErr)


But I can't get the output to work in a Python toolbox:

import arcpy
import os

class Toolbox(object):
    def __init__(self):
        self.label = "Runtime Tools"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [BufferTest]

class BufferTest(object):
    def __init__(self):
        self.label = "Buffer Test"
        self.description = "Create buffers"
        self.canRunInBackground = False

    def getParameterInfo(self):
        params = []
        param0 = arcpy.Parameter(
            displayName = "Input Features",
            name = "inputFeatures",
            datatype = "Feature Set",
            parameterType = "Required",
            direction = "Input")
        param0.value = os.path.join(os.path.dirname(__file__),
            "FC_Templates.gdb\PointTemplate")
        params.append(param0)
        param1 = arcpy.Parameter(
            displayName = "Buffers",
            name = "buffers",
            datatype = "Feature Set",
            parameterType = "Derived",
            direction = "Output")
        params.append(param1)
        return params

    def execute(self, parameters, messages):
        try:
            arcpy.env.workspace = "in_memory"
            # Get parameters
            Input_Features = parameters[0].valueAsText
            Buffers = arcpy.CreateUniqueName("Buffer")
            # Perform buffer
            arcpy.Buffer_analysis(Input_Features, Buffers,
                "100 Feet", "FULL", "ROUND", "NONE", "")
            OutFeatureSet = arcpy.FeatureSet()
            OutFeatureSet.load(Buffers)
            # parameters[1] = OutFeatureSet
            arcpy.SetParameter(1, OutFeatureSet)
        except Exception, ErrorDesc:
            sErr = "ERROR:\n" + str(ErrorDesc)
            messages.addErrorMessage(sErr)
        return



What am I doing wrong?  Thanks!
Tags (2)
7 Replies
JustinShepard
Occasional Contributor II
Not sure if this would work but a few ideas:
1. try setting your out variable to a parameter (ex. outFeatureClass = parameters[1].valueAsText)
http://resources.arcgis.com/en/help/main/10.1/index.html#/Accessing_parameters_within_a_Python_toolb...

2. This may have nothing to do with it but I don't see where you're setting the parameterDependencies (not sure if it is required for all python toolboxes)
param2.parameterDependencies = [param0.name]
http://resources.arcgis.com/en/help/main/10.1/index.html#/Defining_parameters_in_a_Python_toolbox/00...
0 Kudos
MarkCederholm
Occasional Contributor III
This works fine if I define param1 as "Feature Class":

    def execute(self, parameters, messages):
        try:
            arcpy.env.workspace = "in_memory"
            # Get parameters
            Input_Features = parameters[0].valueAsText
            Buffers = arcpy.CreateUniqueName("Buffer")
            # Perform buffer
            arcpy.Buffer_analysis(Input_Features, Buffers,
                "100 Feet", "FULL", "ROUND", "NONE", "")
            # OutFeatureSet = arcpy.FeatureSet()
            # OutFeatureSet.load(Buffers)
            parameters[1].value = Buffers
        except Exception, ErrorDesc:
            sErr = "ERROR:\n" + str(ErrorDesc)
            messages.addErrorMessage(sErr)
        return


Perhaps, unlike a regular toolbox, a Python toolbox cannot understand "Feature Set" as an output type.
0 Kudos
KevinBell
Occasional Contributor III
Great sample!  I tried adding a distance parameter but it doesn't show up in the dialog when run...  I hope to get my head around these pyt's, but the simple things seem to stump me.

import arcpy
import os

class Toolbox(object):
    def __init__(self):
        self.label = "Runtime Tools"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [BufferTest]

class BufferTest(object):
    def __init__(self):
        self.label = "Buffer Test"
        self.description = "Create buffers"
        self.canRunInBackground = False

    def getParameterInfo(self):
        params = []
        param0 = arcpy.Parameter(
            displayName = "Input Features",
            name = "inputFeatures",
            datatype = "Feature Set",
            parameterType = "Required",
            direction = "Input")
        param0.value = r'E:\gis\default.gdb\point'
        params.append(param0)
        
        param1 = arcpy.Parameter(
            displayName = "Buffer Distance",
            name = "distance",
            datatype = "Long",
            parameterType = "Required",
            direction = "Input")
        params.append(param1)
        
        param2 = arcpy.Parameter(
            displayName = "Buffers",
            name = "buffers",
            datatype = "Feature Set",
            parameterType = "Derived",
            direction = "Output")
        params.append(param2)        
        
        return params

    def execute(self, parameters, messages):
        try:
            arcpy.env.workspace = r'E:\gis\default.gdb' 
            Input_Features = parameters[0].valueAsText
            Buffers = arcpy.CreateUniqueName("Buffer")
            Distance = parameters[1].valueAsText
            arcpy.Buffer_analysis(Input_Features, Buffers,
                Distance, "FULL", "ROUND", "NONE", "")

            parameters[2].value = Buffers
            
        except Exception, ErrorDesc:
            sErr = "ERROR:\n" + str(ErrorDesc)
            messages.addErrorMessage(sErr)
        return
0 Kudos
KevinBell
Occasional Contributor III
It turns out that refreshing the toolbox wasn't working, and closing the mxd and reopening fixed it.  ugh.
0 Kudos
AJR
by
Occasional Contributor II
Kevin,
   Thanks for posting this.  I had been getting the same errors when attempting to set the return value in my python toolbox and had been banging my head against the wall until I saw your post.  You are 100% correct, just refreshing the script doesn't work, exiting and restarting the app (ArcCatalog in my case) that the script was built in/called from was required for the extra return parameter to be accessible.  This certainly seems to be a bug, I'm wonder if it's been logged with ESRI?
MarkCederholm
Occasional Contributor III
Even at 10.2, Python toolboxes apparently only accept feature sets (GPFeatureRecordSetLayer) as an input parameter; I can't get the data type to work as an output parameter.  I still have to attach a script to a TBX file if I want to create a geoprocessing service or package that outputs a feature set that I build using arcpy.FeatureSet().  Perhaps I should add this issue to the ideas site?
AdamKerz
New Contributor

Mark, I'm not sure, but I think I was having the same problem as you. Defining the output parameter datatype as GPFeatureRecordSetLayer and using SetParameterAsText (I fully tested that function, but with minimal testing, looks like SetParameter also works for me). I'm using 10.2.1.

A basic test case is here:

arcpy - Returning feature class from Python toolbox - Geographic Information Systems Stack Exchange

Hope that stops others from giving up as I almost had.

0 Kudos