error setting parameter as text

4707
6
09-01-2011 01:55 PM
LoriEmerson_McCormack
Occasional Contributor
I've created a simple python script that creates selection criteria based on the current year.  All works fine until I try to pass the parameters as text.

<type 'exceptions.RuntimeError'>: Object: Error in setting parameter as text

For the script parameters in my toolbox, I set up four parameter properties as Type = Derived, Direction = Output, Data Type = String.

What am I doing wrong?


Code:
import arcpy, datetime

currYear = datetime.datetime.today().strftime("%Y")
arcpy.AddMessage("Current Year = " + str(currYear))


# Set Local variables
num3YearsAgo = int(currYear) - 3
num4YearsAgo = int(currYear) - 4
num5YearsAgo = int(currYear) - 5
num6YearsAgo = int(currYear) - 6
num12YearsAgo = int(currYear) - 12

# Set up Selection Criteria to be passed to next step
strCat1 = '"Surf_Hist_Pavement_Year" >= ' + str(num3YearsAgo)
strCat2 = '"Surf_Hist_Pavement_Year" >= ' + str(num5YearsAgo) + ' AND "Surf_Hist_Pavement_Year" <= ' + str(num4YearsAgo)
strCat3 = '"Surf_Hist_Pavement_Year" >= ' + str(num12YearsAgo) + ' AND "Surf_Hist_Pavement_Year" <= ' + str(num6YearsAgo)
strCat4 = '"Surf_Hist_Pavement_Year" < ' + str(num12YearsAgo)


arcpy.AddMessage(strCat1)
arcpy.AddMessage(strCat2)
arcpy.AddMessage(strCat3)
arcpy.AddMessage(strCat4)

# Set Output parameters
arcpy.SetParameterAsText(1, strCat1)
arcpy.SetParameterAsText(2, strCat2)
arcpy.SetParameterAsText(3, strCat3)
arcpy.SetParameterAsText(4, strCat4)
Tags (2)
0 Kudos
6 Replies
StacyRendall1
Occasional Contributor III
Yo, just played around with this; despite what the documentation says, the indexes for your output parameters need to start at 0... In your previous code it was only the last SetParameterAsText that actually fails. It works for me if I do this:

import arcpy, datetime

currYear = datetime.datetime.today().strftime("%Y")
arcpy.AddMessage("Current Year = " + str(currYear))


# Set Local variables
num3YearsAgo = int(currYear) - 3
num4YearsAgo = int(currYear) - 4
num5YearsAgo = int(currYear) - 5
num6YearsAgo = int(currYear) - 6
num12YearsAgo = int(currYear) - 12

# Set up Selection Criteria to be passed to next step
strCat1 = '"Surf_Hist_Pavement_Year" >= ' + str(num3YearsAgo)
strCat2 = '"Surf_Hist_Pavement_Year" >= ' + str(num5YearsAgo) + ' AND "Surf_Hist_Pavement_Year" <= ' + str(num4YearsAgo)
strCat3 = '"Surf_Hist_Pavement_Year" >= ' + str(num12YearsAgo) + ' AND "Surf_Hist_Pavement_Year" <= ' + str(num6YearsAgo)
strCat4 = '"Surf_Hist_Pavement_Year" < ' + str(num12YearsAgo)


arcpy.AddMessage(strCat1)
arcpy.AddMessage(strCat2)
arcpy.AddMessage(strCat3)
arcpy.AddMessage(strCat4)

# Set Output parameters
arcpy.SetParameterAsText(0, strCat1)
arcpy.SetParameterAsText(1, strCat2)
arcpy.SetParameterAsText(2, strCat3)
arcpy.SetParameterAsText(3, strCat4)


Let me know how you get on.
0 Kudos
LoriEmerson_McCormack
Occasional Contributor
Worked like a charm.  Thanks!

The other code samples I looked at all had an input parameter which is why their output started with an index of 1 instead of 0.
0 Kudos
StacyRendall1
Occasional Contributor III
I meant the actual ESRI documentation...
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//000v00000019000000

It is usual for Pyhton indexes to start at 0, which is why I gave it a shot, but ESRI ought to at least be consistent between get and set!
0 Kudos
DarrenWiens2
MVP Honored Contributor
Stacy, your link points to a consistent example (the same list is being referenced by both get and set).

Index 0 = input parameter
Index 1 = 1st output parameter
Index 2 = 2nd output parameter
Index 3 = 3rd output parameter
0 Kudos
StacyRendall1
Occasional Contributor III
Excellent!
0 Kudos
NigelBerjak
Occasional Contributor II

Hi all

I'm trying to use your script as an example for myself to try work out how to parse the SetParameterAsText results to a model's parameters. Your script works and runs but I simply cannot get the results into a subsequent model's parameters. I have tried looking for a clear example online that shows the entire process of setting the parameter text and then using it, but I simply don't know where to 'put' the parameter text. I'm sure it is quite simple, but I'm just not seeing it. Presently I've done your script, placed it into ArcGIS scripts with the parameters parsed into the ArcGIS, as per the example image below. However, now what do I do to place the derived output into the 'next' model/script?

Thanks,

Nigel

Set Parameter as Text example

0 Kudos