How to link my python script created with OpenAI into Revit

I know basically 0 about scripting, and i hope no hard working script writers take offence to this post, as it was created with OpenAI.

I asked the program to write me a script to use within revit. Basically to create window elevations and dimension them. It came up with a few errors so i asked it to fix the errors, and it did, i think?
I just know very little and am unsure as to how to link this python script into dynamo for it to read my revit file and run the script. The code is as below:

# Autodesk Revit Python Script
# Script to create window elevations and dimension them

# import necessary modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *

# define a transaction
def create_window_elevations_and_dimensions(doc):
    t = Transaction(doc, 'Create window elevations and dimensions')
    t.Start()

    # create a new 3D View
    view3d = View3D.CreateIsometric(doc, ElementId(BuiltInCategory.OST_Views))

    # get all windows in the model
    windows = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Windows).WhereElementIsNotElementType().ToElements()

    # loop through all windows
    for window in windows:
        # create a detail level for elevation view
        viewDetail = ViewDetailLevel.CreateDetail
1 Like

There’s a Dynamo node for Python scripts that you can use, simply called “Python Script”.

But uhhh… that script isn’t going to do much…

I know basically 0 about scripting

So with no knowledge of Python or coding in general, instead of trying to learn anything you’re hoping an AI can produce functioning code that actually does what you ask it to? Why not just use the node based systems in Dynamo? Visual scripting is accessible as well as easy and fast for prototyping. You don’t need coding knowledge.

E.g. below, I saw your Python is trying to create a 3D view at the start, so I rolled with that:

30 seconds max to put together and now you’ve got a named 3D iso view for every window in the file.

Creating elevations is more complex, adding dimensions is more complex again. Take it step by step (which is easy to do with nodes in visual scripting). An AI isn’t going to whip this up for you in 10 lines of code.

4 Likes

And so it begins.

Unless you plan on waiting the 30+ years we’ll need for code to learn how to think like a human vs harvest pre-existing info off the web I’d say it’s time to learn to program vs hope openAI does it for you (no offense intended). At best, most programmers I’ve spoken to (software and hardcore alike) agree it will become an aid for programmers at the best, to assist with debugging and generating simple/reusable code - people will still be needed for creative process’ and ingenuities.

If code becomes so easy that anyone will be able to do it, your boss will do it and fire you instead in a capitalistic society, but I digress…

A good post here by a solid programmer identifying common errors chatGPT makes and highlighting them in light of Revit API here:

5 Likes

Apologies, don’t think i pasted the whole code…

I knew posting this would annoy people, which isn’t my intention. If there is a tool to assist people with understanding how code functions, and an AI can give you an answer, or better yet a path to go down and post onto the internet for others to assist in what you’re trying to solve and further teach you scripting.The reason i don’t delve into scripting or coding is because it overwhelms me and if this sets me in a direction as to better understand the method behind the madness then whats the issue, right?

SyntaxError: unexpected token ‘’

Fix: Add a colon after the for loop.

# Autodesk Revit Python Script
# Script to create window elevations and dimension them

# import necessary modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *

# define a transaction
def create_window_elevations_and_dimensions(doc):
    t = Transaction(doc, 'Create window elevations and dimensions')
    t.Start()

    # create a new 3D View
    view3d = View3D.CreateIsometric(doc, ElementId(BuiltInCategory.OST_Views))

    # get all windows in the model
    windows = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Windows).WhereElementIsNotElementType().ToElements()

    # loop through all windows
    for window in windows:
        # create a detail level for elevation view
        viewDetail = ViewDetailLevel.CreateDetail

It wont so much as annoy people to explore this field, but it will if we get it thrown at us as a viable alternative - which it clearly isn’t at this point in time.

At the very least we would expect you endeavour to learn the basics of a programming language or environment before asking a forum to fix a half baked code from an NLP such as chatGPT. Myself and lot of other programmers are already having to explain to colleagues and relatives on a daily basis why our passions aren’t doomed to the masses, so you might find a frosty reception if you don’t show any interest/evidence in learning to program a bit.

If you truly want to learn a bit about programming begin with Dynamo, and the Dynamo Primer.

If you don’t want to learn programming at any level, keep using third party add-ins built for you until chatGPT and its equivalents learn to build error-proof code.

3 Likes

I knew posting this would annoy people

The idea of using an AI to aid programming or program simple but time consuming tasks that it won’t stuff up, won’t necessarily annoy people. Posting code generated by an AI on a forum with “help me fix this, I have zero coding knowledge” will annoy people.

If there is a tool to assist people with understanding how code functions

AI isn’t going to do this for a long time yet. You’ve asked it to create elevations and it’s creating 3D views. Trying to understand an interpretation of an interpretation is like playing Chinese whispers.

I saw a meme recently that was poking fun at that while the AI might write the code in a fraction of the time it takes a human to, you’ll spend 10 times as long debugging it. If the AI code takes significant effort for a dev to work out what’s going on and fix, how to you expect to understand that code with zero programming knowledge?

AI coding feels more like a tool for people who already have coding knowledge to use as an aid rather than something average joe can use to get things done. Especially when it comes to the Revit API, perhaps it’s more usable for simpler more isolated tasks.

Fair enough. I appreciate your response, thankyou. I will look into it more and try wrap my head around it.

1 Like

