Search code examples
pythonrevit-apipyrevit

How to store Revit Add-in Settings with Python?


My Revit Add-in reads at some point a text file, that could be located anywhere. In my current implementation, the path to the text file is hardcoded. I'd like to avoid that, so that when I distribute the Add-in to other people, it doesn't simply crash.

Ideally I'd like to give them the ability of specifying their own location for that file on their computer, and that they don't need to re-specify it every time they re-launch the Add-in! In other words, I'd like to store once and for all this information. And if you close and re-open Revit, the location is still stored somewhere when you re-use the Addin.

This question is actually similar to this one, except that I'd need a solution when developing in Python (pyRevit). Any help?


Solution

  • if you're developing you addon in pyRevit, then you can use the pyrevit.script module to get the configuration for that script.

    Ask user for the file location (pyrevit.forms.save_file helps) and then save the file path in the script configuration. pyRevit handles this automatically and saves the information inside its master configuration file at %appdata%/pyRevit

    from pyrevit import script
    config = script.get_config()
    config.filelocation = 'path/to/your/file'
    script.save_config()
    

    And then later, read the configuration like this:

    from pyrevit import script
    config = script.get_config()
    print(config.filelocation)
    # or to get the config safely
    print(config.get_option('filelocation', None)