2

I am building a model to add many fields to an iteration of feature classes. For each step (broken into sub-models) to add a field I am checking, using a boolean Field Check script, whether the field already 'Exists'. If it does not the output for '0' acts as a precondition to run the Add Field tool. If the field does exists, the sub-model simply ends.

What I need to achieve is a way to have the branch logic end at an output variable regardless of whether the field exists or not, this way I can pass the output to the next 'Add Field' sub-model. Below are pictures of a sub-model and the whole model. Sub-model - Field1

Model

4

I believe you need only use the Merge Branch model only tool. As you can see in their figure the two branches feed into the merge branch tool resulting in a single output that continues the workflow. Looking at your example I'm not sure you need to use collect values as that returns a list so could never feed into your next sub-model as that takes a FeatureClass as input. The output of your merge branch tool you would expose as a parameter and that is what feeds into your next sub-model.

Personally I think your approach is not the best method. If it were me I would put all the Add Fields into a single sub-model and process them there with a single featureclass feeding in and out of it.

So my sub model would be something like this:

Sub-model

The code in the field Calculate Value tool would be:

import arcpy,os
def hasField(fc):
  arcpy.env.workspace = os.path.dirname(fc)
  fieldList = [field.name for field in arcpy.ListFields(fc)]
  if "Path2" in fieldList: # Path2 is what you change to fit your add field
    b = False
  else:
    b = True
  return b

The tool set up as:

Tool

4
  • I would appreciate some help on this error. It says the feature class does not exist: ERROR 000539: Error running expression: hasField(r"") Traceback (most recent call last): File "", line 1, in File "", line 4, in hasField File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy_init_.py", line 1139, in ListFields return gp.listFields(dataset, wild_card, field_type) File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\geoprocessing_base.py", line 346, in listFields self._gp.ListFields(*gp_fixargs(args, True))) IOError: "" does not exist Failed to execute (Calculate Value). – ndthl Jan 20 at 10:50
  • You don't appear to have provided an input it says hasField(r""), note I provide a layer name using inline variable substitution hasField(r"%FeatureClass%"). – Hornbydd Jan 20 at 11:18
  • So you can see that %Name% is replaced by the text SWARBYCH, it is not the full path name to your featureclass, it's a layer name, the line of code referring to workspace, see how it sets it to the dirname of the dataset, you have passed in literally the text SWARBYCH and not something like c:\temp\data\SWARBYCH.shp – Hornbydd Jan 20 at 12:26
  • 1
    Thanks, I fixed this: hasField(r"<path>.gdb\%Name%") – ndthl Jan 21 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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