If it helps get the interest/understanding going of programming, I have a platform agnostic series about Python here. About 1.5 hours total watch time and it covers all the basic concepts and syntax involved with Python. No strings attached, free and aimed at non/low level programmers.

At the least it might help you recognize the structure of the code being returned as well as things like variables, methods and comments. Hope that helps! Programming is always frustrating to learn at first I think, and over time things fall into place (but there is always a degree of frustration - learning is hard sometimes!).

4 Likes

As i have 0 program knowledge, and do not understand it how am i meant to know if the script is incorrect? Idk about you, but if i have the option to enable an AI to spit out a script, and debug that script for me to solve a particular tedious issue - and it works, then yes i will choose to not learn scripting as that isn’t my goal. I posted this to get insight into the code it wrote to see if it was anywhere on the right path and if it can be incorporated into dynamo to solve my issue.

but if i have the option to enable an AI to spit out a script, and debug that script for me to solve a particular tedious issue - and it works

The fact you are here asking why the code doesn’t work shows you this isn’t currently an option. Insight has been taken from the outcome at the least I guess, as you’ve noted was the goal of coming here and asking about it.

I think if an AI is developed with the goal of solving Revit code specifically you’ll have a better chance, but chatGPT is a very general NLP, so will only look for patterns and concepts to form what it assumes is a solution - which whilst amusing and still impressive, will not usually generate viable code for use in Dynamo/Revit.

As i have 0 program knowledge, and do not understand it how am i meant to know if the script is incorrect?

Start learning a basic level of coding so you can understand? I’m probably going come across like a dick here, but this really feels like “I want results for zero effort”. People will help you, but there’s a difference between posting relatively complete code with some minor errors, and this code which is so wrong and so far from doing what is described that you’re really not going to get much.

Idk about you, but if i have the option to enable an AI to spit out a script, and debug that script for me to solve a particular tedious issue - and it works

It doesn’t though. And a human will need to debug it, and if you have no knowledge then you can’t do that.

then yes i will choose to not learn scripting as that isn’t my goal

Learning scripting may not be your goal, but it is a method to get to your goal. If coding was my goal, I’d be a developer, not in architecture. I was once a computer science student but bailed because I disliked it, yet here I am haha. If you’re so against programming that you don’t even want to touch Dynamo nodes then maybe it’s time to start forking out for third party add-ins.

2 Likes

Something that is very important is that the data set used for chatGPT is over 1 year old and it does not have access to current information on the internet.

Also you as a user of AI technology and going to implement something it has given you, you must QA the code and assure it is right for the task in hand. If you have no coding knowledge then things could go bad fairly quickly.

What if the AI gives you some code from a bad source that then infects your system with malware…Very bad situation.

Hey @jem.cicekdagKV2ZC.

Welcome to the community! It’s good to see these new technologies are brining in new perspectives. :slight_smile:

Don’t worry about us taking offense to your work - it’s a totally legitimate toolset to explore. If you feel unwelcome feel free to tag me and I’ll try and smooth things over - while we are welcoming some of us can be a bit grouchy in nature.

I have done some GPT3 testing for this over the last few weeks/months. To ‘finalize’ my review today I live tweeted an attempt to use the OpenAI GPT3 chatbot to make some VERY basic Revit code - drawing a wall. It took me half an hour to get rate limited/break the chatbot as it was just calling variables which weren’t defined. The code all looked legitimate in all cases, but in no case was it anywhere near running completely.

After more then 30 minutes and getting to the ‘broken’ state of the chatbot, I did a google search. The first hit was to a 2014 Dynamo forum post which had working code for me.

The take away is that in the current state these coding tools don’t provide usable solutions; This makes sense as they’re built to aggregate answers from multiple sources, but code snippets themselves can’t be built piecemeal. If you define your active Revit document as actDoc and I define it as doc, and Gavin defines it as theGreatestDocumentToEverBe then none of our code will be ‘copy/paste’ ready between code samples. This is the first error I see in your code above; doc isn’t even defined but it’s the variable you are attempting to feed into the definition… which has it’s own transaction but you don’t deal with the active one created by Dynamo.

Tools like copilot likely make more sense for what you’re after, as they are built to assist in faster development using a code driven context (doc would already be defined before it recommended calling it); however it doesn’t appear that is what you were using (hence the undefined variable).

The good news about this is that you don’t really have to worry about it inserting malicious code into your system as even if it tried to the code wouldn’t run. The ‘bad’ news is that some education and training is going to be required to get into this. The bar is likely lowered some already, but it’s got a long way to go to get to the point where an untrained user can get working code.

Good luck with the journey into this space; you’ll certainly find many useful new tools as your skill set grows.

8 Likes

I love this chatbot! :slight_smile:
It helps alot :slight_smile:

3 Likes

Good on you @jem.cicekdagKV2ZC, for us non coders it is a worthwhile question, I landed on this forum for exactly that reason of trying to gain an understaning of what AI means for… everything really. Even just trying to get a handle one what area & depth of knowledge you would need to have to utilise it as a tool & whether its a worthwhile investment of my time, or do I continue with business as usual.

If you can afford to wait say 5-10 years then it might be possible to avoid learning any code. In the meantime it will mostly benefit those with a fundamental understanding trying to do more complex things.

If you follow the trajectory of mainstream programming, it’s only gaining more relevance and application to everyday life and careers - I wouldn’t advise waiting personally.

2 Likes