This is my script where I am trying to use selectby_attribute within a model, based on a field, F*. There are 6 fields, so I want 6 derived layers. Then I am going to use bearing and distance tool on each of these output layers. I cannot pass query to the model. How do I call a script from my 1st model, then call a model from this script?

import arcpy, sys, string, os

#arcpy.env.workspace = arcpy.GetParameterAsText(0)
in_FC = arcpy.GetParameterAsText(0)
#FList = arcpy.GetparameterAsText(1)
#F_list = FList.split(";")
#arcpy.ListFields(in_FC, "Rev_2016_F*")

arcpy.AddMessage("The table %s exists" % (in_FC))
CurrentWorkspace = r"D:\Fort_Belvoir\Conservation\Conservation.gdb"

#in_Table = "D:\Fort_Belvoir\Conservation\Conservation.gdb\QryTab4_CopyRows"

#tbx = arcpy.AddToolbox("C:/Temp/MyToolbox.tbx")
tbx = arcpy.AddToolbox(r"D:\Fort_Belvoir\Conservation\PIF.tbx")
#arcpy.AddMessage("this is toolbox %s" % (tbx))

# Get a list of field objects
num = 0
fields = arcpy.ListFields(in_FC, "Rev_2016_F*", "LONG")

for field in fields:

    # Check the field name, perform a calculation when finding the field 'Flag'
    # Local variables
    anum = str(num)
    thetab = "F" + anum
    #arcpy.SetParameter(1, SR)
    theFld = field.name
    anum = str(num)
    thetab = "F" + anum

    out_FC = r"D:\Fort_Belvoir\Conservation\Conservation.gdb\PIF" + os.sep + thetab

        arcpy.SetParameter(1, theFld)
        arcpy.SetParameter(2, out_FC)
        tbx.Model2_Toolbox(theFld, out_FC)


    num = num + 1

enter image description here


1 Answer 1


As @PolyGeo told:

I suggest you look at ImportToolbox rather than AddToolbox. However, I suspect using a single Python Script tool may be simpler than mixing ArcPy and ModelBuilder any more than necessary.

I think it is better not to mix models with arcpy scripts, however I also used to do it. Assuming you have a local toolbox with models and scripts inside. First step is to import toolbox.


And then everything inside toolbox become callable with arcpy. I.e. if you had a model called 'Calculate_distances' with input parameters 'feature_path' and 'out_path' you can type then:

arcpy.Calculate_distances(r'path_to_fc', r'path_out')

Be patient to call the the exactly model/script name, not its alias which is in toolbox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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