Select to view content in your preferred language

Python Script Tool Validation errors

3843
2
01-04-2014 09:24 AM
MattGray
Deactivated User
I am currently creating a python script tool at work that works fine, and could be even more polished if I can get certain functionality working using the tool validation.  I am trying to get the tool to recognize what the the geometry type of the first input is, and based on that, set a parameter further down as a required parameter if its a "Polygon", or not required if it is a "Point".  Here is my code so far:

import arcpy

class ToolValidator(object):

  def __init__(self):
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
 
        [INDENT]return[/INDENT]

  def updateParameters(self):
    if self.params[0].value:
desc = arcpy.Describe(self.params[0].value)
feature_type = desc.shapeType
if feature_type == "Polygon":
  self.params[7].enabled = True
  self.params[7].parameterType = "Required"
                self.params[8].enabled = False
  self.params[8].parameterType = "Optional"
elif feature_type == "Point" or "Multipoint":
  self.params[7].enabled = False
  self.params[7].parameterType = "Optional"
  self.params[8].enabled = True
  self.params[8].parameterType = "Required"
 
       [INDENT]return[/INDENT]

  def updateMessages(self):

        [INDENT]return[/INDENT]


I have gotten it to recognize the geometry type of the first input once someone sets it. When its "Polygon", it will disable parameter[8], and when its "Point", it will disable parameter[7].  My problem is with with trying to toggle the parameterType property of a Parameter object, as suggested http://resources.arcgis.com/en/help/main/10.1/index.html#//018z00000063000000 here.  It says that the parameterType property is both read and write using tool validation.  For whatever reason, I cannot get it to work.  Ultimately, if the input geometry type is "Polygon", I would like parameter[7] set to parameterType "Required" and parameter[8] set to disabled.  If the input type is "Point", I would like parameter[8] set to parameterType "Required" and parameter[7] set to disabled. Any help is appreciated.
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Matt,

The parameterType parameter is actually 'Read-only'.  Take a look at the following link:

http://resources.arcgis.com/en/help/main/10.1/index.html#//00150000000v000000

What you can do is set both parameter 7 & 8 to 'Optional' under the 'Parameters' tab.  That way when one of the parameters is disabled, the tool will not error upon execution.
0 Kudos
MattGray
Deactivated User
I noticed on another link in the ArcGIS help sections that it states parameterType is read-only.  On the one I provided, it states that it is both read and write.  However, I managed to figure out my own solution to my problem. The reason I didn't go with setting both of them to optional, was that when someone picks a "Polygon" type, the field I need to be enabled and required, still says "optional" by the parameter where it is entered. This is a tool that might eventually be given to clients, so this could lead to alot of confusion.  I found the solution in using the .clearmessages() method and placing the logic to them under the updateMessages(self) function . My code looks like this:

import arcpy

class ToolValidator(object):

  def __init__(self):
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    return

  def updateParameters(self):
    if self.params[0].value:
desc = arcpy.Describe(self.params[0].value)
feature_type = desc.shapeType
if feature_type == "Polygon":
  self.params[7].enabled = True
  if self.params[7].value == "All":
   self.params[8].enabled = True
  
       else:
       self.params[8].enabled = False
 
else:
  self.params[7].enabled = False
  self.params[8].enabled = True
 
 
    return

  def updateMessages(self):
    if self.params[0].value:
desc = arcpy.Describe(self.params[0].value)
feature_type = desc.shapeType
if feature_type == "Polygon":
  if self.params[7].value == "All":
   pass
       else:
   self.params[8].clearMessage()
else:
  self.params[7].clearMessage()
    return

This got the tool working the way I wanted it based on what the geometry type of the first inputs.  Also if parameterType is read-only, it would be nice if someone at ESRI would correct that for the link I provided in the first post.
0 Kudos