Get all parameters of a node - python

   976   3   1
User Avatar
Member
385 posts
Joined: 7月 2018
Offline
It seems i can access the name and value of each parameter but not the label. How can i grab the labels instead of the names?

node = hou.node("../CONTROLLER")
# Get the parameters of the node
parameters = node.parms()
# Create an empty string to store parameter information
param_string = ""

# Iterate through the parameters and populate the string
for param in parameters:
    # Check if the parameter name contains the substring "folder"
    if "folder" in param.name().lower():
        continue  # Skip this parameter

    param_name = param.name()

    # Check if the parameter has the 'label' attribute
    if hasattr(param, 'label'):
        param_label = param.label()
    else:
        param_label = "Label not available"

    param_value = param.eval()

    # Add parameter information to the string
    param_string += f"Parameter Name: {param_name}\n"
    param_string += f"Parameter Label: {param_label}\n"
    param_string += f"Parameter Value: {param_value}\n"
    param_string += "------------------------\n"
    
return param_string
Edited by papsphilip - 2023年12月1日 12:58:48
User Avatar
Member
1908 posts
Joined: 11月 2006
Offline
You need to use hou.Parm.description() to get the label.
Graham Thompson, Technical Artist @ Rockstar Games
User Avatar
Member
385 posts
Joined: 7月 2018
Offline
graham
You need to use hou.Parm.description() to get the label.
thanks Graham!
I transferred all the code to a python module.
Is there a way to take all these values and dump them inside a string parameter without a button to trigger the function?

def getparms():

    hda = hou.pwd()
    mynode = hda.parm('mynode').evalAsString()
    node = hou.node(mynode)
    # Get the parameters of the reference node
    parameters = node.parms()
    # Create a dictionary to store parameter information
    param_data = {}
    # Iterate through the parameters and populate the dictionary
    for param in parameters:
        param_label = param.description()
        param_name = param.name()
        param_value = param.eval()
    
        # Get the type of the parameter
        param_type = str(param.parmTemplate().type()).split('.')[1]
        if param_type == 'FolderSet':
            continue
        if param_type == 'Menu':
            label = param.menuLabels()[param_value]
            index = param.menuItems()[param_value]
            param_value = label
    
        # Add parameter information to the dictionary
        param_data[param_label] = {
            'name': param_name,
            'value': param_value,
            'type': param_type
        }
    # data = json.dumps(param_data, indent=2)
    # hda.parm('getparameters').set(data)
    return param_data


right now i am returning the data and then in a string parameter i am calling the function like this
import json
data = hou.pwd().hm().getparms()
mydata = json.dumps(data, indent=2)
return mydata

is there better way?
User Avatar
Member
385 posts
Joined: 7月 2018
Offline
stumbled across another issue where node.parms() didn't quite read tuples correctly. so this was my solution if anyone is interested or knows a better way:

def getparms():

    hda = hou.pwd()
    mynode = hda.parm('mynode').evalAsString()
    node = hou.node(mynode)
    parms = node.parms()
    
    tmp = []  
    values = []
    names = []
    type = []
    labels = []
    indexes = []
    param_data = {}
    
    for parm in parms:
        # check if the parameter is inside a specific folder. This is in case i want the parms of the HDA itself
        # and want to exclude the interface for dealing with exporting json parameter files
        if 'Export Presets' in parm.containingFolders():
            continue
        # Skip parameters with expressions
        try:
            exprlang = parm.expressionLanguage()
            if exprlang:
                continue
        except hou.OperationFailed:
                exprlang = "None"
        
        t = str(parm.parmTemplate().type()).split('.')[1] 
        # Here is the part where i don't use parm.eval() to get the values but instead
        # get the everything as tuples and store all parameter names in a tmp array which we read later to get the values
        p = parm.tuple().name()
        n = parm.name()
        l = parm.description()
        # Skip Folder parameters - not folder contents
        if t == 'FolderSet':
                continue 
        # if its a Menu we need the index to read it back later
        if t == 'Menu':
            index = parm.menuItems()[parm.eval()]
        else: index = -1
            
        tmp.append(p)
        names.append(n)
        type.append(t)
        labels.append(l)
        indexes.append(index)
    
    # Getting the correct values ---- remove duplicates and flatten array
    tmp_values = [item for index, item in enumerate(tmp) if item not in tmp[:index]]
    for i in tmp_values:
        v = node.evalParmTuple(i)
        values.append(v)
    flat_values = [item for sublist in values for item in sublist]
    
    count = 0
    for name in names:
        if type[count] == 'Menu':
            index = indexes[count]
            
            # Add an extra key 'index' with the value from the variable index
            param_data[name] = {
            'label': labels[count],
            'value': flat_values[count],
            'type': type[count],
            'index': index
            }
        else:
            # Add parameter information to the dictionary without the 'index' key
            param_data[name] = {
            'label': labels[count],
            'value': flat_values[count],
            'type': type[count]
            }
        count += 1
    
    return param_data


next steps would be to also include other types of parameters like ramps and multiparms but also keyframes!
Also making it more general, for example i exclude expressions for the purposes of my project but ideally they should be stored and set as well.
Another useful feature would be to add a auto populating ordered menu to read all stored data so that we can choose more easily without opening windows browser etc.
Last but not least this code needs more error checking. currently i dont print anything to the console or inside some string.
Edited by papsphilip - 2024年3月8日 05:18:17
  • Quick Links