I have a finished model that has been saved as a model tool in a custom toolbox. I add the toolbox to ArcToolbox, double click, set the parameters, and run just like any other tool. I have the model setup to add certain results to the map display. However, when running as a model tool and not in edit mode, the results will not add to the display. Is this a workspace environment issue?
[GIS] ArcGIS 10.2 Model Builder, getting results to ‘add to display’ when running as a model tool
arcgis-10.2arcgis-desktopdisplaymodelbuildermodelling
Related Solutions
I agree with Jason's comment about avoiding relying too much (or at all) on the export to Python functionality in ModelBuilder. I also think that if you are brand new to Python and programming in general, it's better to cut your teeth on "pure" Python than Python + ArcGIS. But since your task is to do exactly that, here are my suggestions:
Think about what your model or code is actually trying to accomplish. Write a written description of what the intended purpose of the tool is and place it as a comment at the top of your script, to remind you of your goal.
Think about how you would do it manually/interactively in ArcGIS. Write this down as a series of steps, or flowchart it if you prefer.
Try grouping these steps into logical sections, which can later be turned into functions. Think about what inputs and outputs (if any) apply to each of these sections. These will be your function's arguments and return values.
Stub out the basic structure of the script, or use a template.
I like to just start off with a few imports I know I'll probably end up using:
import arcpy, os, sys
Stub out the functions I think I'll need:
def buildWhereClause(table_name, field_name, value): # TODO: construct a SQL WHERE clause based on the given parameters return "(some hardcoded WHERE clause)" def selectFeatures(layer, where_clause=None): # TODO: select the features in the given layer and using the optional SQL WHERE clause pass
(These are just examples and may be overly simplistic but it hopefully gets the point across.)
After that I'll usually have an
if __name__ == "__main__"
section that either contains or calls a main procedure that is the entry point to the code and starts taking inputs and calling other functions. The purpose of this check is to prevent the code from running if all you want to do is import the module and call its functions independently:if __name__ == "__main__": # TODO: Get user-entered parameters, hardcode them for now input_table = r"C:\test.shp" field_name = "NAME" field_value = "12345" whereclause = buildWhereClause(input_table, field_name, field_value) layer = arcpy.MakeFeatureLayer_management(input_table) selectFeatures(layer, whereclause)
Test the script, either from the script tool dialog, a Python IDE (if it doesn't need to run inside ArcMap), or by importing the module into the ArcMap Python window and calling specific functions.
- Implement the rest of the code, referring to documentation as needed, testing frequently, and iteratively improving the script until it is complete.
See also:
- What are some resources for learning ArcPy?
- How do I execute a script in the arcpy window?
- How do I include a variable in the where clause of arcpy.Select_analysis?
- https://gis.stackexchange.com/a/52701/753 (General tips for another Python/arcpy beginner)
[GIS] Getting arcpy.AddMessage() to display message from script tool called from another script tool
When you call a script tool it behaves just like any other geoprocessing tool you call from Python. No messages are automatically added to the console (unless there is an unhandled exception).
What you want to do is use arcpy.GetMessages() and arcpy.AddMessage, AddWarning, AddError, etc. after calling the script tool as described in the help.
Best Answer
Try making the output a parameter and then it should add it to the display when you run the tool from the toolbox.