2

I have been attempting to make a Tool within a Toolbox with four different parameters. My code consists of the following:

def getParameterInfo(self):
    """Define parameter definitions"""

    # First parameter = layer with customized features
    source = arcpy.Parameter(
        displayName = "Source layer with customized features",
        name = "source",
        datatype = "Feature Layer",
        parameterType = "Required",
        direction = "Input")

    # Second parameter = layers to which customized features will be applied
    updlayers = arcpy.Parameter(
        displayName = "Layers with features to be updated",
        name = "updlayers",
        datatype = "GPValueTable",
        parameterType = "Required",
        direction = "Input")

    labelFontSize = arcpy.Parameter(
        displayName = "Label Font Size",
        name = "labelFontSize",
        datatype = "GPDouble",
        parameterType = "Required",
        direction = "Input")

    labelBoldFont = arcpy.Parameter(
        displayName = "Bold Font:",
        name = "labelBoldFont",
        datatype = "GPDouble",
        parameterType = "Required",
        direction = "Input")

    updlayers.columns = [['Feature Layer', 'Features']]

    params = [source,updlayers,labelFontSize,labelBoldFont]
    return params

From my understanding, the params list is the list that will later be included as one of the inputs into my execute function. A portion of my execute function is as follows:

def execute(self, parameters, messages):
for a in range(0,len(parameters)):
        arcpy.AddMessage("This is the item at index " + str(a) + " " + parameters[a].name)
    updlayers = parameters[1].values
    labelFontSize = parameters[2].value

Hence, params from getParameterInfo() will be parameters in the execute function. Is that correct? Either way, I am having trouble with my last parameter, labelBoldFont. I essentially want this to be a checkbox in my tool so that the user can choose to have their label text to be bold or not. However, the parameters list in my execute() function only has three elements and not four (labelBoldFont is not being included in the list???). I have done some thorough testing and I can't seem to find the issue with my code. Any ideas as to what the source of the problem would be?

  • I'm assuming that you are doing this as part of tool validation on a Python script tool rather than in a Python toolbox. If I have misunderstood would you be able to edit your question to clarify that detail, please? – PolyGeo Oct 1 '14 at 21:45
  • This is actually for a toolbox and not part of a tool validation, I am not familiar with what that is. What type of clarification are you looking for? – coolDude Oct 1 '14 at 22:11
  • 1
    I was trying to figure whether you were writing a tool class within a Python toolbox or a Python script tool in a standard toolbox. I think getParameterInfo is used by both. This is a link to information about Tool validation. – PolyGeo Oct 1 '14 at 22:23
  • I did not know there was a difference between a toolbox and a script. Is a toolbox just a collection of scripts then? Either way, yes I am working on a Tool within a Toolbox class. – coolDude Oct 1 '14 at 23:10
  • 1
    A standard toolbox can hold Python script tools (and other tools based on models or system tools e.g. Clip, etc) which only allow their parameters to be configured via a GUI. I think it is fair to say that this type of toolbox can include a collection of scripts. At 10.1 Python toolboxes were added to enable you to implement tools as classes so that the dialog is "generated" by the Python code. For these there can be multiple tools written in a single script (or called as modules from multiple scripts). – PolyGeo Oct 1 '14 at 23:17
1

So, one of the funny things I've noticed about Python toolboxes is that casting your parameters to custom variables in the getParameterInfo method often causes more problems than it solves. When I did this, I couldn't get any parameters to display. However, when I simply followed the examples shown by ESRI and cast them to variables param0, param1, param2...so on and so forth, it worked fine.

def getParameterInfo(self):
    """Define parameter definitions"""

    # First parameter = layer with customized features
    param0 = arcpy.Parameter(
        displayName = "Source layer with customized features",
        name = "source",
        datatype = "Feature Layer",
        parameterType = "Required",
        direction = "Input")

    # Second parameter = layers to which customized features will be applied
    param1 = arcpy.Parameter(
        displayName = "Layers with features to be updated",
        name = "updlayers",
        datatype = "GPValueTable",
        parameterType = "Required",
        direction = "Input")

    param2 = arcpy.Parameter(
        displayName = "Label Font Size",
        name = "labelFontSize",
        datatype = "GPDouble",
        parameterType = "Required",
        direction = "Input")

    param3 = arcpy.Parameter(
        displayName = "Bold Font:",
        name = "labelBoldFont",
        datatype = "GPDouble",
        parameterType = "Required",
        direction = "Input")

    param1.columns = [['Feature Layer', 'Features']]

    params = [param0, param1, param2, param3]
    return params

The fact is that you're going to have to cast them to custom variables again in the execute method.

def execute(self, parameters, messages):
    updlayers = parameters[1].values
    labelFontSize = parameters[2].value
    labelBoldFont = parameters[3].valueAsText

So really you're not doing anything but perhaps making the code a little more readable. Typically, I just use comments for that which you did for your first and second parameters but stopped for the third and fourth. I admit it though, would be cool to see that work and not have to declare them at execute.

Regarding getting your last parameter labelBoldFont to show as a checkbox, you need to change the dataType to GPBoolean.

param3 = arcpy.Parameter(
    displayName = "Bold Font:",
    name = "labelBoldFont",
    datatype = "GPBoolean",
    parameterType = "Required",
    direction = "Input")

Then just interrogate that parameter's variable (which is whatever you cast it as in execute) whenever it is appropriate in your workflow.

if labeledBoldFont:
    # Do all sorts of awesome crazy stuff
  • So I would have never thought that it would depend on how I actually instantiate my parameters... Either way, this seems to have resolved my issue. Thank you for your input on this! – coolDude Oct 6 '14 at 20:29
  • 1
    I agree. It's bananas. Just one of those lovely little arcpy nuances. I did exactly what you did a year ago and thought I was losing my mind. – MrBubbles Oct 6 '14 at 21:33
  • Hey, we were in the same boat! I was about to lose my mind as well. – coolDude Oct 7 '14 at 22:05

Your Answer

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

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