The pyRevit IronPython Script Library

Two weeks ago, Maltezc raised a question on the availability of a version of RevitLookup for Python in the Revit API discussion forum thread on RevitLookup for Revit 2016.

I am not aware of any Python version of RevitLookup, but you can certainly call into RevitLookup from RevitPythonShell.

In his question, Maltezc pointed out the pyRevit IronPython script library that I was previously unaware of.

A chat with Ehsan Iran-Nejad, pyRevit creator and maintainer, led to him writing a blog post describing this powerful and popular collection, reproduced below in full after a couple of minor updates.

I hope you find this as interesting and useful as I do.

Here is Ehsan's introduction to the package:

Introduction to pyRevit

Question: I'm an architect and engineer and love coding. Unfortunately, I don't have the time and experience to code in complex languages that require special coding environments (e.g. Visual Studio) or need to be compiled and reloaded after each change. I therefore like scripts. I can create or modify them for a highly specific task, in the least amount of time, and get the job done. I want to learn how to use IronPython for Revit and I need examples. Do you know a good resource for that?

Response: Yes, definitely!

Take a look at pyRevit.

pyRevit is an IronPython script library for Revit. However, it is not really written as an example library. It is a working set of tools fully written in IronPython that explores the power of scripting for Revit and also adds some cool functionality.

Download and install it, launch Revit and you will note the new pyRevit tab that hosts buttons to launch all the scripts provided by the package to easily run them without the need to load them in RevitPythonShell or some similar IronPython console.

You can also write your own scripts and add them to the tab.

There is even a Reload Scripts button than dynamically adds the new scripts to the current Revit session without the need to restart Revit.

All the scripts are provided in the pyRevit folder which is downloaded at installation. You can look into them and learn how to use IronPython for Revit to perform different tasks.

Please refer to the pyRevit GitHub repository for links and instructions on how to install on your machine.

Quick Look at some pyRevit Scripts

Let's take a quick look at some of the more useful scripts in this library:

Selection Memory

A couple of scripts help you select object more efficiently in Revit. They are similar to the M+, M-, buttons on calculators where you can add or deduct values from memory and read the final value using the MR button.

Under the pyRevit tab, you'll find MAppend, MAppendOverwrite, MRead, MDeduct, and MClear buttons that add, add and overwrite, read, deduct, and clear the contents of the selection memory. Using these tools, you can navigate between multiple views and select objects, add them to the memory and when you're done, recall the selection. These tools work really well in combination with other selection tools under pyRevit tab. See images here for the tools and tooltips.

Each tooltip shows the button name, the script that the button is associated with and a description of what the script does.

Memory append overwrite

Memory read:

Memory read

Other selection memory utilities:

Other selection memory utilities

Copy and Convert Legend Views

This set of scripts help you copy Legend Views to all other project open within a Revit session. You can copy the Legends as Legend views or as Drafting views.

Copy legends

Two more scripts duplicate and convert Legend views to Drafting views and vice versa within the same project.

Convert legends

Matching Element Graphic Overrides

This one is pretty obvious. Run the script, select your source object to pick up the style, and then one by one, select the destination objects to apply the graphic overrides. You can also navigate to other views and apply to objects within that view.

Match graphic overrides

pyRevit provides many other powerful scripts, and most of them are really useful in a production environment.

Analyse and modify pallete

Project palette:

Project palette

Desktop palette:

Desktop pallette

An Even Quicker but Deeper Look at Setting up pyRevit

Now let's take an even quicker and slightly deeper look at setting up pyRevit:

In it's simplest form, it's a folder filled with .py IronPython scripts for Revit.

pyRevit folder

Since Revit itself does not provide an IronPython console, you need RevitPythonShell to run them.

RevitPythonShell console

Let's say you have written a script that automatically designs amazing buildings and creates the Revit model and construction documents for it, and let's say you want to run this script as fast as you can and make a whole buncha money really quickly, but it takes time to open the command prompt every time, browse to the script file, open it and run it, so you naturally want something faster!

In order to make pyRevit more user friendly, it includes a helper script that finds all the other scripts and creates buttons for them in the Revit user interface. This way. you can just click on the buttons instead of using the command prompt.

This script is appropriately called __init__.py and lives in the pyRevit library root folder.

pyRevit _init_ script

What's neat about this is that the user interface buttons only store the address to each script. The script is reloaded and run every time the user clicks on the button.

This means that you can change a script on the fly while Revit is running, and the next time you click on the button, Revit will run the modified script.

But how do you tell Revit to run this script during start-up?

There are two ways to achieve this:

If you'd like to find out more about pyRevit and how to add your own scripts, visit the pyRevit GitHub home page and everything you want to know about it is provided.

Happy scripting!

Many thanks to Ehsan for creating, sharing, maintaining, and documenting pyRevit with and for us all!

pyRevit Overview, Installation and Tutorial Video

Neil Reilly created a 26-minute pyRevit overview, installation and tutorial video on it:

Many thanks to you too, Neil!