How to Get User Input for New Python Tool

1931
9
02-14-2020 10:18 AM
RobertStevens
Occasional Contributor III

I have written a number of tools using arcpy as pure Python standalone scripts.

Now I want to make an interactive tool that I can use, for example, in a model pipeline.

I have the Python code which I can run as a standalone, and get the parameters from the python command line

e.g

    python <script_name> <parameter1> ... <parameterN>

All the tools supplied by ESRI have a GUI to interact with the user and collect these run time parameters. How do I do that instead of using the command line?

Specifically I need to get the name of an input and output feature class, and also to collect a list of field name mappings (so there will be an a priori unknown number of these).

I am not sure whether I am posting this question to the proper group. Please tell me if not.

Tags (3)
0 Kudos
9 Replies
JoeBorgione
MVP Esteemed Contributor
DanPatterson_Retired
MVP Esteemed Contributor

the custom toolbox is the easiest since a dialog allows you to specify the parameters you need

There is a post I did that walks you through the basics

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 

RobertStevens
Occasional Contributor III

Thank you Joe, thank you Dan.

I used the procedures that you both recommended (actually, I am using Arcap, not pro, but it was easy given the toehold that you provided to find the corresponding pages in the latter).

It was straightforward to get boxes for the input and output feature classes.

I cannot seem to get the list of field renames to work. Nothing I do seems to allow me to enter values. In the list of types there is a "Field Info" type, and the GUI shows the boxes. But it does not allow me to enter anything. Even if I select a type of "String" with multiple values I cannot enter any data. I must have a severe misunderstanding of what these data boxes mean. I attach a screen shot of where I am right now.dialogue box for python script tool

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

If memory serves a list of fields would be a derived parameter.  That is, the field names are "derived" from the input featureclass or attribute table. So I suspect the same would be for the field mapping

0 Kudos
RobertStevens
Occasional Contributor III

Dan

Onwards!  I have an answer to my last difficulty. The problem was that with the data type of Field Info the Multivalue parameter property should be "No". I had it set to "Yes". I would say that the Multivalue property needs to be better explained in the documentation.

Now I need to figure out how that data is returned to my python script. That is not at all self evident since there seems only the capability to read the data as a string. I am wondering whether it is some kind of JSON.

0 Kudos
DavidPike
MVP Frequent Contributor

Would it be a field mapping object you would call as arcpy.GetParameter()? Good luck, field mapping objects give me nightmares.

0 Kudos
RobertStevens
Occasional Contributor III

Hi David

Amen to what you said in your second sentence. They are not well described in ESRI documentation, and my understanding, which can scarcely be the right word, is only superficial derived from reading the ad-hoc code of others.

If I understand you properly, I think you are telling me that GetParameter() returns me an object of the type described in the the parameter list of the tool. I am coming up to speed with how to use these APIs (eg how to write meaningful stuff into the Results, and how to reports errors and warnings).

0 Kudos
DavidPike
MVP Frequent Contributor

Exactly, I would test this with another script tool with only a feature and the field map and see if you can grab a class attribute or type.

Something like

fieldmap = arcpy.GetParameter(1)

property = fieldmap.inputFieldCount

arcpy.AddMessage(property)

0 Kudos
RobertStevens
Occasional Contributor III

I was able to get this to work, even though the arcpy documentation is not very good.

It turns out that I really didn't need to expend the time and effort although it was a very useful learning exercise.

I simply wanted a tool where I could rename several fields at once, the Alter Field tool in the standard ESRI toolbox only allows you to change one field at a time: having to run that tool a score of times is tedious. Instead you can use the Merge tool, but you only enter one file, and then change the names of the fields in the dialogue box. It has the disadvantage that you have to create a new feature class whereas Alter Field allows an in-place modification.

I still do not properly understand this API. The dialogue box I get has a checkbox to set the field's visibility. But this is a function of a layer, not of a feature class (isn't it???).  This whole arcpy toolkit really is second rate, and especially so the documentation that goes with. 

0 Kudos