I've been working on a problem lately, and discovered that there was no out-of-the-box way to achieve this. In short, I have 2 feature classes going into a Spatial Join, but the fields in those feature classes could change each time the model is run. The issue here is that the Field Map in the Spatial Join tool is static, and can only be updated by editing the model, running up to the Spatial Join, opening the Spatial Join parameters and resetting the Field Map to include additional fields, or remove those no longer present in the input datasets.
This is obviously not ideal - Model Builder should be able to deal with this. Enter python script tools!
With a bit of help from a certain AI that everyone seems to know of, I quickly had a python script that when loaded into a python script tool, would take the Target feature class and Join feature class (or as many input datasets as required), iterates through the fields in each and adds them to a Field Mappings object. This Field Mappings object is then passed as an output of the script tool and as an input Field Mappings parameter to the Spatial Join. This then dynamically updates the Field Map of the Spatial Join every time the model is run.
Configuring the parameters of the python script to accept the multiple input datasets and output Field Mappings object, this tool was ready to be added to my model. In short, I now have a reusable tool that when added to any model, will generate a new Field Map for any GP tool that requires it (e.g. Export Features)
import arcpy
# Get semicolon-delimited input feature layers
input_features = arcpy.GetParameterAsText(0)
# Split into a list of layers
layers = input_features.split(";")
# Create FieldMappings object
field_mappings = arcpy.FieldMappings()
# Add fields from all input layers
for layer in layers:
fields = arcpy.ListFields(layer)
for field in fields:
if field.type not in ['Geometry', 'OID']: # Skip geometry and object ID fields
field_map = arcpy.FieldMap()
field_map.addInputField(layer, field.name)
field_mappings.addFieldMap(field_map)
# Set output parameter (derived)
arcpy.SetParameter(1, field_mappings)
This approach could be used to work with any tool that requires a Field Map (e.g. Export Features).
Next time you have a problem with tools in Model Builder not quite doing what you need, fire up ChatGPT, Copilot or another AI helper and ask it how to make your process happen. Check the code it provides thoroughly because they do make mistakes, but it's a great way to get into the coding space when you just don't quite know where to start.
***Update*** - since originally posting this, I have simplified it as I had made things more complex that necessary. Also another risk with the AI helpers. They don't always point out that there may be easier ways of achieving a problem as they tend to focus on the problem through the narrow lens of the prompts you entered. Always ask if a solution can be streamlined or done with fewer steps.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.