MCederholm

Python Toolbox:  How to set an output parameter

Discussion created by MCederholm on Aug 21, 2012
Latest reply on Jun 15, 2015 by adamkerz
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[n] = 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!

Outcomes