Hello, I have a recurring problem with the models, there are hundreds or thousands of materials created with the nomenclature “Phase - Existing” within the families and the project itself, this image is an example using the clear unused window (purge), however in my project this window does not load, revit crashes.
Is there a way to exclude by selecting certain materials?
I saw that there is “Wipe Models Components” but it deletes all materials from the project.
I quickly made this in ChatGPT and tested it.
It list duplicate materials and how many duplicates there are in the model.
If you select one ore more, it will delete all but the “first” one (the one with the lowest numbered ID.
from pyrevit import revit, forms
from Autodesk.Revit.DB import FilteredElementCollector, Material
# Initialization
uidoc = __revit__.ActiveUIDocument
doc = uidoc.Document
# Function to get all materials
def get_all_materials(doc):
return FilteredElementCollector(doc).OfClass(Material).ToElements()
# Function to find duplicate materials
def find_duplicate_materials(materials):
material_dict = {}
for mat in materials:
mat_name = mat.Name
if mat_name in material_dict:
material_dict[mat_name].append(mat)
else:
material_dict[mat_name] = [mat]
duplicates = {k: v for k, v in material_dict.items() if len(v) > 1}
return duplicates
# Function to delete selected materials
def delete_selected_materials(doc, materials_to_delete):
with revit.Transaction("Delete Duplicate Materials"):
for mat in materials_to_delete:
doc.Delete(mat.Id)
# Main script
materials = get_all_materials(doc)
duplicates = find_duplicate_materials(materials)
if duplicates:
duplicate_materials_list = ["{} ({} duplicates)".format(mat_name, len(mats)) for mat_name, mats in duplicates.items()]
selected_duplicates = forms.SelectFromList.show(duplicate_materials_list, multiselect=True, title="Delete Duplicate Materials")
if selected_duplicates:
materials_to_delete = []
for sel in selected_duplicates:
mat_name = sel.split(" (")[0]
materials_to_delete.extend(duplicates[mat_name][1:]) # Keep the first instance, delete the rest
delete_selected_materials(doc, materials_to_delete)
else:
forms.alert("No duplicate materials found.", title="Check Complete")
Might not be exactly what you are after, but should get you going, I hope!