am trying to set up a simple script to automate the export of NWC of specific Views filtered by name. I keep getting the error on my Python Script block in Dynamo when I try to run it:
NameError: name ‘FilteredElementsCollector’ is not defined [‘ File “< string >”, line 22, in < module > \n’]
See attached dynamo script and python code. What am I missing?
You haven’t imported the Revit API, so it won’t find filtered element collector as a class. Typically this means you’ve got the wrong boilerplate/template.
Check the Dynamo Primer section on Python and Revit for more info on why all this matters, but basically Python doesn’t know how to access the Dynamo API, Revit API (or any other API for that matter) by default and so the ‘import’ statements at the top have to be included and customized to what you want to do with the Python.
You can get some more functional templates than the OOTB one in the Dynamo Primer, @solamour’s Dynamo Python repository, the Dynamo Python Primer by Oliver Green, and other places online. There is no one ‘perfect’ setup - what works for person A might not be right for person B. You’ll need to learn how to manage your own code so it works for you.
And as an added holiday bonus (), here is my Python template for Dynamo for Revit:
########################################
############## Properties ##############
########################################
__author__ = 'First Last'
__version__ = '0.0.0'
__description__ = ""
__RevitBuilds__ = "20xx.y"
__DynamoBuilds__ = "X.YY.Z"
__ReleaseNotes__ = ""
__Dependancies__ = ""
__Copyright__ = ""
__License__ = ""
########################################
### Configure the Python environment ###
########################################
### standard imports ###
import sys #add the sys class to the Python environment so we can work with the sys objects
import clr #add the CLR (common language runtime) class to the Python environment so we can work with .net libraries
import math #add the Math class to the Python environment so we can do math stuff
import random #add the Random module to the Python environment so we can impersonate a squirrel
### basic Dynamo imports ###
clr.AddReference('ProtoGeometry') #add the Dynamo geometry library to the CLR
from Autodesk.DesignScript import Geometry as DG #add the Dynamo geometry class using the alias DG, to ensure no overlap between class calls in Revit or Dynamo
clr.AddReference('DSCoreNodes') #add the standard Dynamo library to the CLR
import DSCore # import the standard Dynamo classes to the Python environment
clr.AddReference('GeometryColor') #add the Geometry color library to the CLR
from Modifiers import GeometryColor #import the GeometryColor class to the Python environment
### Dynamo Revit imports ###
clr.AddReference("RevitNodes") #add Dynamo's Revit nodes library to the clr
import Revit #import Dynamo's Revit node class
clr.ImportExtensions(Revit.Elements) #add the element conversion methods to the CLR
clr.ImportExtensions(Revit.GeometryConversion) #add the geometry conversion methods to the CLR
clr.AddReference("RevitServices") #add the Revit services library to the CLR
import RevitServices #import the Revit services class to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment
from RevitServices.Transactions import TransactionManager #import the transaction manager class to the Python environment
### Revit API imports ###
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #add the Autodesk class to the Python environment
from Autodesk.Revit.DB import * #import every class of the Revit API to the Python environment
### trade specific Revit imports ###
from Autodesk.Revit.DB.Architecture import * #import every class of the Revit architecture API to the Python environment
from Autodesk.Revit.DB.Structure import * #import every class of the Revit Structure API to the Python environment
from Autodesk.Revit.DB.Plumbing import * #import every class of the Revit plumbing API to the Python environment
from Autodesk.Revit.DB.Electrical import * #import every class of the Revit electrical API to the Python environment
from Autodesk.Revit.DB.Mechanical import * #import every class of the Revit mechanical API to the Python environment
### .net imports ###
clr.AddReference("System") #add the .net System library to the CLR
from System.Collections.Generic import List as NetList #imports the .net list module to the Python environment as the alias NetList
#elementIds = NetList[ClassHere](ListOfObjectsHere) #how to build a .net list from a python list of objects
#########################################
######## Classes and Definitions ########
#########################################
class ClassName: #sample class
def Example(input): #sample definition
#do things here
return input #return the result
#########################################
###### Global variables and inputs ######
#########################################
### documents and standard variables ###
doc = DocumentManager.Instance.CurrentDBDocument #the current Revit document
uiapp = DocumentManager.Instance.CurrentUIApplication #the current user interface application
app = uiapp.Application #the current Revit application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument #the current UI document
### imports and unwrapping ###
elems = UnwrapElement(IN[0]) #import the elements from IN[0] of the Dynamo environment and convert to native Revit elements
if not elems.__class__ == [].__class__ : elems = [elems] #ensure that elems is a list, not an individual object, so that we can always prepare for a loop
#########################################
############ Code goes here #############
#########################################
### section 1 ###
TransactionManager.Instance.EnsureInTransaction(doc) #start transaction
variable1 = 1
TransactionManager.Instance.TransactionTaskDone() #end transaction
#########################################
##### Return the results to Dynamo ######
#########################################
OUT = variable1