Reuse Your Code with lib

"Good Programmers Code, But Great Programmers Reuse Code." Let's learn how to be a great programmer with pyRevit.

Reuse Your Code with lib

"Good Programmers Code, But Great Programmers Reuse Code." Let's learn how to be a great programmer with pyRevit.

Summary

Reuse Your Code as early as possible

Whenever you catch yourself copy-pasting a large code snippets, consider making it a function and adding it to your library of reusable snippets. That's a really good habit for any programmer and you will benefit the most in the long run.

There are plenty of reason for that:

  • Better organized code

  • Cleaner code in scripts

  • It's easier to update function in 1 place than search for all placed

  • Quicker to develop tools

Let's look at how to create your custom library with pyRevit.

Create lib folder

Creating library of snippets is also very easy. We need to create a lib folder inside of our .extension folder and pyRevit will find it automatically.

Inside lib and all other nested folders we will have to create __init__.py file. This indicates to python that this folder should be treated like a package. This will allow us to easily import different modules and functions and classes out of them.

Let's create a few files and a folder inside of our lib folder like this:

💡You don't need to write anything inside __init__.py files.

Now we can place functions that we would want to reuse. Obviously you can place any function you want, but in the video I used the following examples

example.py
def test_print():  
"""Placeholder function to print text in the console."""   
    print('test_print() was imported from example.py')
Snippets/_selection.py
# -*- coding: utf-8 -*-

# Imports
from Autodesk.Revit.DB import *

# Variables
uidoc = __revit__.ActiveUIDocument
doc   = __revit__.ActiveUIDocument.Document

# Functions
def get_selected_elements():
    """Function to get selected elements in Revit UI."""
    return [doc.GetElement(e_id) for e_id in uidoc.Selection.GetElementIds()]

How to Import custom functions?

Importing functions is not complicated either. As I mentioned before, pyRevit will know about lib folder right away. So we just need to reference the right folders and files from lib folder.

👇 In our case to import them in our .pushbutton, we will use the following code.

from example import test_print
from Snippets._selection import get_selected_elements

# Test Print
test_print()

# Get selected Elements
selected_elements = get_selected_elements()
print(selected_elements)
Add custom lib Autocomplete

💡 As you remember we can add references in our virtual environment in pyCharm. We can do that for our custom lib as well so it gives us autocomplete and also provides quick access to doc strings.

🎦 You can see how it's done in the video.


Tips & Tricks

💡 Reload pyRevit after lib changes!

That's something I struggled a lot in the beginning.

If you going to run the tool which uses a function from a library, it will read lib once and remember it. And if you modify the used function in lib and try to run the tool again, you will notice that there is no difference to how it worked before.
Good luck debugging that…

Every time we make any changes in lib, we need to reload pyRevit. This way you will know that your latest lib version is being used by pyRevit. Don't repeat my mistakes.

💡 Add uidoc as one of the arguments if you need doc or uidoc inside the function.

As I've mentioned pyRevit lib functions are remembered by pyRevit. And so do variables such as doc, uidoc and others if you define them in lib file…

If your function need to use doc or uidoc variable, make sure you make it as an argument.

Otherwise, if you going to use your tool in Project A, it will remember doc as Project A. But then if you jump to project B and use it as well, it will still use doc as Project A, because it was declared in lib folder file.

💡 I use uidoc as argument because we can get doc with this snippet:

def foo(uidoc):
    doc = uidoc.Document

HomeWork

Remember:
💪 Good programmers code, but Great programmers reuse code!

✅ Create lib folder
✅ Write simple functions in lib folder
✅ Import your custom functions and test them in the script

⌨️ Happy Coding!

Questions:

What should I write in __init__.py file?

What should I write in __init__.py file?

Should I always add uidoc as argument in lib functions?

Should I always add uidoc as argument in lib functions?

Can I name folder 'Snippets' differently?

Can I name folder 'Snippets' differently?