[GIS] Converting ModelBuilder to Python Script

arcpymodelbuilder

I have exported a ModelBuilder into a Python script. I am not able to figure it out at which point of the python script I need to make changes so that I can completely run the python script in Python IDE

Here is the ModelBuilder screen shot

enter image description here

Here is the ModelBuilder script exported to Python Script.

 # Import arcpy module

import arcpy
from arcpy import env
from arcpy.sa import *

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


# Local variables:
ElevationContourRingSimply = "W:\\Projects\\FacilityProcessingtest\\ShpPython\\HobokenFlood.gdb\\ElevationContourRingSimply"
Value = "20"
fdcb_Value_ = "W:\\Projects\\FacilityProcessingtest\\ShpPython\\HobokenFlood.gdb\\fdcb%Value%"
fdcb_Value___2_ = fdcb_Value_
fdcb_Value___3_ = fdcb_Value___2_
fdcb_Value___4_ = fdcb_Value___3_
fdcb_Value___5_ = fdcb_Value___4_
fdcb_Value___6_ = fdcb_Value___5_

# Process: For
arcpy.IterateCount_mb("1", "20", "1")

# Process: Select
arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= %Value%")

# Process: Add Field
arcpy.AddField_management(fdcb_Value_, "WaterLevel", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(fdcb_Value___2_, "WaterLevel", "%Value%", "VB", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(fdcb_Value___3_, "GRIDCODE", "%Value% - [GRIDCODE] + 1", "VB", "")

# Process: Alter Field
arcpy.AlterField_management(fdcb_Value___4_, "GRIDCODE", "FloodDepth", "FloodDepth", "", "4", "NON_NULLABLE", "false")

# Process: Delete Field
arcpy.DeleteField_management(fdcb_Value___5_, "FID_FloodAllW")

Best Answer

I see several items that will need to be cleaned up. Going from top to bottom...

  1. You need to use the entire toolbox path to import it. A stand-alone script won't know how to find it just by name. Also, open your toolbox properties and make sure you have given it an alias. That way you won't have to type out the whole name every time you use a tool from it.
  2. The For loop will need to be put in python. The iterator is a modelbuilder-only tool. Python can't use it. It looks like you are iterating through a range? If so, that would be "for x in range(1, 20):"
  3. You will need to fix your local variables so python can use them. The reason why you have a bunch of variables that all look alike is because your model is a chain. If your workflow processes are all using the same input, you don't need all the separate variables in python. So pare them down to one.
  4. In your processes, you will need to change the input parameters so python can use them.

For example, instead of "%Value%", you will just put Value, i.e., instead of

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= %Value%")

you will put

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= " + str(Value))

or if GRIDCODE is a text column

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= \"" + str(Value) + "\"")
  1. If you want to be able to input different parameters, you will need to add user input code at the top of the script. (Param1 = arcpy.GetParameterAsText(0)) If you do that, since you are making a stand-alone script (not running out of an ArcGIS toolbox), you will need to add code to check the user's inputs to make sure they are something that will work. Running scripts out of a toolbox saves you a lot of coding.
  2. You will also want to add some error control. This comes in the form of a try, except, finally statement.

If you know ahead of time that your final tool will be a script tool, it is more efficient to code it as such from the beginning. Making a model first doesn't save you any time or effort. The only reason I ever do that is if the ESRI documentation on a tool is not very clear (at least to me), and I need to see how a working model actually shows up in python.

I hope this is helpful and gets you started.

Related Question