Mass Duplicate View revit crash

Hello, i am pretty much without a clue here.

My problem is that i want to duplicate views from a revit file.

It generally works pretty fine, but now i have a revit file where the script just stuck.

Even the except doesn’t work, probably because it does not through an error.

do you guy hav an idea what could be the cause and why the duplicate function is not working?

I would be very thankfull

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import *
import Autodesk

clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
Schedules = UnwrapElement(IN[0])
filepath = IN[1]

#Option for Duplication Duplicate / WithDetailing / Dependent

#Option = Autodesk.Revit.DB.ViewDuplicateOption.Duplicate
Option = Autodesk.Revit.DB.ViewDuplicateOption.WithDetailing
#Option = Autodesk.Revit.DB.ViewDuplicateOption.Dependent

Building = list()
j=0
TransactionManager.Instance.EnsureInTransaction(doc)

for Apartments in Schedules:

Apartmentlist = list()
for ScheduleApartment in Apartments:
	with open(filepath, 'a') as file:
		file.write(str(ScheduleApartment.Name))
		file.write("\n")		
		file.close
		j=j+1
	try:
		newSchedule = ScheduleApartment.Duplicate(Option)
		Apartmentlist.append(doc.GetElement(newSchedule))
	except:
		Apartmentlist.append(None)
Building.append(Apartmentlist)

TransactionManager.Instance.TransactionTaskDone()

So I’m narroing my analysis and it is due to the duplicate method from the Revit API.

Not knowing if it is related, I identified Views that are nul

image

How are such Views possible?

and can this be the reason why it is getting mixed up while duplicating a lot of views?

I think the nuls are View Templates and the Browser organization (arborescence).

If you use the OOTB node Views, you can find in the list “Project View” and “System Browser”.
These are probably the nulls.