arcgis-desktop – Adding Arbitrary Code to ArcGIS ModelBuilder

arcgis-desktopmodelbuilder

Is there any way to add arbitrary code snippets to an ArcGIS model?

I envision something similar to the "show code block" the expression builder in advanced labelling or field calculator.

For example my project of the moment could benefit greatly from fetching the username of the person running the tool. I know in python this is a simple import os; os.environ.get('USERNAME'). I think I could write that snippet to get_username.py, add it to a toolbox in Arccatalog, then add that tool to the model and use it as per-requisite model parameter for my current tool. That's a lot of work for such a small thing, and next time I might want something else, like inline variable substitution or a compound variable.

I think a code snippet tool or similar could be address all of these needs in a generic, broadly helpful manner.

Best Answer

Actually you can do this with the Calculate Value (Data Management) tool.

It's not particularly elegant (the entire source code for the tool is printed in the log every time it runs), and the editor (really just a text box) is abysmal.

I would suggest writing modules that you can then import and call functions from and then you would only need to write very short snippets in the dialog. Otherwise it's just not very clean.

Calculate Value is available at all license levels for Arcgis v10. With v9.x it is available to Arcview only if the Spatial or 3D Analyst extensions installed (ref).