Custom Toolbox (.tbx, not .pyt)

1943
8
Jump to solution
03-29-2018 10:23 AM
ChrissyWhite
New Contributor III

I have to create a custom toolbox in ArcMap and have a .py tool in it. I have my .py script already written and functional (without input parameters). I knew I had to write a toolbox with this script, found the python toolbox and went to town. Got it all written (with input parameters) and it works fine. However, I just found out that I have to write a custom toolbox (.tbx), rather than a python toolbox (.pyt), but I cannot find any documentation on how to do so other than via the ArcToolbox in ArcMap (steps show gui, not python). I have to do this in python, but I cannot figure it out. I took the toolbox code from my .pyt code and made a new file on my computer with the .tbx extension, refreshed my toolbox in ArcMap and it saw it without any issue, but the new toolbox is not usable. I can't add my script to it (all those options are grayed out). I'm thinking that the code I pulled from my .pyt toolbox is not the same code I need in my .tbx toolbox. What needs to go in there? I went ahead and kept the def execute section in my new .tbx, but not really sure what needs to go in there as my actual code needs to be in my .py file unlike in my .pyt toolbox.

I also tried to just create a toolbox (.tbx) manually (gui) through ArcToolBox and then check its script (like I could for the python toolboxes) to see if I could figure out where I was going wrong in mine and fix it, but no luck, it's all in binary or other such gibberish (proprietary? Sorry ESRI if so, I wasn't trying to steal anything, just figure out what I was doing wrong...)

This is what I copied from my .pyt file and put in the .tbx:

############################################

class Toolbox(object):
   def __init__(self):
   self.label = "PPPtest"
   self.alias = "PPPtest"

   self.tools = [CFTool]

class CFTool(object):

   def __init__(self):
      self.label = "CFTool"
      self.description = "DOES STUFF - fix later"
      self.canRunInBackground = True

   

   def getParameterInfo(self):
      #Get Parameters
      #InputRaster - Top
      param0 = arcpy.Parameter(displayName = "Input Raster - TOP",
         name = "input_raster_top",
         datatype ="GPRasterLayer",
         parameterType = "Required",
         direction = "Input")

   

   #InputRasters - Bottoms
      param1 = arcpy.Parameter(displayName = "Input Rasters - BOTTOM",
         name = "input_raster_bottom",
         datatype ="GPRasterLayer",
         parameterType = "Required",
         direction = "Input",
         multiValue=True)


      params = [param0, param1]

      return params

def isLicensed(self):
"""Set whether tool is licensed to execute."""
   return True

#######No idea what to put in here ###########
## def updateParameters(self, parameters):
## """Modify the values and properties of parameters before internal
## validation is performed. This method is called whenever a parameter
## has been changed."""
## return
##

def updateMessages(self, parameters):
   topRaster = parameters[0]
   botRaster = parameters[1]
   return

####No idea what to put in here now......######

def execute(self,parameters, messages):

   return

############################################

I'm not asking anyone to write my code, really just what is supposed to be in the .tbx file and how to make it work as a normal toolbox so I can add my script to it and get everything run like it should. Thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Creating the tbx can only be done via the ArcGIS GUI. It is a binary format. Adding python scripts to the tbx can also only be done via the ArcGIS GUI.

View solution in original post

8 Replies
RebeccaStrauch__GISP
MVP Esteemed Contributor

Another option is to make a python addin using the wizard, although this will not work with ArcPro, so maybe you need to go the route you are going.  just some tipes I have (the name is a bit off, but I do have tips Tip: Python Addins - getting custom tools/toolbox to work - GPToolDialog error and other tips (9/7/2...

and there is anothr recent thread about addin deployment https://community.esri.com/message/757778-python-add-in-deployment 

fwiw

0 Kudos
ChrissyWhite
New Contributor III

I appreciate the response. I've read through both of those, but I'm not sure I follow what I need to change or include in my .tbx file.

0 Kudos
RebeccaStrauch__GISP
MVP Esteemed Contributor

My suggestion was going a different route, and not having a Python Toolbox but a Python addin using a regular custom Toolbox.  That is, create a new toolbox, then create your tool using the script.  Then setup the parameters.  Then use the wizard to create you addin and toolbar with buttons.  The toolbar will not work in Pro however...but the toolbox would (assuming the syntax works in the python 3.x).

So, mine wasn't an actual answer to your issue (I haven't created any Python Toolboxes), but an alternative.  Sorry, I didn't want to confuse the issue, just offer an option.

0 Kudos
RandyBurton
MVP Regular Contributor

There is some help documentation here: A quick tour of creating tools with Python (although it is not exactly a tutorial).   It does offer some explanation  of the differences between a script tool and a Python toolbox.

RebeccaStrauch__GISP
MVP Esteemed Contributor

to add to that, you may want to check out some of the tech session videos available

Esri Events - YouTube 

You may have to look thru the list to find one that seems appropriate for the python toolboxes

RandyBurton
MVP Regular Contributor

Here is a good tutorial from Arc User magazine: Create a Python Tool That Summarizes ArcMap Layer Properties.  There are links at the top of the article so you can save the article as a PDF and download the associated dataset and resources.  This article will walk you through the steps of adding a Python script to a standard toolbox (.tbx).

When Comparing custom and Python toolboxes (this link is a branch from the documentation in my previous comment), the scripts are quite different.  Examine the script that comes with the Arc User article to see the differences.  The script gets added to the toolbox using a "Wizard" tool.

Your script might start something like this:

#Import modules...
import arcpy

#User input variables...
topRaster = arcpy.GetParameterAsText(0)
bottomRaster = arcpy.GetParameterAsText(1)
# other input parameters ??

# code to do something with the rasters

# Return or output something
arcpy.SetParameter(2, SomeResult)‍‍‍‍‍‍‍‍‍‍

Can you describe what you would like your tool to do?

0 Kudos
Luke_Pinner
MVP Regular Contributor

Creating the tbx can only be done via the ArcGIS GUI. It is a binary format. Adding python scripts to the tbx can also only be done via the ArcGIS GUI.

ChrissyWhite
New Contributor III

That would explain the lack of documentation on how to do it any other way.  Thanks a ton. 

0 Kudos