in pyt (Python Toolbox) why can't I make the parameter Optional?

4320
5
Jump to solution
09-30-2014 01:13 AM
MartinHvidberg
Occasional Contributor

Dear Community

When defining parameters in a .pyt (Python toolbox) I would like to make one of the parameters optional - I think.

It's a boolean parameter, allowing the user to specify weather the tool is allowed to overwrite certain values.

I have made it default to False, and that would be the correct setting in 9 of 10 cases, so I would prefer if the tool would be able to just go with this default value. Unfortunately, when run, the tool insist that I toggle the boolean, before it will remove the little green dot. If I run the tool with the default values untouched, it gives me an error-box saying: "ERROR 000735: Overwrite Existing OBJNAM and NOBJNM: Value is required"

I have tried to change the parameter from parameterType="Required", to parameterType="Optional", but it seems to have no effect on the tool behaviour.

I have decided to paste the entire code here, it's still 90% cut-and-paste from ArcGIS Help (10.2, 10.2.1, and 10.2.2) , ArcGIS Help (10.2, 10.2.1, and 10.2.2) , and others but I guess the error is in a combination of things, and not the isolated line.

I have marked the parameter (line 81), that I try to change to make a difference.

#-------------------------------------------------------------

# Name:       GNDBtoolbox.pyt

# Purpose:    A python-toolbox to work with the "Greenlandic Names Data Base" (GNDB) and NIS.

# Author:     Martin Hvidberg

# e-mail:     mahvi@gst.dk, Martin@Hvidberg.net

# Created:    2014-09-16

# Copyright:  CopyLeft

# ArcGIS ver: 10.2.0

# Python ver: 2.7.3

#-------------------------------------------------------------

import arcpy

class Toolbox(object):

   

    def __init__(self):

        """Define the toolbox (the name of the toolbox is the name of the .pyt file)."""

        self.label = "The_GNDB_Toolbox"

        self.alias = "Toolbox to work with GNDB and NIS"

        self.description = "This is the description of the toolbox..."

        # List of tool classes associated with this toolbox

        self.tools = [GNDBruninTOC]

       

class GNDBruninTOC(object):

    """

    GNDB update NIS - assuming feature classes all ready opened in TOC

    Created on 27 Sep 2011

    @author: mahvi@gst.dk / Martin@Hvidberg.net

   

    """

   

    def __init__(self):

        """Define the tool (tool name is the name of the class)."""  

       

        self.label = "GNDB_update_NIS_run_in_TOC"

        self.description = "This tool will update the selected NIS FC, which must be in the TOC"

        self.canRunInBackground = True # True = Obey "Background Processing setting" in the Geoprocessing Options dialog.

    def getParameterInfo(self):

        """Define parameter definitions"""

        # 0. The Feature layer to receive GNDB names

        param0 = arcpy.Parameter(

            displayName="Input Features",

            name="in_features",

            datatype="GPFeatureLayer",

            parameterType="Required",

            direction="Input")

   

        # 1. The point feature class holding the GNDB

        param1 = arcpy.Parameter(

            displayName="GNDB points",

            name="GNDB",

            datatype="GPFeatureLayer",

            parameterType="Required",

            direction="Input")   

        param1.value = "NamesP"

   

        # 2. The Mode

        param2 = arcpy.Parameter(

            displayName="The Mode",

            name="Mode",

            datatype="GPString",

            parameterType="Required",

            direction="Input")           

        param2.filter.type = "ValueList"

        param2.filter.list = ["Berit", "Test"]

        param2.value = param2.filter.list[0]

   

        # 3. Overwrite

        param3 = arcpy.Parameter(

            displayName="Overwrite Existing OBJNAM and NOBJNM",

            name="Overwrite",

            datatype="GPBoolean",

            parameterType="Optional",   # <------ This seems to have no effect ?

            direction="Input")   

        param3.value = "False"         

       

        params = [param0, param1, param2, param3]

   

        return params

    def isLicensed(self):

        """Set whether tool is licensed to execute."""

        return True

    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):

        """Modify the messages created by internal validation for each tool

        parameter.  This method is called after internal validation."""

        return

    def execute(self, parameters, messages):

        """The source code of the tool."""

        # put processing here ...

        return

0 Kudos
1 Solution

Accepted Solutions
TimothyHales
Esri Notable Contributor

Martin,

I created a new pyt with the code provided, and the parameter shows optional for me:

OptionalGNDB.jpg

Have you refreshed the toolbox since you updated the code? Could you try copying it into a new pyt?

Timothy

View solution in original post

0 Kudos
5 Replies
TomGeo
by
Occasional Contributor III

Hi Martin,

I checked some of my code in a Python toolbox and was wondering if you can solve the problem by simple omitting the line param3.value = "False". The standard behaviour as far as I can see is anyway to have the checkbox unchecked. When I force my optional parameters to be true by setting param3.value = "True" then it works without any problems.

Hope it helps!

Otherwise... give me a call 😉

Cheers, Thomas

- We are living in the 21st century.
GIS moved on and nobody needs a format consisting out of at least three files! No, nobody needs shapefiles, not even for the sake of an exchange format. Folks, use GeoPackage to exchange data with other GIS!
0 Kudos
MartinHvidberg
Occasional Contributor

Hi Thomas - nice to hear from you...

Unfortunately that line is the solution to an earlier problem Re: in a pyt (python toolbox): How do I make a boolean parameter default False??? so I would like to keep it.

/ Martin

0 Kudos
TimothyHales
Esri Notable Contributor

Martin,

I created a new pyt with the code provided, and the parameter shows optional for me:

OptionalGNDB.jpg

Have you refreshed the toolbox since you updated the code? Could you try copying it into a new pyt?

Timothy

0 Kudos
MartinHvidberg
Occasional Contributor

Thanks for your reply.

I restarted ArcMap, and now everything works exactely as expected 🙂 ???

/ Martin

MartinHvidberg
Occasional Contributor

for the record: I had used 'refresh toolbox' many times during the edits, so that was not the problem.

/ M

0 Kudos