Revit-API-02

/

/

/

revit-api/02-prepare-battlestation

Prepare your Battle Station for Revit API

We are almost ready to start writing Revit API code. But, we need to setup your Battle station. You could use Notepad, but let's be honest - you wouldn't become the greatest pyRevit hacker with that.

Revit-API-02

/

/

/

revit-api/02-prepare-battlestation

Prepare your Battle Station for Revit API

We are almost ready to start writing Revit API code. But, we need to setup your Battle station. You could use Notepad, but let's be honest - you wouldn't become the greatest pyRevit hacker with that.

Lesson Objectives

  • Install Code Editor

  • Configure pyRevit Autocomplete

  • Configure Revit API Autocomplete

    Are you Ready?

Summary

Setup Your Battle Station

We need to install and configure a proper IDE (Code-Editor) so you can code faster and avoid beginner's mistakes.

The 2 most popular choices are:

  • pyCharm

  • VS Code

These code editors come with a lot of standard features that help you during development, but most importantly we are interested in Revit API and pyRevit Autocomplete.

We will also add an AI-Assistant later on, but for now let's focus on autocomplete feature.

💡Tip: Do not neglect or postpone this step, as it can save you a lot of time and nerves in the future.

Choose Code Editor

We've already installed pyCharm previously so we will continue with that and configure it for Revit API and pyRevit hacking.

However, if you want to use any other IDE like VS Code you can also do that. I will leave some links for setting up VS Code for Revit API.

What is python Interpreter?

In PyCharm, an interpreter is the Python runtime environment used to:

  • Execute your code,

  • Manage project dependencies,

  • And determine the Python version,

This allows you to work with multiple virtual environments for efficient development.


Also a very important note:

💡It doesn't matter what python version you use in pyCharm for Revit API.


We can't execute Revit API code without direct access to Revit Application. That's why we need to use Dynamo, pyRevit or similar add-ins to run python scripts for Revit API.

Therefore, it's irrelevant what python version you use in pyCharm. We use it as a helping tool for writing scripts with autocomplete, and then we use pyRevit to execute this code(more on that later).

Configure phCharm's Interpreter

Now let's open Interpreter Settings in pyCharm:

Look at the bottom right corner of pyCharm and you should see the name of your current interpreter.

In my case it's named Python 3.10, because that's what was installed on my new PC by default, you might have something a little different.


Next you will see this menu with your Python Interpreter selected, or it will say something that it is missing.

Normally, this is the place where you can install additional packages for your project, but we won't need that for Revit API development. Instead, we need to configure something else in a moment.

For now, just open the pulldown menu and click on Show All…

Next you will see a list of all available Python Interpreters that might look something like this:

As you can see I don't have it setup and named correctly, so I've removed all of them during tutorial and created a new one by clicking on the plus (+) sign.

Then you get this menu to create new Interpreter:

We need to specify 2 things for python interpreter:

  • Location of your Virtual Environment (Choose Any Safe Location)

  • Python Base Interpreter (You can select any version)

Once you ready, click on OK and you are supposed to see this loading bar for a brief moment.

Possible Errors at this stage:

💡Tip: If you don't see python version you might need to install python. Go to official python website and you can download any version. But if you can't decide grab python 3.8.5 version.

💡Tip: Sometimes latest pyCharm version gives you errors after this step. I know it's annoying but the best way to fix it is to install older version of pyCharm. For example I use pyCharm Community 2024.2.3. If you get any errors install that pyCharm version, trust me.

Test Default Autocomplete

Alright, if everything works well, you should see your new python interpreter in the list. In my case mine is name (venv_RVT23) because that's how I named the folder in the previous step.

Now we want to test if your regular python autocomplete works. This will help you avoid confusion if you get any errors later on.

Create a new python file:

Right Click on Folder -> New -> Python FIle

Then I'm going to write a very simple python code and I should see Autocomplete pop-up to help me finish method names and provide documentation like this:

PS You can use [CTRL] + [Q] to access documentation in pyCharm.

This means that Default python Autocomplete works!

💡Tip: If not, then try restarting pyCharm or even restart windows. It might help.

Add pyRevit Autocomplete

To add autocomplete for other libraries like pyRevit or Revit API we need to provide paths in python interpreters.

To do that, open interpreter settings and then click on Show Interpreter Paths button. It should open a list of default python paths.

Now we need to add path to pyRevit's library, so pyCharm can reference the code and help us with autocomplete.

Find pyRevit-Master folder. Usually it's in the AppData folder and it can be access by using special name %appdata% in your file explorer.

Now we need to add path to pyRevit's library, so pyCharm can reference the code and help us with autocomplete.

  • Find pyRevit-Master folder.

Usually it's in the AppData folder and it can be access by using special name %appdata% in your file explorer.

INside you need to look for pyRevit-Master/pyrevitlib folder.

Once you have the path, go back to pyCharm and paste the path to pyrevitlib folder like this:

Test pyRevit Autocomplete

Now it's time to test pyRevit Autocomplete.

Sometimes, you might need to wait a little for pyCharm to read everything or restart pyCharm.

To test it, try writing something like this and you should get some autocomplete.

Add Revit API Autocomplete

Now, the most important part - Revit API Autocomplete. To do that we need to download Revit API stubs that will be referenced like pyrevitlib folder for pyRevit.

Revit API Stubs are lightweight placeholders for Revit API classes to simulate their behavior without acces to Revit Application. Think of it as a Revit API documentation that pyCharm can use to provide autocomplete and access to doc strings.

You can download Revit API Stubs Here:


Once you downloaded stubs, extract them somewhere safe. You can place it in the same location you chose for your virtual environment earlier.

Now grab the path and add it to the interpreter's paths same as we did for pyRevit.

Test Revit API Autocomplete

Now it's time to test Revit API Autocomplete.

Firstly, keep in mind that Revit API is much larger than pyRevit, so it needs more time for indexing in pyCharm and sometimes requires restarting pyCharm.

And then you can test Revit API Autocomplete by using the following code:

You should get auto-suggestions and be able to access doc string by using [CTRL] + [Q] shortcut.

💡Tip: If autocomplete doesn't work - you need to double check paths in pyCharm and restart it. Sometimes paths aren't remembered on the first try for some reason, so you might need to add it again.

Disable Auto-Import

Recently pyCharm has added a feature to automatically add imports. It sounds great but it's really horrible in the way it works. Usually it silently add the wrong import and then you wonder why you get an error on importing something you never heard of before.

So let's disable that


Here is how

COMING LATER

HomeWork

Follow along the tutorial to configure Revit API Autocomplete in pyCharm.

This is a very important step and it will save you a lot of time and trouble in the future. So, I strongly recommend that you do that.

⌨️ Happy Coding!

Questions:

I can't create Virtual Environment in pyCharm.

I can't create Virtual Environment in pyCharm.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Discuss the lesson :

P.S. Sometimes this chat might experience connection issues.

Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.

Use coupon code "upgrade" to get 150EUR Discount as a member.

⌨️ Happy Coding!

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API