Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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, 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.

share|improve this question
crazy thought: instead of the expression builder, what about a python command line window that saves it history for the model to re-use? – matt wilkie May 16 '12 at 17:57
actually what I'm really after is the entire Python library to be exposed to model builder the same way arcgis tools are. but that's just crazy talk (right?) – matt wilkie May 16 '12 at 18:01
Great idea! You want to write code snippets on the fly into a "tool" that will process the snippet and return the results, right? So in your example, the "tool" would return <current username> which could then be applied to other tools downstream. – RyanDalton May 16 '12 at 21:04
@RyanDalton, yes that's exactly right. – matt wilkie May 16 '12 at 21:58
up vote 3 down vote accepted

New answer to an old post, just to clarify how to create a re-usable model to do this.

Open the model, Insert-> Model Only Tools-> Calculate Value.

In the Expression, type: getuser()

In the Code Block, type:

def getuser():
    import getpass
    return getpass.getuser()

(See "Getting user name in specific format" for source of username code snippet from @ChadCooper)

For Data Type, choose String.

enter image description here

Rename the output circle to username. The end-result model will look like: Using Calculate Value tool

And you can then just drag this model into any future models and reference the variable called %username% in any future models or scripts. Just be sure this model runs before you try to access the %username% variable. You can do this by using a precondition.

share|improve this answer
Ryan perhaps you could expand on why output is a [P]arameter, and show example of the module being included in another module. I was initially very confused why P for an output, as parameters are usually all about inputs. – matt wilkie Jun 29 '15 at 19:47
@mattwilkie, you don't need any input parameters because the python script will "read" the inputs from your system. However, you need to make the output (the username) a parameter so that you can consume the results into another model or python script. The next step would be passing the username parameter into something like an "email script" that would automatically use the uesrname output parameter as the input to the "To" email address. – RyanDalton Sep 3 '15 at 16:21

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).

share|improve this answer
thank you! This worked quite well for what I needed today (insert %USERNAME% into an .sde database connection). Agreed the editor is not worthy of that name, works fine as a paste destination. – matt wilkie May 17 '12 at 15:42
re: 9.x, the wording of the CV help page indicates the tool might be available if the SA/3D extensions are merely installed. It may be possible to use it without the extension being licensed or activated. – matt wilkie May 17 '12 at 15:48
"I would suggest writing modules that you can then import" - that's a really great idea! – Curtis Price May 13 '13 at 17:21

Perhaps I'm misunderstanding the question but it seems that the python script tool functionality, within ArcToolBox, is what you are looking for:

You can in fact use/combine several other scripting languages as well (R in this example):

share|improve this answer
Also see Integrating scripts within a model – Chad Cooper May 16 '12 at 19:58
This works (and is how I would do it), but Matt specifically called this method "a lot of work for such a small thing" in his question :) – nmpeterson May 16 '12 at 21:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.