Get View From Viewport_Python

Hi guys

I am trying to get the views from a list of viewports using Python. I am struggling to find the method that does this. Appreciate any help. See my attempt below.

# Get Viewports on Sheet
all_viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()
all_viewports_flat = [ ]
for view in all_viewports:
	if isinstance (view,list):
		for v in view:
# Get Views
all_views = []
for viewport in all_viewports_flat:

I have a node in Rhythm that gets the view for a viewport, that source code is here:

But, to recreate something similar in python it would be as follows:

import clr

#import the specific Revit API sections we need, you can also just do import* if you want
from Autodesk.Revit.DB import FilteredElementCollector,BuiltInCategory,Viewport, View

import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()

views = []

for viewport in viewports:
	viewId = viewport.ViewId

OUT = viewports,views

Thanks so much!
Yes I do have Rhythm downloaded but I’m trying to force myself to complete full scripts in Python. Much appreciated.


For sure! Learning python is great! I mentioned Rhythm more because it is open source and a lot of the code is fairly easy to translate. :slight_smile:


Is there anything for the reverse of this?

You mean to get the viewports from the views? Or are you referring to crop regions?

Yup to get viewports from views :slight_smile:

Can you provide some context so I can understand your goal?

Sorry dont worry about it, found out its something else

Take a look here:

c.poupin came up with a collector to iterate all viewports (I.e. Views on sheets) and it was rehashed to ID legends not on shets for deletion.

1 Like