1

I would like to make a python script (.pyt) where i have 2 input parameters:

If the first parameter (which is a boolean) is set TRUE, therefore it is checked, the 2nd parameter should become required (if not, the 2nd parameter should be optional).

This is the used code so far:

def getParameterInfo(self):
    param0 = arcpy.Parameter(
    displayName="BooleanTest",
    name="in_features",
    datatype="Boolean",
    parameterType="Required",
    direction="Input")

    param1 = arcpy.Parameter(
    displayName="Test",
    name="testfield",
    datatype="Field",
    parameterType="Optional",
    direction="Input")

    params = [param0,param1]

    return params



def updateParameters(self):
    if self.params[0].value == "True":
        self.params[1].parameterType = "Required"
    else:
        self.params[1].parameterType = "Optional"

    return

what am I doing wrong? I'm new to Python.

3

The parameterType property is read-only; take a look at the Python toolbox help page here.

In other words, you cannot make a parameter required or optional on-the-fly; the closest you could get is to enable/disable the parameter with the .enabled property.

Other things: use GPBoolean instead of Boolean - refer to this page for data types; updateParameters() takes in params as an argument - yours doesn't; don't supply the self. before params inside the function. Look at the samples at Python toolbox Help.

Complete code:

def getParameterInfo(self):
    """Define parameter definitions"""
    param0 = arcpy.Parameter(
        displayName="BooleanTest",
        name="in_features",
        datatype="GPBoolean",
        parameterType="Required",
        direction="Input")

    param1 = arcpy.Parameter(
        displayName="Test",
        name="testfield",
        datatype="Field",
        parameterType="Optional",
        direction="Input",
        enabled=False)

    parameters = [param0,param1]

    return parameters

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."""
    if parameters[0].altered:
        if parameters[0].value == True:
            parameters[1].enabled = True
        else:
            parameters[1].enabled = False
        return

Alternatively, you can implement the solution you need by using the error messages, which provides exactly the same behavior as you need.

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if parameters[0].value == True:
        parameters[1].setErrorMessage("You must supply a value for this parameter")
    return

You don't need to use the updateParameters at all then.

Complete code:

def getParameterInfo(self):
    """Define parameter definitions"""
    param0 = arcpy.Parameter(
        displayName="BooleanTest",
        name="in_features",
        datatype="GPBoolean",
        parameterType="Optional",
        direction="Input")

    param1 = arcpy.Parameter(
        displayName="Test",
        name="testfield",
        datatype="Field",
        parameterType="Optional",
        direction="Input")

    parameters = [param0,param1]

    return parameters

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    if parameters[0].value == True:
        parameters[1].setErrorMessage("You must supply a value for the parameter X")
    return

If user has checked the check box, she won't be able to execute the tool unless she provides some value for the second parameter.

  • thank you, would this task be possible in a normal python script (not a python toolbox)? – Duddel Jan 25 '16 at 13:39
  • No, it's not possible to change the parameter type there either. But you can check whether the tool can be run by checking that the param1 is not emtpy when param0 is True. This is legal – Alex Tereshenkov Jan 25 '16 at 13:40
  • Alternatively, you can supply a default value for param1 which will be used whenever the param0 is checked. – Alex Tereshenkov Jan 25 '16 at 13:48
  • @Duddel, I've provided an alternative workaround which seems to do exactly what you need. – Alex Tereshenkov Jan 25 '16 at 13:54

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.