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

2517
4
10-07-2011 02:39 AM
MoragHawkins
New Contributor II
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.
Tags (2)
4 Replies
ChrisFox3
Occasional Contributor III
Hi Morag,

What you have described below is definetly supported and acutally makes a lot of sense for your workflow. The other cool thing is in your script tool you could code in the validation code to pull values from the xml so that the users inputs persists between sessions allowing them to just change one value at a time if they wish. Nice post.
0 Kudos
KevinBell
Occasional Contributor III
I like the sound of this, but I'm not exactly clear where this goes:

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


---This should definitely be a presentation at the UC, and/or a white paper with a working sample!
0 Kudos
MoragHawkins
New Contributor II
Please find attached a quick and dirty example.

OptionsDialog.esriaddin - a sample addin

arcpyXmlHelper.zip - a zipfile containing a set of instructions and sample options toolbox.

I have tried to do a helper module for those not familiar with reading/writing xml, so you can for example call the following from within your python addin to read parameters.

getXmlParameterAsText(0)
or
getXmlParameterAsValue(0)
or 
getXmlParamterInfo()
or
getXmlAttributeAsText(0,'datatype')

Everything is detailed in the .zip file -> installation_instructions.txt
0 Kudos
MoragHawkins
New Contributor II
I like the sound of this, but I'm not exactly clear where this goes:

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


---This should definitely be a presentation at the UC, and/or a white paper with a working sample!


in the button or menus onclick function

def onClick(self):
        pythonaddins.GPToolDialog(relPath+r'\OptionsDialog.tbx', 'SaveOptions')
0 Kudos