Downloads
Resources
Summary
Python Script for pyRevit
Soon we will start coding a lot of tools in this course. But before we do that, let's prepare a python template for pyRevit scripts.
First of all I want to show you pyRevit's features that are super easy to use and also you will have a file that you can use for you new tools, so you don't ever start with a blank page.
Let's break down everything we want to include in our template file.
UTF-8 Encoding
First of all it's good to always add utf-8 encoding to our scripts by using this very special syntax.
This is necessary to add if you use any unique letters from other languages or special symbols.
If you forget to add it and you have a special character in your script you will get an error like shown on the left, so it’s always worth to include it.
Button's Title and Description
Next we usually provide name and description for our button. We use __title__ and __doc__ variables so python can automatically detect them and display in Revit UI.
💡You can also get creative in your description. I like to separate it in multiple sections so it's much more readable and organized. Feel free to adjust to your own needs.
Other Meta Tags
There are also multiple meta tags available for pyRevit scripts that can provide additional functionality.
__author__ / __authors__ - Show Author's name in the button's description. Personally I prefer to write it in my __doc__ variable to save some space.
__helpurl__ - We can provide any link to a website or path to a file and it will be opened when we hover over the button and click F1. Can be useful to provide additional documentation on how to use button.
Revit Versions - We can limit our script to certain Revit version using:
__min_revit_ver__
__max_revit_ver__It’s very useful when you use new Revit API classes and methods which are not available in older versions used differently.
e.g. We couldn't create Ceilings before Revit 2022 with Revit API, so it's wise to restrict the button to Revit 2022+.__beta__ - This one will hide your button from Revit UI, but you could still access it with shortcuts. I would recommend creating a separate extension for development and only bring complete tools to your actual toolbar. It will give you more space to keep buttons in development and test your code.
__highlight__ - We can highlight our buttons as 'new' or 'updated', it will look like screenshot below in Revit UI and can draw extra attention from your users.
__context__ - Context is great when your tools are made for specific views or certain elements have to be selected before you run the button.
💡 It even has it’s own Bundle Context page, with more examples.
Imports
To be able to access Revit API classes, we will need to add a few imports. Add most used imports in your template, you can always remove them once your script is complete.
💡My example is a little overkill, so feel free to delete unused imports.
💡Check out pyRevit's site-packages to see what packages are available by default.
Variables
There are multiple variable that are needed in every script that we are going to write, so include all of them in your template so you don't have to worry about it.
Functions and Classes
To be honest, I don't think we need any Functions or Classes in our template file.
💡 If you think you need a function or a class that is going to be reused so often, it's much better to place it to a reusable library of snippets and then import it.
🎦 We will cover that in the next lesson.
Main
Lastly, we can prepare our main section if you think you need something.
Customize to Your Own Needs
My template is just an example. If you are new feel free to use it, but make sure you modify it to your own needs over time.
FigLET Plugin
When I code you will often notice comment blocks that I create for marking different sections of my code. I use FigLET plugin for creating these blocks.
👇 They Look like this
💡 I haven't seen anyone else do that, it's just something that I started doing in the beginning to write more readable code for myself and others. You can decide yourself if they are helpful or not.
Install FigLET
To install the plguin you need to open pyCharm's plugins window.
File -> Settings -> Plugins -> Marketplace -> Search 'FIGlet'
💡When you install it, you might get the following Notice from JetBrains. There is nothing to worry, you can Accept it.
Use FIGlet Plugin
To use FIGlet, select text which you want to convert and click ALT + Instert and select ASCII Art…
I use Calvis S font to generate blocks you see in my videos. You can explore other fonts as well.
Template from Video
Template_min from Video
HomeWork
💡 Creating a pyRevit script Template is a very important step. You will use it to start all your new tools.
✅Follow the video to create pyRevit Template
✅Modify Template to your own needs if necessary
⌨️ Happy Coding!