Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Tip: Python Addin - getting custom tools/toolbox to work - GPToolDialog

$
0
0
The “TypeError: GPToolDialog() takes at most 1 argument (2 given)” is a very misleading message and is a bug (NIM089253)
This process helped me get rid of this message and was a simple was to add my custom python tool/toolbox to an addin button. [Update/comment...actually I think the error message still shows in the Python window, but it seems to be harmless]

This post assumes you have already run the wizard so some buttons are stubbed out and you are modifying the addin_addin.py (or YourProjectName_addin.py if you renamed it). I'm assuming it will work with other classes, but I only tested it with toolbar-buttons.

This also let me take advantage of the python tools that I already had working in my toolbox. One of the advantages of this is that any dialog box you already have associated with your tool on start-up (i.e. in the tools parameter tab) will still be in place.

Some of this info is scattered around the forums (but the online help is lacking right now), so I thought it may help to consolidate what worked for me, in case it helps others. This is how I was able to get the GPToolDialog to work with my custom python tools/toolbox:
  • - Copy your .tbx file into the addin wizards “Install” folder.
  • - Create a “scripts” folder in the same location
  • - Copy all your .py script to this folder
  • - Open the .tbx in the Install folder, make sure “relative path” is checked, and repoint all the tools to the new script location
  • - the "import os" and the "relPath = " line seem to be the key....see sample code below

Code:

import os
import arcpy
import pythonaddins
relPath = os.path.dirname(__file__)

class ButtonClass1(object):
    """Implementation for YourProjectName_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        toolPath = relPath + r"\YourToolBox.tbx"
        pythonaddins.GPToolDialog(toolPath, "ToolNameFromToolbox")

If you have any custom or special python modules you want to package with the addin, that is modules that are usually stored in C:\Python27\ArcGIS10.1\Lib (which you may import in your tools) but aren’t part of the normal ArcGIS or Python install, if you place these in the scripts folder, that seems to work the best.

Remember, if you open the project up in the wizard again....which is sometimes necessary...and it tells you it is saving a backup _addin.py file, this backup contains the customization you did and the new file may be back to the defaults from the wizard. You may need to open both files and copy the custom code back into the new file.

This method seems to be the cleanest process I've found to do this, at least at version 10.1 (and the addin released April 2012).

Anyone else have tips?

Viewing all articles
Browse latest Browse all 2485

Trending Articles