Python and Revit
Python and Revit
Now that we've demonstrated how to use Python scripts in Dynamo, let's take a look at connecting Revit libraries into the scripting environment. Remember, we imported Python Standard and our Dynamo core nodes with the first four lines in the block of code below. To import the Revit nodes, Revit elements, and the Revit document manager, we only have to add a few more lines:
This gives us access to the Revit API and offers custom scripting for any Revit task. By combining the process of visual programming with Revit API scripting, collaboration and tool development improve significantly. For example, a BIM manager and a schematic designer can work together on the same graph. In this collaboration, they can improve design and execution of the model.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-1ef482c3faf4fbbeca8e0a18551f18b5764bb499%252Fpython%2520%2526%2520revit%2520-%252001.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=655ea083&sv=1)
Platform Specific APIs
The plan behind the Dynamo Project is to widen the scope of platform implementation. As Dynamo adds more programs to the docket, users will gain access to platform-specific APIs from the Python scripting environment. While Revit is the case study for this section, we can anticipate more chapters in the future which offer comprehensive tutorials on scripting in other platforms. Additionally, there are many IronPython libraries accessible now which can be imported into Dynamo!
The examples below demonstrate ways to implement Revit-specific operations from Dynamo using Python. For a more detailed review on Python's relationship to Dynamo and Revit, refer to the Dynamo Wiki page. Another useful resource for Python and Revit is the Revit Python Shell Project.
Exercise 1
Create a new Revit Project.
Download the example file by clicking on the link below.
A full list of example files can be found in the Appendix.
In these exercises, we'll explore elementary Python scripts in Dynamo for Revit. The exercise will focus on dealing with Revit files and elements, as well as the communication between Revit and Dynamo.
This is a cut and dry method for retrieving the doc, uiapp, and app of the Revit file linked to your Dynamo sesson. Programmers who have worked in the Revit API before may notice the items in the watch list. If these items do not look familiar, that's okay; we'll be using other examples in the exercises below.
Here is how we're importing Revit Services and retrieving the document data in Dynamo.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-912ed2adb09e479607c57bc322029012f9720d0d%252Fpython%2520%2526%2520revit%2520-%2520exercise%252001%2520-%252001.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=a6648037&sv=1)
Take a look at the Python node in Dynamo. You can also find the code from below:
Exercise 2
Download the example file by clicking on the link below.
A full list of example files can be found in the Appendix.
In this exercise, we'll make a simple Model Curve in Revit using the Dynamo Python node.
Begin by creating a new Conceptual Mass family in Revit.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-a7f11c2cb8e685f406139ef4a3ce16474f571c45%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252001.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=afded2ef&sv=1)
Open the Conceptual Mass Folder and use the Metric Mass.rft template file.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-cf34bc8377fb42717afb9a2f1be63a0ba1a96f99%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252002.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=c3add7c7&sv=1)
In Revit, use the keyboard shortcut un
to bring up the Project Unit settings, change the length unit to meters.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-84db55bb9a1f7cc3da34a05fa0748d1802c0fd31%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252003.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=66d8a4ed&sv=1)
Launch Dynamo and create the set of nodes in the image below. We'll first create two reference points in Revit from Dynamo nodes.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-22bd3bc5802e266329daa513a678fc1bbd11dbe0%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252004.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=2e28eab1&sv=1)
Create a Code Block and give it a value of
"0;"
Plug this value into a ReferencePoint.ByCoordinates node for X,Y, and Z inputs.
Create three sliders, ranging from -100 to 100 with a step size of 1.
Connect each slider to a ReferencePoint.ByCoordinates node.
Add a Python node to the workspace, click the "+" button on the node to add another input and plug the two references points into each input. Open the Python node.
Take a look at the Python node in Dynamo. Find the full code at the below.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-6ac89434aed7c5f12a64576e77f1c4ccfc0ae4ce%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252005.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=5111feff&sv=1)
System.Array: Revit needs a System Array as an input (rather than a Python list). This is just one more line of code, but paying attention to argument types will facilitate Python programming in Revit.
In Dynamo, we've created two reference points with a line connecting them using Python. Let's take this a little further in the next exercise.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-32471c2dc6609bd976404712adfdca797d0f2721%252Fpython%2520%2526%2520revit%2520-%2520exercise%252002%2520-%252006.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=81d39e1b&sv=1)
Exercise 3
Download the example file by clicking on the link below.
A full list of example files can be found in the Appendix.
This exercise keeps it simple, but drives home the topics of connecting data and geometry from Revit to Dynamo and back. Let's begin by opening Revit-StructuralFraming.rvt. Once opened, launch Dynamo and open the file Revit-StructuralFraming.dyn.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252FkkUSzoJGOZ22KcBCWlN5%252Fpython%2520%2526%2520revit%2520-%2520exercise%252003%2520-%252001.jpg%3Falt%3Dmedia%26token%3D642a8d6d-a152-4ea9-819f-ed469b976439&width=768&dpr=4&quality=100&sign=9862293&sv=1)
This Revit file is about as basic as it gets. Two reference curves: one drawn on Level 1 and the other drawn on Level 2. We want to get these curves into Dynamo and maintain a live link.
In this file we have a set of nodes plugging into five inputs of a Python node.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-6121270396455aa13fba8d84a9217bda9b6d4915%252Fpython%2520%2526%2520revit%2520-%2520exercise%252003%2520-%252002.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=669c5287&sv=1)
Select Model Element Nodes: Hit the select button for each and select a corresponding curve in Revit.
Code Block: using the syntax
0..1..#x;
, connect an integer slider ranging from 0 to 20 into the x input. This designates the number of beams to draw between the two curves.Structural Framing Types: We'll choose the default W12x26 beam here from the dropdown menu.
Levels: select "Level 1".
This code in Python is a little more dense, but the comments within the code describe what's happening in the process
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-eecb24102e9619747a50886fda34e1e1abfd7ffb%252Fpython%2520%2526%2520revit%2520-%2520exercise%252003%2520-%252003.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=7c8fb425&sv=1)
In Revit, we have an array of beams spanning the two curves as structural elements. Note: this isn't a realistic example...the structural elements are used as an example for native Revit instances created from Dynamo.
In Dynamo, we can see the results as well. The beams in the Watch3D node refer to the geometry queried from the Revit elements.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-82a5d23bd2ca90e68857733c8e7d54e837976cc5%252Fpython%2520%2526%2520revit%2520-%2520exercise%252003%2520-%252005.jpg%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=5313d8df&sv=1)
Notice that we have a continuous process of translating data from the Revit Environment to the Dynamo Environment. In summary, here's how the process plays out:
Select Revit element
Convert Revit element to Dynamo Curve
Divide Dynamo curve into a series of Dynamo points
Use the Dynamo points between two curves to create Dynamo lines
Create Revit beams by referencing Dynamo lines
Output Dynamo surfaces by querying the geometry of Revit beams
This may sound a little heavy handed, but the script makes it as simple as editing the curve in Revit and re-running the solver (although you may have to delete the previous beams when doing so). This is due to the fact that we are placing beams in python, thus breaking the association that OOTB nodes have.
With an update to the reference curves in Revit, we get a new array of beams.
![](https://primer2.dynamobim.org/~gitbook/image?url=https%3A%2F%2F1734247194-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252FY5ZuHF3yuXFWp1C46ZSo%252Fuploads%252Fgit-blob-e18312c9498c44024f3a75f0e39832dc6d178fb9%252Fpython%2520%2526%2520revit%2520-%2520ex%252003%2520-%252006.gif%3Falt%3Dmedia&width=768&dpr=4&quality=100&sign=6459d5c&sv=1)
Last updated