AnsweredAssumed Answered

Problem accessing ModelBuilder variable in a python script?

Question asked by ecopirate on Jul 25, 2012
Latest reply on Jul 29, 2012 by clthompson
Dear ArcPy & Python Folk,

I am trying to make a fairly simple model in ArcGIS10.1 ModelBuilder to do the following:

i) Iterate through a polygon feature layer (High_resolution.shp).
ii) As each new polygon is selected fire up a python script ( that zooms to the selected polygon and exports a Geojpeg

Model looks like this:


Code for the script looks like this:

import arcpy  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd,"Imagery")[0] lyr = arcpy.mapping.ListLayers(mxd, "High_resolution", df)[0]  # Use the SelectLayerByAttribute tool to select and  # zoom to the selection  arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "AreaID = '%output_value%'") df.zoomToSelectedFeatures() arcpy.RefreshActiveView()  # Export the map to a .jpg  # Next Line Wrong! Outputs a jpeg actually called "test%output_value%.jpg" # outfile = "C:/Test/test" + "%output_value%" + ".jpg"  # Next Line Wrong! Gives Syntax error # outfile = "C:/Test/test" + %output_value% + ".jpg"  # Works.... outfile = "C:/Test/test1.jpg"  arcpy.mapping.ExportToJPEG(mxd, outfile, df, df_export_width=1600, df_export_height=1200, world_file=True)  # Clear the selection and refresh the active view arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") arcpy.RefreshActiveView()

My problem:

I can not get the Exported Jpeg filename to change each time for a new polygon.
Ideally I would like a series of output jpegs for each poly: test1.jpg. test2.jpg, test3.jpg, etc....

Current script is not able to recognize the ModelBuilder variable %output_value% when trying to form the outfile name.

However, I can leave the output file as static name (eg. "C:/Test/test1.jpg") and then manually periodically check it:
It is working correctly and a new geojpeg for each polygon is being made (but unfortunately overwriting the previous jpeg).

Quite confused as this would indicate that the ModelBuilder variable is getting passed correctly to the script eg:
"arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", "Area = '%output_value%'")"
is working correctly....

So how do I successfully use the ModelBuilder %output_value% to make sequential filenames???
Or is there some other work-around? Didn't really expect this to be such a problem....

PS. I also observe that arcpy.RefreshActiveView() is also not updating the screen for each new polygon (which would be nice).

Many thanks in advance for your advice,

Ian Thomas