QuickStart in the Realm of pyRevit and the Revit API

Hello everyone!

This forum is a fantastic place to connect, learn, and troubleshoot together. To ensure that we make the most of this community, let’s go over some guidelines that will make seeking and offering help much smoother:

  1. Search First: Before posting a new question, take a moment to search the forum. Someone might have already encountered a similar issue and found a solution.
  2. Provide Details: When asking for help, be sure to include your code and configuration settings. Generic descriptions or error messages aren’t enough for us to pinpoint the problem accurately.
  3. Use Code Blocks: When sharing code snippets or file contents, wrap them in triple backticks (```) to maintain formatting and readability.
  4. Mark Solutions: If someone provides a solution that solves your problem, please mark their message as the solution. This helps others quickly identify what worked.

Let’s keep the collaborative spirit alive by following these simple guidelines. Happy coding and problem-solving!

Good Places to Look For

For those seeking resources to enhance their pyRevit skills, here are some valuable sources to explore:

.Net/C# to IronPython/CPython

Transitioning from .Net/C# to IronPython/CPython might feel tricky, but here are some tips to ease the process:

  • You don’t need to declare variable types explicitly; just assign the value.
  • Unlike in C#, you don’t need the new keyword when creating class instances.
  • in C#, variables and method arguments are in camelCase, while in Python, the preferred style is snake_case. This is not an obligation, but helps make the code readable for everybody. Note that this applies to objcets created by you in your code, of course revi API objects needs to be accessed with the exact case you find in the reference.
  • Python modules (files) should also be named in snake_case.
  • Most of the time, you can directly use Python lists without converting them to typed lists using List[type](python_list).

Dynamo to pyRevit

For those transitioning from Dynamo to pyRevit, here are some helpful resources:

Keep in mind:

  • See the section below
  • Note that the UnwrapElement function is not needed in pyRevit, as you directly access Revit objects.
  • Watch out for indentations! Dynamo script editor uses tabs, while common IDEs (vscode, pycharm and so on) converts tabs to spaces. And python likes to have the same type of whitespace, so make sure they are all of the same tyoe when copy-pasting

Overlooked pyRevit Python Package Functionalities

In case you missed them, here are some useful things provided by pyRevit’s Python package:

  • No need to import clr or add references to the Revit API; many objects are already loaded by pyRevit and available to you, just import pyrevit.revit.
  • Access the current document easily with pyrevit.revit.doc.
  • Utilize pyrevit.revit.query functions for element searching; the module provides easy access to DB.ElementCollector methods.

Common Pitfalls/Known Issues

Here are some common issues to watch out for:

  • CPython support is buggy. While we’re trying to update the compatibility layer (pythonnet) to the latest version before we can address pyrevitlib bugs.
  • Python 2.x doesn’t handle UTF-8 script files by default, you need to place # -*- coding: utf-8 -*- at the beginning of each file;
  • python 3 defaults to UTF-8, so there’s no need for that line, but for pyRevit to recognize the python version to run, you have to use #! python3
  • Metadata variables at the beginning of the script are deprecated in favour of bundle.yaml files - this is not an issue per se, but the problem arises when one uses both of them for the same script. To be on the safe, future-proof, side, just use bundle.yaml!
  • If you want to share your extensions with others, prefer a versioning system such as git instead of putting your code on a shared cloud drive: client apps treat files and paths in an unpredictable manner, and can lead to pyrevit erroring out while trying to load the extension.

Python Libraries Shipped with pyRevit

Don’t miss out on the convenience of these libraries:

  • rpw - Revit Python Wrapper

Useful Tools

Here are tools that could prove invaluable:

from rpw.ui.forms import Console


Some Advice

A few words of wisdom:

  • If you find a tool similar to what you’re trying to achieve, ALT+Click on it and explore the code in the script.py file to understand the solution.
  • Break down your problem into smaller tasks and tackle them one by one. This approach can make complex tasks much more manageable.

Let’s learn and grow together! If you have questions or insights, don’t hesitate to share. Happy coding!

Note: Thanks to @sanzoghenzo for the heavy lifting :point_up_2: