Begginer to Python: 'FilteredElementCollector' is not defined Error

Hi.

I’m trying to follow the PyRevit tutorial.
In the first example he tries to show how to collect all elements of the wall category from within the Interactive Revit Python Shell. I have already configured the RPS startup script to be the file this file he offers in the course: https://gist.github.com/eirannejad/4e275166bf2015f4f844bb1d087ca4d6
For some reason though, I get this error. Please see image attached:

Thanks for the help in advance.

Tom.

Just to check if your startup script is working,
Did you check just typing >>> el
did that returned all the selected items if <5 ?

Hi Saju.
It works. When I select elements and type “el” I see that the first element is selected.

please add the below lines into your code at the beginning and check.

import clr
clr.AddReference(‘RevitApi’)
from Autodesk.Revit.DB import *
clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

OK. It worked once, but now It’s giving me this error:

experts will reply. Please close and open the console and try

OK. I just needed to import the DB library where the FilteredElementCollector class is found.
from Autodesk.Revit import DB

1 Like

thank you brother.
had the exact same issue.
i would like to understand why this happened? is it possible that the init script got somehow corrupted and it didn’t read the imports?

Ok, I found out to anyone interested: The correct init script that you need is in the ‘revisions’ page, here:

This one will import everything in the Autodesk.Revit.DB Namespace.
Thanks @eirannejad for the great tutorials! :slight_smile:

Thanks. Ya just use this init script with RPS and then use the DB namespace to access all DB types like DB.FilteredElementCollector

Generally importing all types into your local script namespace is a bad idea and takes execution time and also creates name conflicts. e.g. from Autodesk.Revit.DB import * is bad

2 Likes

Hello,

How you did that ?
Thank you in advance!

Hi,

The same error, with the latest init code.
Did anyone have a solution? Many thanks.

Exception : IronPython.Runtime.UnboundNameException: name 'ElementCategoryFilter' is not defined

I find that Dynamo is a better debugging platform for pyRevit Python than the python shell.
Just put the declarations in your Dyanmo python template and run the script in Dynamo for testing. When you have everything working - just copy/paste it all out to a *.py file and away you go.
Note that I’ve referenced both Dynamo and pyRevit so I can use either (Dynamo is a bit of a pain because it needs to be initialized first)

sys.path.append('C:\Program Files\pyRevit-Master\pyrevitlib')
sys.path.append('C:\Program Files\Autodesk\Revit 2020\AddIns\DynamoForRevit')

#Use this for pyRevit. Ok to use inside Dynamo as well if pyRevit is referenced.
#doc = __revit__.ActiveUIDocument.Document
#app = __revit__.Application
#uidoc = __revit__.ActiveUIDocument
#uiapp = __revit__.CurrentUIApplication 

#Dynamo - I'll use this if the end product is going to be a dyn rather than a py.
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument