execute_revit_code
Runs IronPython 2.7 code inside Revit within a single transaction and returns printed output, enabling custom model queries or modifications when no dedicated tool applies.
Instructions
Run IronPython 2.7 code inside Revit and return what it prints.
Only use this when no dedicated tool can do the job. The code runs inside a single transaction that is committed on success and rolled back on error, so do not start your own Transaction.
Available names: doc (active Document), DB (Autodesk.Revit.DB),
revit (pyRevit helpers), System, clr, and print (its
output is returned to you).
IronPython 2.7 rules:
no f-strings; use "{}".format(...)
Revit stores lengths in feet; divide millimeters by 304.8
build element IDs as DB.ElementId(System.Int64(n)); a bare int is ambiguous in Revit 2027
some elements lack
.Name; read it defensively, e.g. getattr(el, "Name", None) or the ALL_MODEL_TYPE_NAME parametercheck doc.GetElement(...) for None before using the result
Example:
walls = DB.FilteredElementCollector(doc)
.OfCategory(DB.BuiltInCategory.OST_Walls)
.WhereElementIsNotElementType().ToElements()
print("walls: {}".format(len(walls)))
Args: code: IronPython source to execute. description: Short label; it becomes the transaction name shown in Revit's Undo menu.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "execute_revit_code"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| description | No | Code execution |