0

I have an Python Toolbox script that contains two classes/tools. I want it so that the user inputs the data into the first class and then the second class will handle the processing.

How can I write the code so that the list of parameters within one of the functions of the first class will be referenced in the execute function of the second class?

class Toolbox(object):
    def __init__(self):

       self.label = "Toolbox"
       self.alias = "ClearRoadsScript"

       # List of tool classes associated with this toolbox
       self.tools = [ClearRoadsInputTool,DataProcessing]

class ClearRoadsInputTool(object):
   def __init__(self):

       self.label = "Input Data Layers"
       self.description = ""
       self.canRunInBackground = False


   def getParameterInfo(self):

      inputWorkspace = arcpy.Parameter(

         displayName = "Input Workspace",
         name = "Workspace",
         datatype = "DEWorkspace",
         parameterType = "Required",
         direction ="Input")

      parameters = [inputWorkspace]

      return parameters

   def execute(self, parameters, messages):
    
      return

class DataProcessing(object):
   def __init__(self):
    
       self.label = "Data Processing"
       self.description = ""
       self.canRunInBackground = False

   def getParameterInfo(self):

       inputPlowRoutes = arcpy.Parameter(

           displayName = "Input Plow Routes",
           name = "Plow Routes",
           datatype = "GPFeatureLayer",
           parameterType = "Required",
           direction = "Input")
    
       params = [inputPlowRoutes]
       return params

   def execute(self, parameters, messages):

      arcpy.env.overwriteOutput = True

      arcpy.CheckOutExtension("spatial")

      Plow_Routes = parameters[0].valueAsText

      InputLayers = ClearRoadsInputTool.getParameterInfo.parameters

      messages.addMessage(Plow_Routes + InputLayers)
3
  • That's not how GP tools work. You need to collect all parameters in one tool. You can then call another tool, function or class and pass it parameters to do the processing.
    – user2856
    Apr 1, 2022 at 22:43
  • I am confused. That is what I am asking to do, how would I call the parameters set in the first tool in the second tool to do the processing?
    – Mdebella
    Apr 7, 2022 at 14:57

1 Answer 1

1

It looks like you are trying to run DataProcessing and get parameter values from ClearRoadsInputTool.

That won't work. You can't get parameters from another tool.

You can only collect parameters in one tool and pass them to a second tool. The user will never see the second tool though, so you will need to collect all parameter values in the first tool.

So in your case, you would run ClearRoadsInputTool to collect the parameters and then pass those parameter values to DataProcessing.

The arcpy way of doing this is to import the toolbox containing the tool you want to call.

E.g.

import os
import arcpy

class Toolbox(object):
    def __init__(self):

       self.label = "Toolbox"
       self.alias = "ClearRoadsScript"

       # List of tool classes associated with this toolbox
       self.tools = [ClearRoadsInputTool, DataProcessing]


class ClearRoadsInputTool(object):
    def __init__(self):

        self.label = "Input Data Layers"
        self.description = ""
        self.canRunInBackground = False


    def getParameterInfo(self):

        inputPlowRoutes = arcpy.Parameter(
            displayName = "Input Plow Routes",
            name = "Plow Routes",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input")
    
        inputWorkspace = arcpy.Parameter(
            displayName = "Input Workspace",
            name = "Workspace",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction ="Input")

        parameters = [inputPlowRoutes, inputWorkspace]
        return parameters

    def execute(self, parameters, messages):
        plowroutes = parameters[0].value
        workspace = parameters[1].valueAsText
        with arcpy.EnvManager(workspace=workspace):
            fcs = [os.path.join(workspace, fc) for fc in arcpy.ListFeatureClasses()]

        arcpy.ImportToolbox(__file__)
        arcpy.DataProcessing_ClearRoadsScript(plowroutes, fcs)

class DataProcessing(object):
    def __init__(self):
    
        self.label = "Data Processing"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):

        inputPlowRoutes = arcpy.Parameter(

            displayName = "Input Plow Routes",
            name = "plow_routes",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input")
    
        inputLayers = arcpy.Parameter(

            displayName = "Input Layers",
            name = "input_layers",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input",
            multiValue=True)
    
        params = [inputPlowRoutes, inputLayers]
        return params
 

    def execute(self, parameters, messages):

        arcpy.env.overwriteOutput = True
        arcpy.CheckOutExtension("spatial")

        Plow_Routes = parameters[0].value
        InputLayers =  parameters[1].values

        messages.addMessage(Plow_Routes)
        messages.addMessage(InputLayers)

However, I think that's not the best way to do this as users will have to know to run the ClearRoadsInputTool not the DataProcessing tool.

It would be better to have a DataProcessing function which users never see.

import os
import arcpy

class Toolbox(object):
    def __init__(self):

       self.label = "Toolbox"
       self.alias = "ClearRoadsScript"

       # List of tool classes associated with this toolbox
       self.tools = [ClearRoadsInputTool]


class ClearRoadsInputTool(object):
    def __init__(self):

        self.label = "Input Data Layers"
        self.description = ""
        self.canRunInBackground = False


    def getParameterInfo(self):

        inputPlowRoutes = arcpy.Parameter(
            displayName = "Input Plow Routes",
            name = "Plow Routes",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input")
    
        inputWorkspace = arcpy.Parameter(
            displayName = "Input Workspace",
            name = "Workspace",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction ="Input")

        parameters = [inputPlowRoutes, inputWorkspace]
        return parameters

    def execute(self, parameters, messages):
        plowroutes = parameters[0].value
        workspace = parameters[1].valueAsText
        with arcpy.EnvManager(workspace=workspace):
            fcs = [os.path.join(workspace, fc) for fc in arcpy.ListFeatureClasses()]

        DataProcessing(plowroutes, fcs)


def DataProcessing(inputPlowRoutes, inputLayers):

    arcpy.env.overwriteOutput = True
    arcpy.CheckOutExtension("spatial")

    arcpy.AddMessage(inputPlowRoutes)
    arcpy.AddMessage(inputLayers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.