3

I have created a pythonaddin toolbar, with a button that executes a python script tool. This tool is in an ArcToolbox which has been stored in the Install folder of the addin. It executes and runs perfectly. However, the rest of the script in the pythonaddin button class does not wait for the tool to finish. I searched on the ESRI help forums, and there was a suggestion that I use a while loop to wait for the result of the tool. However, I do not believe that a custom python script tool returns a result object like arcpy gp tools.

My toolbar (toolbar_addin.py) code

parameterList = []
class button1(object):
    def__init__(self):
        self.enabled=True
        self.checked=False
    def onClick(self):
        ### Get path to external toolbox
        toolbox = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Toolbox.tbx')
        ### execute external script tool
        pythonaddins.GPToolDialog(toolbox, 'tool')

        ### wait for result from gp tool
        while len(parameterList) == 0:
            time.sleep(0.2)

        #do something with items from parameterList

My tool (tool.py) code

import toolbar_addin

input1 = arcpy.GetParameterAsText(0)
input2 = arcpy.GetParameterAsText(1)

#do something to create output1 and output2...

toolbar_addin.parameterList = [output1, output2]

Now I get the following error:

Traceback (most recent call last):
File "C:\path\to\addin\toolbar_addin.py", line 229, in onClick
    time.sleep(0.2)
TypeError: GPToolDialog() takes at most 1 argument (2 given)

Any suggestions on how I can force my code to wait on the gp tool?

EDIT: I've modified the fuctioning of my button and script tool to resemble this post. The tool passes parameters to a global list variable "parameterList" in the toolbar.

3

The behavior of the GPToolDialog function is only documented as follows: Opens a geoprocessing tool dialog box.

The function asks the application to bring up a dialog window, but does not return any status/result object to you. It does not offer any way of directly determining when the tool finishes running or if it gets cancelled.

  • +1 for the answer, but I am really trying to determine if the tool can be run synchronously. – Barbarossa May 13 '14 at 16:30
  • I implemented that function. It does run synchronously up until 10.2.1 or so when I had to change it to run async to enable interactive selection in the dialogs for feature set inputs. The function was only designed to pop up a tool dialog, not give any feedback. How/when it runs is an implementation detail. – Jason Scheirer May 13 '14 at 16:44
  • Thanks again, I'm trying to see if the tool, not the dialog, can be run synchronously. – Barbarossa May 13 '14 at 16:47
  • You cannot force your code in an add-in event to wait on the tool as it is popped up from GPToolDialog. You cannot communicate with the result object like you can when you call the tool directly from Python. You will need to look into other control flow constructs to get your desired behavior. – Jason Scheirer May 13 '14 at 16:50
  • Understood, please see my edit. – Barbarossa May 13 '14 at 16:52
1

This answer is way late, but I have been having this exact same problem (using Arcpy 10.4.1) and came up with a workaround that maybe someone will find helpful.

Basically what I did is create a new tool (I'm calling it "new_tool") to serve as a wrapper for the actual tool I want to run. new_tool has the same parameters as the tool you actually want to call, and it calls the tool you want to run and then does whatever useful stuff with the output you want to.

So for your code you could look something like this:

parameterList = []
class button1(object):
    def__init__(self):
        self.enabled=True
        self.checked=False
    def onClick(self):
        ### Get path to external toolbox
        toolbox = os.path.join(os.path.dirname(os.path.abspath(__file__)), 
'Toolbox.tbx')
        ### execute external script tool
        pythonaddins.GPToolDialog(toolbox, 'new_tool')

And new_tool would look something like:

#Get however many parameters you need
param1 = arcpy.GetParameter(0)
param2 = arcpy.GetParameter(1)

#call the tool you actually want to use
tbx = arcpy.ImportToolbox(os.path.join(os.path.dirname(__file__), "Toolbox.tbx"))         
tbx.tool(param1,param2)

#Do whatever you wanted to do with the output here

Anyways, a few years late but hopefully this saves someone some time as it took me a hot minute to figure out this workaround!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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