Bug in Modelbuilder iterators when run as tool

7143
9
07-25-2014 10:50 AM
curtvprice
MVP Esteemed Contributor

I just found a bug in 10.2.1 / 10.2.2 that has already been logged but is not fixed. Thankfully it is marked Severe!

[#NIM103740  When a model with an Iterator is run from within the Model editor, it will complete correctly, but overwrites the outputs when run as a tool. ]

The issue I ran into is that when you use an iterator output variable in the output name variable (for example %outworkspace%/%name%) it gets set in iteration 1 and 2, but after that it reverts to the value from iteration 1. In my test example, my inputs are pp01.txt, pp02.txt, .. pp05.txt and are read correctly, but the output name variable is set to.

pp01

pp02

pp01

pp01

pp01

This is quite a show stopper.

The only workaround at this time seems to be to not run the model as a tool.

0 Kudos
9 Replies
IanMurray
Frequent Contributor

Could you convert the model to a python script then turn the script into a script tool?  That way you would still have an GUI to work with, but it shouldn't run into the same error as running it as a regular tool.

curtvprice
MVP Esteemed Contributor

I agree ModelBuilder is a really nice environment for putting a workflow together.

However, models with iterators do not directly convert and run as exported. In this case, you need to write the arcpy.ListFiles() and set up the loop - ModelBuilder will not do that for you.

0 Kudos
IanMurray
Frequent Contributor

Thats true, but the changes that need to be made are not very major.  While it would be best if the bug was addressed, I was merely suggesting this a workaround.

0 Kudos
curtvprice
MVP Esteemed Contributor

Thanks Ian.

I guess users of Model Builder‌ should think about upgrading to 10.3 as soon as it is released later in 2014.

Update, the bug above has been rejected in favor of this one. According to the URL, this was apparently found at Beta 10.1 and is marked as fixed in 10.3.

[NIM098729 In ArcGIS for Desktop 10.2.1, when running a model with the Iterate Feature tool, an incorrect number of outputs are generated only when running the model as a tool. ]

Alternate Solution

Instead of running the model as tool, use the Edit window.

1. Right-click the model in ArcCatalog.

2. Navigate to Edit > Validate > Run.

http://support.esri.com/en/bugs/nimbus/role/beta10_1/TklNMDk4NzI5

This is how Ian's workaround works (for the good of the thread, and as a training exercise for me on posting pictures and code in GeoNet).

Here's a picture of my test model. The issue is the value of Output_Name is "%workspace%\%Name%" and the %Name% variable is not getting updated properly for iterations 3..n.

Capture.PNG

Here's what gets exported:

# -*- coding: utf-8 -*-

# ---------------------------------------------------------------------------

# python_iterator_raw.py

# Created on: 2014-07-25 12:26:57.00000

#   (generated by ArcGIS/ModelBuilder)

# Usage: python_iterator_raw <workspace> <ascii>

# Description:

# ---------------------------------------------------------------------------

# Import arcpy module

import arcpy

# Load required toolboxes

arcpy.ImportToolbox("Model Functions")

# Script arguments

workspace = arcpy.GetParameterAsText(0)

if workspace == '#' or not workspace:

    workspace = "D:\\Users\\cprice\\work\\test" # provide a default value if unspecified

ascii = arcpy.GetParameterAsText(1)

if ascii == '#' or not ascii:

    ascii = "D:\\Users\\cprice\\work\\Iterator_test\\ascii_rasters" # provide a default value if unspecified

# Local variables:

File_txt = workspace

Output_Name = File_txt

Name = workspace

# Process: Iterate Files

arcpy.IterateFiles_mb(ascii, "", "txt", "NOT_RECURSIVE")

# Process: ASCII to Raster

arcpy.ASCIIToRaster_conversion(File_txt, Output_Name, "INTEGER")

And here's the working python script I created from the above

# -*- coding: utf-8 -*-

import arcpy

import os

input_folder = arcpy.GetParameterAsText(0)

output_folder = arcpy.GetParameterAsText(1)

arcpy.env.workspace = input_folder

infiles = arcpy.ListFiles("*.txt")

for f in infiles:

    outraster = os.path.splitext(f)[0] # "file.txt" -> "file")

    outraster = os.path.join(output_folder, outraster) # add output path

    arcpy.ASCIIToRaster_conversion(f, outraster, "INTEGER")

    arcpy.AddMessage("Created {}".format(outraster))

0 Kudos
curtvprice
MVP Esteemed Contributor

We took this up with support and there is a hotfix (not a patch) for this problem for 10.2.1. If you are current on support and need this to work I recommend you contact support and request it. The hotfix number is 293414 and the code is: QFE-1021-DT-293414.

We are requesting they do a fix that applies to 10.2.2 for people that are going to be stuck at 10.2.x for a while to keep their ArcGIS 3rd party modules in synch.

0 Kudos
BledarBirbo1
Occasional Contributor

I have the same issue on 10.2.2

0 Kudos
curtvprice
MVP Esteemed Contributor

You should probably update to 10.3.1 if you can, which is out now.

0 Kudos
Tracy_Maier
New Contributor

This same issue seems to be happening  in ArcGIS Pro 2.6.3

0 Kudos
NoahEnverus
New Contributor

 Same in Pro 2.9.1

0 Kudos