Unscriptable lists?


I’m attempting to create a Python routine running in Dynamo which creates room elevations (I appreciate that there are several available on the Internet - I want to write my own as a learning exercise).

So to create a Elevation Marker I need to specify a viewFamilyTypeId:

Type: Autodesk.Revit.DB.ElementId
This ViewFamilyType will be used by all elevations hosted on the new ElevationMarker.

So in Python I’ve used a Filtered Element Collector to find these:

viewfamilies = FilteredElementCollector(doc).OfClass(View)

When I run the routine Dynamo outputs a list of views:

So this looks to have worked.

But when I try to pick out a view in Python with, say, viewfamilies[1] I get the error:

TypeError: ‘FilteredElementCollector’ object is unsubscriptable

Any advice on whats going wrong? Its scriptable when outside Python in Dynamo as its referred to as a list and I can pick out items, but in Python it apparently isn’t…

What gives?

just do FilteredElementCollector(doc).OfClass(View).ToElements() to convert that collector to IList which then can be queried with list syntax [0].

Ps. Keep in mind that a collector.OfClass(View) will return all views in a model. A view class is a superclass that encompasses a lot of things like project browser and view templates. You might want to narrow down that selection.


Great thanks.

What would be the best way of narrowing the search down?

What are you trying to select exactly? Consider using collector.OfCategory() and you can combine it with slow filters. Have a look in the SDK for examples on slow filters.

I’m trying to write a python routing quick generates elevations, the sdk says need to give a viewFamilyTypeId as part of the class which generates them, so I’m trying to query the active document to see which ones are present and then use over of those.

Is there a better way to do this?

To get a view family type just use this:

Kevin, general forum etiquette comment: you asked about a specific error in Python, it was answered. Please start a new thread if you have other questions. This keeps the topics relevant to what the question+answer content of the thread is. Otherwise, this conversation can go on forever and in many different directions.

OK, no problem. Thanks for your help.

Hi Konrad,

The Python script with ToElements() and the Dynamo graph both fail with the same error: ‘View cannot be a template view’.

I tried this:

fec = FilteredElementCollector(doc).OfClass(View).ToElements()

I don’t really see the relevance of templates in the error, but I’m thinking this is also to do with the issue in my initial post where I cannot obtain elements from a list.

Looking at the FilteredElementCollector members it seems that you can use an iterator to access the individual objects, I guess you have to use that method to obtain them.

Would you agree?

Kevin, again, new thread please.

Like I mentioned above View is a superclass that many things inherit from. Have a look at this tree:

Now, View Template is a View that return true for view.IsTemplate check. You can iterate over the FilteredElementCollector (yes, its a collection and for most part it behaves like a list in Python) and check each element for whether its a View Template.

Something like this should do:

allViews = FilteredElementCollector(doc).OfClass(View).ToElements()
for view in allViews:
    if view.IsTemplate:
        # do something with view template
        # do something with other view types