ArcPy ArcMap – Writing Python Script with Open Variables for ModelBuilder

arcmaparcpymodelbuilderparameters

I have the following script:

import arcpy
from arcpy import mapping

arcpy.env.workspace = r"C:\GIS\Test"
mxd = arcpy.mapping.MapDocument(r"C:\GIS\Test\Mxd_to_shape.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]

#Variables
shp_input = "hellow.shp"
layer_output = "test"

arcpy.MakeFeatureLayer_management(shp_input,layer_output)
layer = arcpy.mapping.Layer(layer_output)
arcpy.mapping.AddLayer(df,layer,"AUTO_ARRANGE")

mxd.saveACopy(r"C:\GIS\Test\Mxd_a_shape2.mxd")
del mxd

arcpy.AddMessage("Ended process")

As you can see, I have defined variables in the previous script.
However, I need these variables to remain "open" to later transform the script into a sub-tool of a ModelBuilder model.
The need is that apparently having the pre-set variables in the script, wanting to change them as parameters in ModelBuilder does not work.

Best Answer

I recommend reviewing the ArcMap help topic on Integrating scripts within a model:

Python or other language scripts can be integrated into models by making the script into a script tool and adding the script tool to a model. This can be used when Python or other scripting language logic is needed to enhance a model or to access an external package or program from within ArcGIS.

The starting place for integrating scripts within models is the actual script. The script must be written in such a way that it can be integrated into the geoprocessing framework. For more information, refer to Writing a Python script.

Once the script is written, a script tool can be created and added to a toolbox. This script tool can be added to a model and used to bring additional functionality to the model.

Related Question