BDX
intermediate·3 min read·· views

pyRevit Snippet: Validate a Parameter Exists Before Writing to It

Batch parameter writes fail for one boring reason: a handful of elements don’t carry the parameter. The script crashes at element 847 of 2,000, and now your model is half-updated inside an open transaction.

The Guard Clause

def set_param_safe(element, param_name, value):
    param = element.LookupParameter(param_name)
    if param is None or param.IsReadOnly:
        return False
    param.Set(value)
    return True

Using It in a Batch Run

from pyrevit import revit, DB

elements = DB.FilteredElementCollector(revit.doc)\
    .OfCategory(DB.BuiltInCategory.OST_DuctTerminal)\
    .WhereElementIsNotElementType()\
    .ToElements()

skipped = []
with revit.Transaction('Update AHU zone tags'):
    for el in elements:
        if not set_param_safe(el, 'TBT_Zone', 'AHU-01'):
            skipped.append(el.Id.IntegerValue)

if skipped:
    print('Skipped {} elements: {}'.format(len(skipped), skipped[:20]))

Why Not try/except?

A bare try/except hides why the write failed — read-only parameter, missing parameter, or wrong storage type all look identical. The explicit check costs one line and gives you a skip list you can hand to the modeler responsible.

One More Trap

LookupParameter returns the first parameter with that name. If a family has both an instance and a type parameter named TBT_Zone, you may write to the wrong one. Prefix shared parameter names by discipline (TBT_) and never duplicate names between instance and type.

Was this helpful?

Share thisFacebookLinkedInXZalo

Comments

Never shown publicly

Get new tips in your inbox

Checklists, code snippets and BIM workflows. No spam.