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.

# CODE
# 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:
			all_viewports_flat.append(v)
	else:
		all_viewports_flat.append(view)
		
# Get Views
all_views = []
for viewport in all_viewports_flat:
	all_views.append(viewport.GetViews())

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
clr.AddReference("RevitAPI")

#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

clr.AddReference('RevitServices')
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
	views.append(doc.GetElement(viewId))


OUT = viewports,views
5 Likes

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

2 Likes

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:

3 Likes

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