wboras

Python toolbox output parameter

Discussion created by wboras on Apr 22, 2014
Latest reply on Dec 1, 2014 by wboras
Hello,

I'm using ArcGIS 10.1 and having trouble trying to set and get an output parameter in the execute() method of a Python toolbox. To illustrate, I've created a simple Python Toolbox which takes two input parameters (a and b) and adds them together to get a single output parameter (total). The code is below.

Using arcpy.SetParameterAsText(2, total) to set the output parameter works fine. Logically, after setting the value, I should be able to fetch it using arcpy.GetParameterAsText(2). However, this call always returns None. I instead have to use parameters[2].valueAsText to return the value. There is an inconsistency in the usage of SetParameterAsText and GetParameterAsText.

What is the "proper" way to set and get an output parameter in a Python toolbox? The link below explains to use valueAsText to get the values but says nothing about setting parameters.

http://resources.arcgis.com/en/help/main/10.1/index.html#//001500000037000000

Thanks,
Wayne


import arcpy


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Toolbox"
        self.alias = ""

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


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        a = arcpy.Parameter(
            displayName = 'a',
            name = 'a',
            datatype = 'GPLong',
            parameterType = 'Required',
            direction = 'Input'            
        )

        b = arcpy.Parameter(
            displayName = 'b',
            name = 'b',
            datatype = 'GPLong',
            parameterType = 'Required',
            direction = 'Input'            
        )        

        total = arcpy.Parameter(
            displayName = 'Total',
            name = 'total',
            datatype = 'GPLong',
            parameterType = 'Derived',
            direction = 'Output'            
        )        

        # Set defaults
        a.value = 100
        b.value = 200
        
        params = [a, b, total]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        # Print all parameters before any changes are made
        ndx = 0
        for p in parameters:
            arcpy.AddMessage('parameters[%d].valueAsText: %s'% (ndx, p.valueAsText))
            arcpy.AddMessage('arcpy.GetParameterAsText(%d): %s' % (ndx, arcpy.GetParameterAsText(ndx)))
            ndx += 1

        arcpy.AddMessage('=' * 50)
        
        # Set the output parameter
        total = int(parameters[0].valueAsText) + int(parameters[1].valueAsText)
        arcpy.SetParameterAsText(2, total)
        
        # Print all parameters again
        ndx = 0
        for p in parameters:
            arcpy.AddMessage('parameters[%d].valueAsText: %s'% (ndx, p.valueAsText))
            arcpy.AddMessage('arcpy.GetParameterAsText(%d): %s' % (ndx, arcpy.GetParameterAsText(ndx)))
            ndx += 1
            
        return    

Outcomes