Python Script Running in Model Builder before another Operation Starts

3519
10
Jump to solution
09-14-2015 12:09 PM
ClintonCooper1
New Contributor III

I have a built a rather large Model in Model Builder that process several
million records.  Part of the model includes running an update cursor
script before it does a Dissolve on that dataset.  I have gotten the
python script run, but not in the right order.  In fact, it only runs at
the very end of the whole model.  As I am new to Model Builder, I cannot
figure out how to get it to run in the proper order, before the Dissolve
process and not at the very end of the whole model.  Thanks for your
help in advance!! 

I am running ArcGIS 10.3

0 Kudos
1 Solution

Accepted Solutions
WesMiller
Regular Contributor III

Once you've added the code to your script you'll need to fix your tool to show your changes. Right click your tool in catalog and select properties then set your tool like below you may need to remove and reload into your model

View solution in original post

0 Kudos
10 Replies
WesMiller
Regular Contributor III
0 Kudos
ClintonCooper1
New Contributor III

Thanks/  When I try t set the precondition, I get a pop up that says:  Can only Connect a Variable to a Process.

The script is just a .py file that I have imported into my toolbox.  Do I need to set up anything within that text file to get it to be set as a precondition?

0 Kudos
WesMiller
Regular Contributor III

Does your script have any input output variables you could then set the precondition of the process following the script to wait for the script.

0 Kudos
ClintonCooper1
New Contributor III

It does not.  It just simply runs an update cursor calculation on a dataset updating a bunch of fields.

here is my script:

import arcpy, collections
fc = r"E:\MATCH_WARD"

fields = ('AMOUNT','PARTY_IDEOLOGY','UNK','REP','DEM','CONT_DEM_NUM','CONT_REP_NUM','CONT_UNK_NUM')

with arcpy.da.UpdateCursor(fc, fields) as cursor:
   for row in cursor:
     if row[1] == "UNK":
         row[2]=row[0]
         row[7]=1
     else: 
         row[2]=0
         row[7]=0

     if row[1] == "REP":
         row[3]=row[0]
         row[6]=1
     else: 
         row[3]=0
         row[6]=0

     if row[1] == "DEM":
         row[4]=row[0]
         row[5]=1
     else: 
         row[4]=0
         row[5]=0
            

     cursor.updateRow(row)
del row
del cursor

0 Kudos
WesMiller
Regular Contributor III

You may have to export your model to python and put your update cursor script in, where you need it to go or change your script to have a dummy output that you could use for preconditions.

0 Kudos
ClintonCooper1
New Contributor III

Can you give an example of what a dummy ouput would look like and how to connect the script to the dummy ouput and then to the next process in Model builder?  Thanks again!!

0 Kudos
WesMiller
Regular Contributor III

Add  this code to your script in your tool set a variable as derived and as text

import arcpy

dvariable = "Dummy"

arcpy.SetParameterAsText(0,dvariable)

0 Kudos
ClintonCooper1
New Contributor III

Thank you so much for your help and patience with me!  I fully understand about adding the text within my script and all that, my trouble is understanding what I need to do in model builder.  Can you give a bit more detailed instruction for what I need to do within model builder such as setting a variable as derived and as text?  thanks !!!!!!!!

0 Kudos
WesMiller
Regular Contributor III

Once you've added the code to your script you'll need to fix your tool to show your changes. Right click your tool in catalog and select properties then set your tool like below you may need to remove and reload into your model

View solution in original post

0 Kudos