Dynamo is very useful when interacting with Revit using the various Revit nodes available in Dynamo. These Revit nodes can be accessed and used from within the Python script node. Jeremy explains how to import the Revit node library into Python to utilize in your script.
- [Narrator] Let's now have a look at some more libraries by starting with the Dynamo Revit Node Library. This will allow us to interact with Revit by calling Revit nodes found in the Dynamo Node Library. This provides us basic Revit operations such as selecting elements, getting and setting Revit element parameters, or creating new Revit elements. I currently have the Revit exercise file open for this video. I then opened up a new Dynamo file, so let's start by placing a new Python script node, and opening it up.
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. Like we did with the design script library, we simply need to add the Revit Node library file. To do this, let's access the add reference function from the CLR module, and we'll use the string RevitNodes for the parameter. Making sure to capitalize the R and the N. As we've learned in the last video, Revit Nodes is the name of the DLL file that comes with Dynamo.
Once we have added the Revit Node Library, we need to import the Revit Namespace. This is contained within the Revit Nodes Library. So let's use import Revit. Now that this is added, let's have a look at what's inside. Let's do that by outputting D-I-R and then Revit.Elements. This will show us what classes are available in the Revit.Elements namespace. Go ahead and hit run. You can see that there's quite a few classes to use.
If we look at the Revit Node menu, and then elements, we can see that the output from the Python script matches the elements that are shown in here. All of these Revit classes in the Revit element sub-menu are now accessible to create Revit objects in Python. So let's give that a try in Python by creating a family instance. To do this, we'll use the ByPointAndLevel Node to create some columns. So in Python, let's start by creating an empty list called output.
We can later append the columns to this list. Then let's start a for loop to generate the x and y coordinates for the points that will drive the new columns. To do this, let's use a range method and the variable X. This time however, we're going to use another version of the range function. Instead of using one parameter, we'll be using three. These being zero, five thousand, and one thousand. The additional parameters will mean that we're automatically using another version of the range function.
This is what we call an overloaded function. As the different parameters we give it will use a different version. Just as the Dynamo range node works, we're using a version which takes the parameters start, stop and step. So this range function will create a range of numbers from zero to five thousand with intervals of one thousand. The reason I'm using the large numbers is because the Revit exercise file is in millimeters. So let's use these numbers to create the X and Y coordinates of a new point.
To do this, let's use the variable point, and then the ByCoordinates method. We'll use X for the X and Y coordinate of the new point. This will create a diagonal of columns. Next, let's create our column. To do this, let's have a look at the FamilyInstance ByPointAndLevel node. Go ahead and save changes. And we can see it from the inputs on this node, we need a family Type, a point, and a level. So let's input the family type and level using Dynamo nodes.
Go ahead and search for Family Types, and then in the drop down menu, let's select Round Column 152 millimeters. Then input this into port zero on the Python Script. Next we need a level, so let's search for levels, and then select level 1. Then add this to a new port on the Python Script. The reason we are using the FamilyInstance ByPointAndLevel is because a column requires a level to be placed.
So using these, let's open up Python again, and we'll assign them to two different variables. Family and level. We can now go ahead and create our columns. To do this, let's copy the name of the node. So on a new line, in the for loop, let's use column for the variable, and we'll assign to this FamilyInstance.ByPointAndLevel.
The parameters for this, as we just learned, are the family, a point, and a level. Then lets append this to the output list. Before we run it, let's change the statement Import Revit to from Revit.Elements import star. This means that we can use all of these classes directly in our Python script. Let's go ahead and hit save changes and see what we get. We have an error on line 23, which is saying Revit is not defined.
This is because we changed the import statement, and we're still outputting this D-I-R function. So let's change this to output and hit save changes. Great so it looks like our columns are being created. Let's switch over to Revit and see what they look like. Go to the 3D view, perfect. It looks like our columns are being placed in a diagonal line. By referencing in the Revit Nodes Library, we can extend Pythons 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 the Dynamo nodes and by doing so, extend Dynamo's functionality even further.
Note: This course uses Dynamo 2.0.1 and Revit 2019. For best results, it helps to have the same versions of the software.
- Importing the Dynamo Geometry library
- Python basics: variables, conditionals, operators, loops, and functions
- Using and extending Python modules
- Accessing the Revit API with Python
- Revit and Dynamo conversions
- Creating a collector in the Revit API
- Getting Revit parameters with the Revit API
- Adjusting elements with the Revit API
- Creating views with the Revit API