Save All Revit Families to Folder


# 🛠 Imports
import os
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI.Selection import *

# 🔧 Variables
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
rvt_year = 'RVT_{}'.format(int(app.VersionNumber))

# 🗂 Paths
PATH_SCRIPT = os.path.dirname(__file__)
PATH_FAMILY = os.path.join(PATH_SCRIPT,
                           'Families',
                           rvt_year,
                           doc.Title)

# 1️⃣ Get All Loadable Families
all_families = FilteredElementCollector(doc)\
    .OfClass(Family)\
    .ToElements()
loaded_families = [f for f in all_families if f.IsEditable]

# 🔍 Iterate through Families
for f in loaded_families:
    if f.IsEditable:

        # 2️⃣ Create Filepath
        cat = f.FamilyCategory.Name
        name = f.Name
        f_dir = os.path.join(PATH_FAMILY, cat)
        f_filepath = os.path.join(f_dir, name) + '.rfa'

        # 3️⃣ Create Dirs (if necessary)
        if not os.path.exists(f_dir):
            os.makedirs(f_dir)

        # 4️⃣ Open and SaveAs Family
        try:
            f_doc = doc.EditFamily(f)
            if f_doc and f_doc.IsFamilyDocument:
                if not os.path.exists(f_filepath):
                    f_doc.SaveAs(f_filepath)
        finally:
            f_doc.Close(False)

# 5️⃣ Open Folder with Saved Families
os.startfile(PATH_FAMILY)

#TODO: 🚫SUPPRESS WARNINGS🚫

⌨️ Happy Coding!
Erik Frits