Revit API for Python

Hi,

I just started learning python and I would like to use it at work. I already played around with Rhino Python Script, and it was very intuitive and fun experience. Now I’m trying to write some code for dynamo Python node, but I just don’t know where to start. I’ve already found the revitapisearch.com , but those commands are not for Python - there is only C#, Visual Basic and Visual C++. So as I understand, this won’t work with Dynamo Python node nor PythonShell, right? Or am I being stupid? Could someone give me a simple example how to, lets say, create a reference point in specified coordinates with Python? I just need a piece of a simple code to see how it should be done. Is there any way I could use revitapisearch with python?

 

Best

1 Like

Hi,

Have you had a chance to check out the python guide for 0.7?

The last code snippet should work for reference points.

Thank you, Dimitar. I have checked the examples from this article, and it was very helpful. However I have a problem with XYZ() - Python says it’s not been defined, whether I use doubles as coordinates or leave an empty bracket. Do you know what could be the cause? Does it have something to do with Revit version?

 

Best

You are likely missing a reference to the Revit API like this:

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *

You can find a lot of examples here:

(These are all the Python scripts from package Clockwork extracted as separate files…)

1 Like

Question Marc, Were you able to solve your original post? I am just now starting and wold like to know if C# Scrips can be used or if there is an API library for Revit Python.

I know there’s no API library for Revit in Python. I heard from Ehsan Iran-Nejad that they are looking to implement some code samples & syntax in Python in revitapidocs.com. Nothing has been happening yet but I also have this question:

How do you know what to type in Python node for Revit API syntax that is in C# or C++ or Visual Basic?? :thinking:

Well, you just kind of know. Just type whatever you see in C# but using Python syntax. I don’t think there is an easy way to explain this. There are online syntax converters if you just google C# to Python.

Thanks Konrad. I have used online converters but they don’t really provide a useful result. One of the instances which frustrated me a lot was using Revit API to get file version. I checked the documentation and found that we have the C# syntax below for that:

public string SavedInVersion { get; }

I guessed the syntax for Python would be:

Autodesk.Revit.DB.BasicFileInfo.SavedInVersion(filepath)

Which didn’t work. I tested a few other possibilities but still no luck. Then came across a Python node in Rhythm which does exactly that. By checking the code I found that we have this method to get SavedInVersion!!:

BasicFileInfo.Extract(i).SavedInVersion

in which i is a file path as string!

Full code is:

#Copyright(c) 2017, john pierson
items = IN[0]
typelist = list()
for i in items:
	try:
		typelist.append(BasicFileInfo.Extract(i).SavedInVersion)
	except:
		typelist.append(list())
		
OUT = typelist

Do I need more experience in Python to find these peculiarities? or should I be looking to learn C#?

There are different ways of getting file version. What John demonstrated is a way to get it without opening the file. There are ways to get it from currently open file etc.

Now, this is ALL really simple, but you have to have SOME rudimentary understanding of programming languages, class structure, method vs. property etc. When a programmer looks at public string SavedInVersion { get; } you immediately know that it’s a read only property of a BasicFileInfo class. This is ALL really obvious for someone who has at least a basic understanding of programming in C#. Translating that to Python, is again a matter of understanding basic principles of programming. To call a property on a class object, you have to first get or create an instance of that class. That’s exactly what John was doing.

Like I said, it’s pretty straight forward, but AFTER you have learned some basics of programming. I would suggest that you sign up for some intro classes to programming. Take C# as it’s more relevant to Revit, and Revit API is documented very well in that language. Once you learn that, it will be easy to translate to Python.

6 Likes

drops his Python class
signs up for a C# class instead

As always thanks for the words of wisdom @Konrad_K_Sobon!

1 Like