3.2 Boilerplate Setup Code

Boilerplate code does all of the initial setup so you can start coding

Boilerplate Code Lives Up Top

It's important to understand what boilerplate code is as your scripts simply won't run without it! It's a part of the context we spoke about in the Seeing The Bigger Picture section.

In almost any language, any script begins with bringing in all the libraries and resources you'll to load as part of your script. Most Dynamo definitions depend on loading in custom node packages and scripting is the same. The more generic term for this is dependencies and the copy/pasted code you'll use to bring these into your scripts is generally known as boilerplate code.

The libraries you bring into your script can come from a wide variety of sources - IronPython has the ability to load .NET Framework libraries, which cover everything from UI development to networking and highly capable.

Gimme That Boilerplate Code

If you're simply looking for some quick copy/paste code that will work in most Python Script nodes, then this is provided for you below.

Please Note: Not all of this is necessary. While not absolutely vital, it very much helps to have an understanding of what each line does and why. If you'd like to learn more about each line, click the Annotated Boilerplate Code tab below to switch to a fully-annotated version of the code.

RevitBoilerplate.py
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication 
app = uiapp.Application 
uidoc = uiapp.ActiveUIDocument

#######OK NOW YOU CAN CODE########

Python Script Templates

In Dynamo 2.0, Python script templates were introduced. Set these up and Dynamo will automatically add in your boilerplate to every Python node by default, saving you a lot of copy/pasting!

Footnote: Good & Bad Importing Habits

Technically, some of the above code does not reinforce best practice when importing libraries. It has been written to introduce the basic concepts and key libraries in a straightforward a way as possible.

For scripts that you write for yourself, the above code should perform perfectly well. However, if you decide to publish a Dynamo that will be distributed to others, this guide encourages you to follow best practice in importing dependencies:

  • Avoid importing any classes that are not needed.

  • "From [namespace] import *" is a brute-force import all, which will doubtlessly load in tens or hundreds of unnecessary classes and which will slow down your code.

Last updated