4

I have created a Python toolbox for work that has two tools inside. These tools use the toolbox template and so have a init, getParameterInfo, etc.

I want to be able to run one tool both standalone and call it inside other tools within that toolbox. I can't seem to get the parameters correct though.

class foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        print parameter[0]
        return

class bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        foo("hello, world)
        return

I have tried adding a parameter to the init(self, parameter) or the foo class but I can't get it to work.

I am new to Object-Oriented Programming (OOP) and ArcGIS in general.

  • You have mentioned ArcPy and ArcGIS but your code seems to be pure Python. Can you edit your question to make clear whether you are trying to write a Python toolbox or Python script tools in a standard toolbox, please? – PolyGeo Mar 13 '15 at 20:20
  • I corrected the question. Hopefully its more clear. Though it is mostly a python question, I can't figure out how to make the class take both parameters given from a UI and passed programmatically when used in a different script. Thanks! – user48920 Mar 14 '15 at 18:53
  • 1
    This link is for Python script tools in standard toolboxes and does not mention Python toolboxes but it may give you some ideas: blogs.esri.com/esri/arcgis/2011/08/04/pythontemplate I do not know if what you are trying to do is possible. – PolyGeo Mar 15 '15 at 1:34
  • 1
    I have a feeling what you're trying to do won't work, but can't confirm right now. One way that should work was if you import the tool into itself using arcpy.ImportToolbox() with a path to the PYT itself, then call arcpy.foo() – KHibma Mar 15 '15 at 1:58
5

The simplest option is to have your execute method call a function instead of doing the actual processing. This makes it easily callable by any tool.

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        somefunc(parameters[0].value, parameters[1].value)

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        somefunc(parameters[0].value, parameters[1].value)
        anotherfunc(parameters[2].value, parameters[3].value)
        return

def somefunc(arg1, arg2):
    #do something
    return

def anotherfunc(arg1, arg2):
    #do something else
    return

If you want those functions contained in the tool classes:

class Foo(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages)
        self.somefunc(parameters[0].value, parameters[1].value)
    def somefunc(self, arg1, arg2):
        #do something
        return

class Bar(object)
    def __init__(self)
        #stuff
    def getParameterInfo(self):
        # list parameters for UI tool
    def execute(self, parameters, messages):
        foo = Foo()
        foo.somefunc(parameters[0].value, parameters[1].value)
        self.anotherfunc(parameters[2].value, parameters[3].value)
        return
    def anotherfunc(self, arg1, arg2):
        #do something else
        return
| improve this answer | |
  • Thanks! This is what I was looking for. I thought I may have to create a module and import it but this supports my needs better. – user48920 Mar 18 '15 at 11:49
  • this was very helpful and easy to implement in cases where I collected all parameters in the very first lines of execute(). The rest of that method could then just be copy-pasted into something like run(). Awesome. – Christoph Nov 2 '16 at 13:49

Your Answer

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