Select to view content in your preferred language

Python Code for Setting Obtained from Tool property

3350
6
06-11-2014 10:45 AM
SuzanneRoulston-Doty
Emerging Contributor
Is is possible to set the Obtained from property for a parameter in Validation using python code?

I have populated a string parameter with a list of tables selected in Table View parameters.  If the user selects a table name in the string parameter I would like to populate a Field data type parameter with the list of fields for the selected table.

def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
      
    if self.params[0].altered:
      tblList = [""]   
      desc = arcpy.Describe(self.params[0].value)
      tblList.append(desc.name)
      if self.params[1].value:
        desc = arcpy.Describe(self.params[1].value)
        tblList.append(desc.name)
      if self.params[2].value:
        desc = arcpy.Describe(self.params[2].value)
        tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList
    if self.params[1].altered:
      tblList = [""]
      if self.params[0].value:
        desc = arcpy.Describe(self.params[0].value)
        tblList.append(desc.name)
      desc = arcpy.Describe(self.params[1].value)
      tblList.append(desc.name)
      if self.params[2].value:
        desc = arcpy.Describe(self.params[2].value)
        tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList
    if self.params[2].altered:
      tblList = [""]
      if self.params[0].value:
        desc = arcpy.Describe(self.params[0].value)
        tblList.append(desc.name)
      if self.params[1].value:
        desc = arcpy.Describe(self.params[1].value)
       tblList.append(desc.name)
      desc = arcpy.Describe(self.params[2].value)
      tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList

    if self.params[6].altered:
      At this point I would like to populate self.params[7] with a list of fields corresponding to the table selected in
      self.params[6]

     self.params[7] is currently a Field data type
Tags (2)
0 Kudos
6 Replies
AdamCox1
Deactivated User
Hi Suzanne, it would be good if you could repost with your code formatted as code.  When you're posting, click the pound sign above the text box to get the tags for code, and then format your code inside of those tags.
0 Kudos
SuzanneRoulston-Doty
Emerging Contributor
Hi Adam,  thank you for looking at my question.  I hope this is helpful.

def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
       
    if self.params[0].altered:
      tblList = [" "]    
      desc = arcpy.Describe(self.params[0].value)
      tblList.append(desc.name)
      if self.params[1].value:
        desc = arcpy.Describe(self.params[1].value)
        tblList.append(desc.name)
      if self.params[2].value:
        desc = arcpy.Describe(self.params[2].value)
        tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList
    if self.params[1].altered:
      tblList = [" "]
      if self.params[0].value:
        desc = arcpy.Describe(self.params[0].value)
        tblList.append(desc.name)
      desc = arcpy.Describe(self.params[1].value)
      tblList.append(desc.name)
      if self.params[2].value:
        desc = arcpy.Describe(self.params[2].value)
        tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList
    if self.params[2].altered:
      tblList = [" "]
      if self.params[0].value:
        desc = arcpy.Describe(self.params[0].value)
        tblList.append(desc.name)
      if self.params[1].value:
        desc = arcpy.Describe(self.params[1].value)
       tblList.append(desc.name)
      desc = arcpy.Describe(self.params[2].value)
      tblList.append(desc.name)
      self.params[6].filter.list = tblList
      self.params[8].filter.list = tblList
      self.params[10].filter.list = tblList

    if self.params[6].altered:
      "this is the point where I was wondering if I can use the Obtained from property to populate
      "self.params[6] with the fields for the table name selected in self.params[6]


    return
0 Kudos
Luke_Pinner
MVP Regular Contributor
Do you mean populate a filter list with the field names from the data in params[6]?
Something like:
fldList = [f.name for f in arcpy.ListFields(params[6].value)]
0 Kudos
SuzanneRoulston-Doty
Emerging Contributor
Hi Luke,

Using a string data type parameter I have been able to populate a filter list with field names (though your code was more succinct than mine so thank you for the sample code).

I was wondering if I was using a field data type parameter if there was way a to set the value for the 'Obtained from' property, for the field data type parameter, using python code or if that property can only be set manually in the tool's properties dialog box.  The tool I am working lets the user select one of three tables in a value list and based on the selection I want to display a list of the fields for the selected table name.
0 Kudos
Luke_Pinner
MVP Regular Contributor
Oh I get you. Use the parameterDependencies attribute.

    def getParameterInfo(self):
        '''parameter definitions for GUI'''
        params =[]
        params.append(arcpy.Parameter(
            displayName='GPFeatureLayer1',
            name='GPFeatureLayer1',
            datatype='GPFeatureLayer',
            parameterType="Required",
            direction="Input"))
        params.append(arcpy.Parameter(
            displayName='Field1',
            name='Field1',
            datatype='Field',
            parameterType="Required",
            direction="Input"))

        #this will automatically update the fields list with fields from param[0]
        params[1].parameterDependencies = [params[0].name] 
SuzanneRoulston-Doty
Emerging Contributor
Thank you everyone for your responses.
0 Kudos