Rhino.GetSettings alternative in RhinoPython?

Hi,
Could anyone help me with something?

What is the alternative of Rhino.GetSettings() in Python?

And Rhino6, in case it is old thing?

Thanks in advance.

Seems to be in rhinoscriptsyntax for V6 anyway…

https://developer.rhino3d.com/api/RhinoScriptSyntax/#collapse-GetSettings

Code:

def GetSettings(filename, section=None, entry=None):
    """Returns string from a specified section in a initialization file.
    Parameters:
      filename (str): name of the initialization file
      section (str, optional): section containing the entry
      entry (str, optional): entry whose associated string is to be returned
    Returns:
      list(str, ...): If section is not specified, a list containing all section names
      list:(str, ...): If entry is not specified, a list containing all entry names for a given section
      str: If section and entry are specified, a value for entry
      None: if not successful
    Example:
      import rhinoscriptsyntax as rs
      filename = rs.OpenFileName("Open", "Initialization Files (*.ini)|*.ini||")
      if filename:
          sections = rs.GetSettings(filename)
          if sections:
              section = rs.ListBox(sections, "Select a section", filename)
              if section:
                  entries = rs.GetSettings(filename, section)
                  if entries:
                      entry = rs.ListBox(entries, "Select an entry", section)
                      if entry:
                          value = rs.GetSettings(filename, section, entry)
                          if value: rs.MessageBox( value, 0, entry )
    See Also:
      
    """
    import ConfigParser
    try:
        cp = ConfigParser.ConfigParser()
        cp.read(filename)
        if not section: return cp.sections()
        section = string.lower(section)
        if not entry: return cp.options(section)
        entry = string.lower(entry)
        return cp.get(section, entry)
    except IOError:
        return scriptcontext.errorhander()
    return scriptcontext.errorhandler()
1 Like

Hi @Helvetosaur,

Do you happen to also know the Rhino.SaveSettings?

I don’t see this one in rhinoscriptsyntax.

Thanks in advance.

update:
Anyways maybe a suggestion to add it.
Something like:

def SetSettings(filename, section=None, entry=None, value=None):
    import ConfigParser
    try:
        cp = ConfigParser.ConfigParser()
        cp.read(filename)
        
        if not section: return
        if not entry:return
        cp[section][entry] = value
        with open(filename, 'w') as ini_file:
            cp.write(ini_file)
    except IOError:
        return scriptcontext.errorhandler()
    return scriptcontext.errorhandler()