I am trying to make a custom made Python Script tool which I would like to use in Model builder. I am using the arcpy.Calulate_field_management function (simple reclass function as an example), but it seems not be working properly. Please note that when I run this script on its own through a Python editor (IDLE), the script works fine. After adjusting it to make it work for a custom Python Script tool, the script stops when it is going to calculate the field.
This is the script which I use for my tool with a comment where it stops. The arcpy.GetParameterAsText(0) refers to the table which you can find in the picture below.
import arcpy
try:
fc = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Hi Jelle, your script is starting now...")
def calc_value(orig_field):
if (orig_field == "R<10_min"):
return "1"
elif (orig_field == "R_10-20_min"):
return "2"
elif (orig_field == "R_30+"):
return "3"
def calc_field():
arcpy.AddMessage("Calculating field management has started, Jelle")
reclass_field = "Reclass_SA_All"
##HERE is WHERE PYTHON SCRIPT TOOL STOPS
arcpy.CalculateField_management(fc, reclass_field, "calc_value(!SA_All!)", "PYTHON_9.3")
arcpy.AddMessage("Reclass Field calculation is done, Jelle.")
arcpy.AddMessage("Starting calc_field function, Jelle...")
calc_field()
arcpy.SetParameterAsText(1, fc)
except:
arcpy.GetMessages()
Here is a picture of the table which I am trying to edit together with the customized script tool window when it is finished running. I have added customized messages in my script to find out where the script stops running.
I have the feeling I am doing a basic error which stops the scripts from running the calculate_field_management within my tool. Does anybody know what I am doing wrong? AM i right if I say the calculate_field_management cannot refer to another function within my expression? This does work when I run the script on its own through a Python editor (IDLE).