from Autodesk.Revit.DB import *
import clr
clr.AddReference('System')
from System.Collections.Generic import List
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
all_materials = FilteredElementCollector(doc).OfClass(Material).ToElements()
mat_A = all_materials[0]
mat_B = all_materials[1]
excl_families = ['Curtain Wall', 'Stacked Wall', 'Sloped Glazing']
all_walls = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls).WhereElementIsElementType().ToElements()
all_floors = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Floors).WhereElementIsElementType().ToElements()
all_roofs = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Roofs).WhereElementIsElementType().ToElements()
all_types = list(all_walls) + list(all_floors) + list(all_roofs)
all_types = [t for t in all_types if t.FamilyName not in excl_families]
all_types = [t for t in all_types if type(t) != FamilySymbol]
t = Transaction(doc, __title__)
t.Start()
count = 0
for typ in all_types:
structure = typ.GetCompoundStructure()
layers = structure.GetLayers()
for layer in layers:
mat_id = layer.MaterialId
if mat_id == ElementId(-1):
continue
mat = doc.GetElement(mat_id)
mat_name = mat.Name
inx = layer.LayerId
structure.SetMaterialId(inx, mat_B.Id)
typ.SetCompoundStructure(structure)
count += 1
print('Changed: {} -> {}'.format(mat_name, mat_B.Name))
print('In total {} materials have been renamed.'.format(count))
t.Commit()