Graitec Blog

Welcome to the Graitec blog, we hope you enjoy the content and if so please subscribe to the blog or a category for automatic notifications when new posts are added.

subscribe to blog

Accessing the Entire Revit API Using Dynamo and Python

So, you have been using dynamo but you realise you cannot find a node to do a specific task. You search the packages and still cannot find what you are looking for. Using python, it is possible to access the whole Revit API (Application Programming Interface). Let me show you how, and where you can find resources to show you how it all works.

Let’s start with a very basic overview of the Python node.

Python Script Node

The Python Script node can be found by searching the node library in Dynamo, by default the node will give you a single input and a single output, by using the + and – buttons re can create additional inputs. Double clicking on the node will open the python editor, you will notice that some default code already exists.

Python Script with Default Code

We can now start to define our input and output variables.

Pythin Script Define Input and Output Variables

We can now use a combination of standard python code and Dynamo nodes to create some basic code. You might notice that a lot of the nodes have names with no spaces in them or using “.” This is because we can call any of these nodes by name.

Standard Python Code and Dynamo Nodes

The name also denotes the order in which we assign the inputs. So, if I want to Draw a circle in Python, I can create a point using the 3 inputs I have created and then draw the circle.

Python Script Draw a Circle

All I need to do to get it to work is to create the values on the inputs.

Create the values on the inputs

We can now take it to the next level and produce loops in code to generate multiple elements, I’ll talk more about loops in future blogs, but you can see how we can control the nodes in Python as well as with multiple nodes in Dynamo.

Python Script Produce Loops in Code

The resulting geometry:

Resulting Geometry

I appreciate that this can be done using nodes, but it dramatically reduces the number of nodes required. It also gives us much more control over how we manipulate the data.

The real value of using Python comes when we access the Revit API.

Line for accessing all commands in Revit API

By adding the line above, we can access all the commands that are available in the Revit API, all the commands that can be accessed are documented in the Revit API Docs,  there is even a section here that specifically helps when using python as the programming environment. This document is fully searchable, if for instance I wanted to create a schedule in the project using a Python script I could do so as follows:

Create a Schedule in Project using Python Script

Having searched for schedule I can see that the schedule commands are in the Revit.DB module so the code we need to start with is:-

Code to start with

This will then give me access to the commands that are embedded in the ScheduleDefinition, BuiltInCategory and ElementID module, all of which are required when creating a schedule.

By using this method to access the API we can achieve pretty much anything using Dynamo. This is programming and I am not going to say it is easy, but it is great to know that Dynamo is very expandable.

The below code uses an excel spreadsheet as an input and then creates materials based on the name and colour (R.G.B) value from the spreadsheet.

Code using an Excel spreadsheet

This only creates a new material with the shading value set, it does not create the assets for the material definition. This is going to be my next project. If you would like to examine more code, try doubling clicking into one of the package nodes, such as a LunchBox node, you can then double click on the Python node to find out what is going on in the background.

LunchBox Get Parameter Value

To read my other Dynamo blogs just click the link. Moreover, if you are interested in learning more about Dynamo or Python, then please get in touch with Graitec by clicking the button below and then filling out the form.

Request More Information

Rate this blog entry:
Top Tips to Remember When Using NC Files for Struc...
Spill Consult Animated Banner – A Motion Graphics ...

Related Posts

autodesk reseller sept16v2
bottom logo

In order to provide you with the best online experience this website uses cookies.