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.
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.
We can now start to define our 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.
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.
All I need to do to get it to work is to 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.
The 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.
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:
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:-
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.
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.
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.