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:
- 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.
- 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.
- Use Code Blocks: When sharing code snippets or file contents, wrap them in triple backticks (```) to maintain formatting and readability.
- 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:
- Developer Docs
- pyrevitlib Documentation - Note that not everything is documented here yet - we’re working on it!
- Official Readme
- Learn Revit API and ErikFrits YouTube Channel
- PyRevit YouTube Channel for more advanced content
- Danny Bentley YouTube Channel with excellent foundational material
- @Jean-Marc 2022 Lab in the handout section
- Revit Python Shell Samples
- Useful insights from Gavin Grump
- Refer to Revit API Docs or apidocs.co
- fresh from the press, @Erik Fritz presentation at the bim coordinator summit last week 7 steps - Revit API
.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 issnake_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:
- Dynamo vs pyRevit article from Learn Revit API
- Explore the Python Primer for Dynamo
- Check out well-commented sample scripts here
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 importpyrevit.revit
. - Access the current document easily with
pyrevit.revit.doc
. - Utilize
pyrevit.revit.query
functions for element searching; the module provides easy access toDB.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:
- IronPython Stubs for autocompletion in your favorite IDE
- Check out the PyCharm Guide
- Revit Lookup to inspect the revit database
- Revit Python Shell - although you can create an interactive console with a simple pyRevit script
from rpw.ui.forms import Console
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