Select to view content in your preferred language

Can't Set default boolean value in python toolbox

1173
2
01-31-2018 04:11 PM
ShaneFeirer
Occasional Contributor

I know that this had been asked before, But the simple examples have not worked.  I am including the pyt code in the message to help illustrate the issue.  If I use the line param6.value = True, the python toolbox will not load, but if I comment out that line it loads fine.  How should I be setting that boolean value on load to be checked.

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 = "Toolbox"
self.alias = ""

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


class Tool(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Vector Prep"
self.description = "This tool is a first test of a python toolbox and tool"
self.canRunInBackground = False

def getParameterInfo(self):
#Define parameter definitions

# First parameter
param0 = arcpy.Parameter(
displayName="Stream Flow Database",
name="FlowDatabase",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")

# Second parameter
param1 = arcpy.Parameter(
displayName="Model Name",
name="ModelName",
datatype="GPString",
parameterType="Required",
direction="Input")

# Third parameter
param2 = arcpy.Parameter(
displayName="Workspace",
name="Workspace",
datatype="DEWorkspace",
parameterType="Required",
direction="Input")

# Fourth parameter
param3 = arcpy.Parameter(
displayName="DEM",
name="DEM",
datatype="GPRasterLayer",
parameterType="Required",
direction="Input")

# Fifth parameter
param4 = arcpy.Parameter(
displayName="Regions",
name="Regions",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")

# Sixth parameter
param5 = arcpy.Parameter(
displayName="Precipitation",
name="Precipitation",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")

# Seventh parameter
param6 = arcpy.Parameter(
displayName="Include Stream Network",
name="SteamNetwork",
datatype="GPBoolean",
parameterType="Optional",
direction="Input",
category="Optional")

#param6.value = True

# Eigth parameter
param7 = arcpy.Parameter(
displayName="Acre Threshold",
name="AcreThreshhold",
datatype="GPDouble",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

# NinthSeventh parameter
param8 = arcpy.Parameter(
displayName="Scale Precip. by Gauge",
name="ScaleGuage",
datatype="GPBoolean",
parameterType="Optional",
direction="Input",
category="Optional")

# Tenth parameter
param9 = arcpy.Parameter(
displayName="Gauge Layer",
name="Gauges",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

# Eleventh parameter
param10 = arcpy.Parameter(
displayName="Run Reservoir Model",
name="ReservoirModel",
datatype="GPBoolean",
parameterType="Optional",
direction="Input",
category="Optional")

# Tweth parameter
param11 = arcpy.Parameter(
displayName="Reservoir Layer",
name="Reservoirs",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

# Thirteenth parameter
param12 = arcpy.Parameter(
displayName="Run Water Rights Model",
name="WRModel",
datatype="GPBoolean",
parameterType="Optional",
direction="Input",
category="Optional")

# Fourteenth parameter
param13 = arcpy.Parameter(
displayName="Water Rights Layer",
name="WaterRights",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

# Fifteenth parameter
param14 = arcpy.Parameter(
displayName="Run Demand Model",
name="Demand",
datatype="GPBoolean",
parameterType="Optional",
direction="Input",
category="Optional")

# Sixteenth parameter
param15 = arcpy.Parameter(
displayName="Structures Layer",
name="Structures",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

# Seventeenth parameter
param16 = arcpy.Parameter(
displayName="Vineyards Layer",
name="Vineyards",
datatype="GPFeatureLayer",
parameterType="Optional",
direction="Input",
category="Optional",
enabled="False")

params = [param0, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16]

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."""
return

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Shane, perhaps you could re-paste the code using Code Formatting....

it is a bit hard to follow without the indentation

0 Kudos
ShaneFeirer
Occasional Contributor

Re-paste with code formatting...

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 = "Toolbox"
        self.alias = ""

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


class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Vector Prep"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        # First parameter
        param0 = arcpy.Parameter(
            displayName = "Stream Flow Database",
            name = "FlowDatabase",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction = "Input")

        # Second parameter
        param1 = arcpy.Parameter(
            displayName = "Model Name",
            name = "ModelName",
            datatype = "GPString",
            parameterType = "Required",
            direction = "Input")

        # Third parameter
        param2 = arcpy.Parameter(
            displayName = "Workspace",
            name = "Workspace",
            datatype = "DEWorkspace",
            parameterType = "Required",
            direction = "Input")

        # Fourth parameter
        param3 = arcpy.Parameter(
            displayName = "DEM",
            name = "DEM",
            datatype = "GPRasterLayer",
            parameterType = "Required",
            direction = "Input")

        # Fifth parameter
        param4 = arcpy.Parameter(
            displayName = "Regions",
            name = "Regions",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input")

        # Sixth parameter
        param5 = arcpy.Parameter(
            displayName = "Precipitation",
            name = "Precipitation",
            datatype = "GPFeatureLayer",
            parameterType = "Required",
            direction = "Input")

        # Seventh parameter
        param6 = arcpy.Parameter(
            displayName = "Include Stream Network",
            name = "SteamNetwork",
            datatype = "GPBoolean",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional")

        param6.value = True

        # Eigth parameter
        param7 = arcpy.Parameter(
            displayName = "Acre Threshold",
            name = "AcreThreshhold",
            datatype = "GPDouble",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        # NinthSeventh parameter
        param8 = arcpy.Parameter(
            displayName = "Scale Precip. by Gauge",
            name = "ScaleGuage",
            datatype = "GPBoolean",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional")

        # Tenth parameter
        param9 = arcpy.Parameter(
            displayName = "Gauge Layer",
            name = "Gauges",
            datatype = "GPFeatureLayer",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        # Eleventh parameter
        param10 = arcpy.Parameter(
            displayName = "Run Reservoir Model",
            name = "ReservoirModel",
            datatype = "GPBoolean",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional")

        # Tweth parameter
        param11 = arcpy.Parameter(
            displayName = "Reservoir Layer",
            name = "Reservoirs",
            datatype = "GPFeatureLayer",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        # Thirteenth parameter
        param12 = arcpy.Parameter(
            displayName = "Run Water Rights Model",
            name = "WRModel",
            datatype = "GPBoolean",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional")

        # Fourteenth parameter
        param13 = arcpy.Parameter(
            displayName = "Water Rights Layer",
            name = "WaterRights",
            datatype = "GPFeatureLayer",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        # Fifteenth parameter
        param14 = arcpy.Parameter(
            displayName = "Run Demand Model",
            name = "Demand",
            datatype = "GPBoolean",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional")

        # Sixteenth parameter
        param15 = arcpy.Parameter(
            displayName = "Structures Layer",
            name = "Structures",
            datatype = "GPFeatureLayer",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        # Seventeenth parameter
        param16 = arcpy.Parameter(
            displayName = "Vineyards Layer",
            name = "Vineyards",
            datatype = "GPFeatureLayer",
            parameterType = "Optional",
            direction = "Input",
            category = "Optional",
            enabled = "False")

        params = [param0, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, param15, param16]

        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."""
        return
0 Kudos