RevitPythonShell: Set-Up

<<< Return to Nathan's Revit API Notebook

Imports and Document

A RevitPythonShell script first requires that we define our imports which will give us access to the Revit API

Below I am importing the clr library and the commonly used math library. I am then adding references to 'RevitAPI' and 'RevitAPIUI'

Finally, I am creating a variable 'app' and 'doc' which sets the active Revit/Vasari application and document.

import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
Transaction

Next we need code that tells Revit to begin a new transaction using the active document. We will create a variable t to be our transaction.

We can then using t to start the transaction, perform some action, and commit the transaction.

t = Transaction(doc, 'This is my new transaction')
 
t.Start()
 
#perform some action here...
 
t.Commit()
Close Python Window

After you run your script you can tell RevitPythonShell to automatically close the scripting window if you do not need it.

__window__.Close()
Summary

Our final set-up code will look something like below. We are (1) importing our libraries, (2) setting the application and document, (3) starting a transaction, (4) then committing a transaction.

This will be a common structure I will use in most examples.

#import libraries and reference the RevitAPI and RevitAPIUI
import clr
import math
clr.AddReference('RevitAPI') 
clr.AddReference('RevitAPIUI') 
from Autodesk.Revit.DB import * 
 
#set the active Revit application and document
app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
 
#define a transaction variable and describe the transaction
t = Transaction(doc, 'This is my new transaction')
 
#start a transaction in the Revit database
t.Start()
 
#perform some action here...
 
#commit the transaction to the Revit database
t.Commit()
 
#close the script window
__window__.Close()
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License