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.
Revit Boilerplate
Revit Boilerplate (Annotated)
RevitBoilerplate.py
1
import clr
2
import sys
3
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
4
import System
5
from System import Array
6
from System.Collections.Generic import *
7
clr.AddReference('ProtoGeometry')
8
from Autodesk.DesignScript.Geometry import *
9
clr.AddReference("RevitNodes")
10
import Revit
11
clr.ImportExtensions(Revit.Elements)
12
clr.ImportExtensions(Revit.GeometryConversion)
13
clr.AddReference("RevitServices")
14
import RevitServices
15
from RevitServices.Persistence import DocumentManager
16
from RevitServices.Transactions import TransactionManager
17
​
18
clr.AddReference("RevitAPI")
19
clr.AddReference("RevitAPIUI")
20
​
21
import Autodesk
22
from Autodesk.Revit.DB import *
23
from Autodesk.Revit.UI import *
24
​
25
doc = DocumentManager.Instance.CurrentDBDocument
26
uiapp = DocumentManager.Instance.CurrentUIApplication
27
app = uiapp.Application
28
uidoc = uiapp.ActiveUIDocument
29
​
30
#######OK NOW YOU CAN CODE########
Copied!
RevitBoilerplateAnnotated.py
1
import clr #This is .NET's Common Language Runtime. It's an execution environment
2
#that is able to execute code from several different languages.
3
import sys #sys is a fundamental Python library - here, we're using it to load in
4
#the standard IronPython libraries
5
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib') #Imports the
6
#standard IronPython libraries, which cover everything from servers and
7
#encryption through to regular expressions.
8
import System #The System namespace at the root of .NET
9
from System import Array #.NET class for handling array information
10
from System.Collections.Generic import * #Lets you handle generics. Revit's API
11
#sometimes wants hard-typed 'generic' lists, called ILists. If you don't need
12
#these you can delete this line.
13
clr.AddReference('ProtoGeometry') #A Dynamo library for its proxy geometry
14
#classes. You'll only need this if you're interacting with geometry.
15
from Autodesk.DesignScript.Geometry import * #Loads everything in Dynamo's
16
#geometry library
17
clr.AddReference("RevitNodes") #Dynamo's nodes for Revit
18
import Revit #Loads in the Revit namespace in RevitNodes
19
clr.ImportExtensions(Revit.Elements) #More loading of Dynamo's Revit libraries
20
clr.ImportExtensions(Revit.GeometryConversion) #More loading of Dynamo's
21
#Revit libraries. You'll only need this if you're interacting with geometry.
22
clr.AddReference("RevitServices") #Dynamo's classes for handling Revit documents
23
import RevitServices
24
from RevitServices.Persistence import DocumentManager #An internal Dynamo class
25
#that keeps track of the document that Dynamo is currently attached to
26
from RevitServices.Transactions import TransactionManager #A Dynamo class for
27
#opening and closing transactions to change the Revit document's database
28
​
29
clr.AddReference("RevitAPI") #Adding reference to Revit's API DLLs
30
clr.AddReference("RevitAPIUI") #Adding reference to Revit's API DLLs
31
​
32
import Autodesk #Loads the Autodesk namespace
33
from Autodesk.Revit.DB import * #Loading Revit's API classes
34
from Autodesk.Revit.UI import * #Loading Revit's API UI classes
35
​
36
doc = DocumentManager.Instance.CurrentDBDocument #Finally, setting up handles to the active Revit document
37
uiapp = DocumentManager.Instance.CurrentUIApplication #Setting a handle to the active Revit UI document
38
app = uiapp.Application #Setting a handle to the currently-open instance of the Revit application
39
uidoc = uiapp.ActiveUIDocument #Setting a handle to the currently-open instance of the Revit UI application
40
​
41
#######OK NOW YOU CAN CODE########
Copied!
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 modified 1yr ago
Copy link