Sample Code: Embedding gp tools or data inside of a python addin file

Discussion created by Morag.Hawkins on Oct 7, 2011
Latest reply on Oct 18, 2011 by Morag.Hawkins
Hi All

Not too sure if this will interest anyone, if it is supported etc, but it is possible to embed gp tools (.tbx or .pyt) as part of the addin itself, so that if you have a python addin you do not have to distribute the gp tools separately. This would be useful if you have a complex gp tool that cannot be achieved easily through an addin itself e.g. it has lots of parameters or a value table etc.

This would all apply to data if you wanted to distribute a fgdb or something inside of your addin, or an mxd, a lyr file etc etc.

1) Firstly, copy your .pyt or .tbx into the addins Install folder. If using a .tbx, make sure it is set to use relative paths.
2) In your addin py file, add

import os

relPath = os.path.dirname(__file__)

3) When the addin is created and installed, all the files you included sit relative to the calling script. So, you can use this relPath setting to go grab your extra stuff, e.g. to launch an embedded gp tool, use

pythonaddins.GPToolDialog(relPath+r'\MyPythonToolbox.pyt', 'MyPyTool')

Aside - this has solved an issue I had raised in another thread around the lack of complex parameter setting in python addins, I can now embed a python toolbox with all its parameter functionality as my 'Options dialog' in the addin, writing the changes back to an xml file when user clicks ok in the gp Options dialog. Addin then reads from this xml.