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.

  • crazy thought: instead of the expression builder, what about a python command line window that saves it history for the model to re-use? Commented May 16, 2012 at 17:57
  • 2
    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?) Commented May 16, 2012 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. Commented May 16, 2012 at 21:04
  • @RyanDalton, yes that's exactly right. Commented May 16, 2012 at 21:58

3 Answers 3


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

  • 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. Commented May 17, 2012 at 15:42
  • 1
    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. Commented May 17, 2012 at 15:48
  • "I would suggest writing modules that you can then import" - that's a really great idea! Commented May 13, 2013 at 17:21

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.

  • 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. Commented Jun 29, 2015 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. Commented Sep 3, 2015 at 16: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):


  • Also see Integrating scripts within a model Commented May 16, 2012 at 19:58
  • 3
    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
    Commented May 16, 2012 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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