Join Jeremy Graham for an in-depth discussion in this video Adding the Revit library, part of Dynamo for Revit: Python Scripting.
- [Instructor] Now let's have a look at importing the Dynamo Revit Node Library, which will allow us to interact with Revit. In Dynamo, we can do this by placing Revit nodes from the Dynamo Node Library. This provides us basic Revit operations, such as selecting Revit elements, getting and setting Revit element parameters or creating new Revit elements. Let's import the Revit Node Library into Python and we'll use the FamilyInstance.ByCoordinates and Level node to place an architectural column based on points that we have created in Python. So let's go ahead and start a new Python script.
To use the Revit nodes, the first thing we'll need to do is import the Revit Node Library. The Dynamo developers have made this simple for us as like we did with the Geometry Library, we simply need to add the Revit Node Library file. To do this, we can use the CLR object to AddReference. And we are adding the reference RevitNodes. Once we have added the Revit Node Library, we need to import the Revit Namespace. You might be wondering why we need to add this library using the CLR object, whereas with Python modules, we simply import it.
This is because the Revit Node Library was not actually written in Python. It's been written in another language: C#. The CLR or Common Language Runtime that we have been importing into our script allows our Python scripts to work with other libraries that were not written in Python. Once we have added the library through the CLR object, we can import its contents like a Python module. When we execute our scripts, the CLR object will convert all the code, whether C# or Python, and translate it for our computer to execute. So, now that we have the Revit Node Library added, let's output dir(Revit.Elements) as the parameter to see what's available to us.
Let's have a look at the Revit Elements' dropdown in the Dynamo menu and we can see the output from our Python Script matches that of the elements in the dropdown in Dynamo. All these Revit classes in the Revit Elements' dropdown are now accessible to create Revit objects in Python. So let's jump back into Python and we'll use the FamilyInstance.ByPointAndLevel node to create some columns. First up, let's create an empty list which we'll use to append our columns to.
Then, let's create a for loop to generate the x and y coordinates to the points that will drive the new column families. To do this, let's use a range method. However, this time, we're going to use another version. Instead of using one parameter, we'll be using three. These being zero, 5000 and 1000. The additional parameters automatically uses another version of the range function. Just as the Dynamo range node works, we're using a version which takes the parameters start, stop, and step.
So, this range will create a range of numbers from zero to 5000 with intervals of 1000, producing six numbers. The reason I'm using large numbers is because the Revit project file I'm using is in millimeters. So let's use these to create points. To do this, let's use point.ByCoordinates with the i variable being the x and y-coordinate, which will create a diagonal of points. Next, let's create our column and to do this, let's look at the FamilyInstance.ByPointAndLevel node in our workspace.
We can see from the inputs that this node requires a familyType, point, and level to run. So let's input a familyType and a level into our Python node. Go ahead and search for family types and we'll look for Round Column. Perfect. We'll input that into input zero. And then let's search for the level node and we'll input level one into input one.
The reason we're using FamilyInstance.ByPointAndLevel is because columns require a level to be placed. So let's open up Python again and we'll assign the incoming families to variables family and level. Great, so let's go ahead and create our column by copying the name of the node. So, FamilyInstance.ByPointAndLevel and the inputs are family, point, and level.
And you'll notice that I have assigned this to the column variable. So next, let's go ahead and append our column variable to the output list. And before we accept, let's change the import Revit statement to from Revit.Elements import *. This means that we'll have all the nodes available to us from the Revit.Elements namespace. Then let's output our output list. And so we have... Great, so it looks like our columns are being created.
Let's switch over to Revit and we can see the columns are now in our view. By referencing in the Revit Nodes Library, we can extend Python's functionality to interact with Revit. This is great if we only want to use the methods available to us in the standard Dynamo Library. However, using Python, we can access the Revit API directly rather than using Dynamo nodes and by doing so, extend Dynamo's functionality.
Note: This course uses Dynamo 1.3.1 and Revit 2018. For best results, it helps to have the same versions of the software.
- Importing the Dynamo Geometry library
- Defining variables
- Making decisions with conditionals
- Controlling flow with operators
- Defining functions
- Working with external libraries in Python
- Accessing the Revit API with Python