1

I am trying to create a derived output parameter for a Python script tool so that the output will show in a model (I want to use the output folder as an input for another tool). I want this output to be a folder that is the same as an input folder after geoprocessing has put the results (outShapeFiles) into that folder. Essentially, I have 4 initial parameters, one of which is the outFolder = sys.argv[4]. At the end of the code, I set `outFolder = arcpy.SetParameterAsText(5, "Output Features"). When I run it, everything works but the last part (the outShapefiles are created and put into the outFolder). I get this error message: "RuntimeError: Object: Error in setting parameter as text." Here is the code. Can you see how I can resolve this error so that my output parameter can be viable in a model? Note how I set outFolder at the end.

input_FC = sys.argv[1]
inField = sys.argv[2]
theFName = sys.argv[3]
outFolder = sys.argv[4] #Set as an input parameter in the script tool

script = sys.argv[0]
msg = "\nRunning: ... {}".format(script)
arcpy.AddMessage(msg)

arcpy.env.overwriteOutput = True
...

...
aMax = 1
for aVal in valueList:
  aMax = max(aMax, len(str(aVal)))
for aVal in valueList:
  if (str(aVal).isdigit()) and (not inType == "String"):
    fs = '"' + "%" + str(aMax) + "." + str(aMax) + 'i"'
    aSuffix = fs % aVal
    aVal = str(aVal)
  elif inType == "Double" and inScale == 0:
    aSuffix = str(aVal).replace(".0","")  ###### 
    aVal = str(aVal).replace(".0","")
  else:
    aSuffix = str(aVal) 
    aVal = str(aVal)
  try:
    aSuffix = aSuffix.replace(" ","_")  #replace garbage in output files
    aSuffix = aSuffix.replace('"',"")
    aSuffix = aSuffix.replace("/","")
    aSuffix = aSuffix.replace("-","")
    outName = theFName + aSuffix + ".shp"
    outShapeFile = outFolder + "/" + outName
    outShapeFile = outShapeFile.replace("\\","/")
    # 
    #Create a query and produce the file
    if (not aVal.isdigit()) or (inType == "String"):
      aVal = "'" + aVal + "'"
    whereClause = "%s = %s" % (inField, aVal)
    arcpy.MakeFeatureLayer_management(input_FC, "TempLayer", whereClause)
    arcpy.CopyFeatures_management("TempLayer",outShapeFile)
    arcpy.AddMessage("Output and query: " + outShapeFile + "  " + 
    whereClause)
  except:
    whereClause = "%s = %s" % (inField, aVal)
    arcpy.AddMessage("Output and query: " + outShapeFile + "  " + 
    whereClause + " did not work ")

arcpy.SetParameterAsText(5, outFolder) #This is where I try to 
make outFolder an output parameter. I have set it to derived from the input "outFolder".

arcpy.AddMessage("\n  Processing complete" + "\n")
del arcpy
  • In your question, you state that you are setting: "outFolder = arcpy.SetParameterAsText(5, "Output Features")", but your code shows differently: outFolder = arcpy.SetParameterAsText(6, "Output"). – Brad Mar 22 '18 at 15:10
  • Sorry, I had changed it and run it in a test, thinking that sys.argv uses an index starting with zero, so technically, counting sys.argv[0], outFolder as an output would be the 6th parameter defined. Basically, I got the same error. I am confused on this point since I am trying to use sys.argv and arcpy.SetParametersAsText in the same script. – gwydion93 Mar 22 '18 at 15:18
1

So, the answer to this question is simple. I first changed my primary input parameters to use arcpy.GetParameterAsText.

 input_FC = arcpy.GetParameterAsText(0)
 inField = arcpy.GetParameterAsText(1)
 theFName = arcpy.GetParameterAsText(2)
 outFolder = arcpy.GetParameterAsText(3)
 script = sys.argv[0]

At the bottom, I added my ouput parameter, set to equal outFolder (I probably could have simplified this).

 output = outFolder
 arcpy.SetParameterAsText(4, output)

Now, at the top, notice that arcpy.GetParameterAsText starts its index at 0, not 1. This was the crux of my problem before. Once I set my final output parameter to 4, it was fine. I then set it as the last item in my parameter settings as Derived (automatically as an output) and it worked fine. enter image description here

The main lesson here is to always start the index off at 0, not 1. Hope this helps anyone with a similar problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.