Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

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
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?) –  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
add comment

2 Answers

up vote 9 down vote accepted

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
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. –  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
add comment

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

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Integrating_scripts_within_a_model/002w0000006n000000/

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001500000006000000.htm

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

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000007w000000

share|improve this answer
    
Also see Integrating scripts within a model –  Chad Cooper May 16 '12 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 May 16 '12 at 21:10
add comment

Your Answer

 
discard

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.