execute_revit_code
Run IronPython scripts directly in Revit to access the active document, modify models, and control the UI.
Instructions
Execute IronPython code directly in Revit context.
The code has access to:
doc: The active Revit document
uidoc: The active UIDocument (use for UI operations like switching the active view)
DB: Revit API Database namespace
revit: pyRevit module
print: Function to output text (returned in response)
No transaction is opened automatically. Wrap model-modifying code yourself: t = DB.Transaction(doc, "My change") t.Start() # ... modify model ... t.Commit()
For UI operations that cannot run inside a transaction (e.g. switching the active view): all_views = DB.FilteredElementCollector(doc).OfClass(DB.View).ToElements() target = next((v for v in all_views if v.Name == "Level 1"), None) if target: uidoc.ActiveView = target
Tips:
Use getattr(element, 'Name', 'N/A') to safely access the Name property
Check elements exist before use: if element:
Use hasattr() for optional properties
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| description | No | Code execution |