Can anyone please explain what is the right way to start working in pyRevit environment. I mean what “must have” libraries I need to import to start working?
I tried to study various scripts and everyone uses different methods.
Some people use clr. Do I need to import this when working in pyrevit?
import clr
clr.AddReference(“RevitAPI”)
Some people do this which does not make sense.
from Autodesk.Revit.DB import Workset, Transaction
(this line makes sense as a person tries to create worksets and record transaction so it can be undone).
from Autodesk.Revit.DB import *
(then they do this which does not make sense. They have already imported what they need and now they import everything?).
If I import the above do I need to import this:
from pyrevit import revit, DB
Also, when it comes to Transaction:
Is that okay if I use standard RevitAPI transaction with start() and commit()?
I noticed that some users simply use with revit.Transaction().
Let’s say, I don’t import something that I need for my script from Revit API, will pyRevit give a warning / error to stop a script? Or there is a chance that Revit model gets corrupted?
Last question: when I import the below, does it import everything? Or when I call on Revit API objects, let’s say DB.Workset, it only imports whatever I need (Workset Class in this instance)?
The reason I’m asking is that some say that we need to try to avoid importing everything (import * ).
Why you shouldn’t use import * has to do with a thing called scope.
I’m not going to go too deep into what that means so here’s an analogy.
Imagine you’re working on a lego project, and you have a box that stores all your bricks. (DB)
You are working very clean, and have sorted out all the bricks with different colors and shapes into different boxes inside your bricks box.
If you use a import like so: from Autodesk.Revit import DB
You’re telling python to look inside the bricks box and get whatever you need.
If you use an import like so: from Autodesk.Revit.DB import *
It’s more like this