Commenting code out causes python to fail

I’ve taken some python code from Modelical that deletes all unused view templates in a file. I want to control which view templates are tested so I can keep certain unused view templates.

First I edited a copy to create a new python script that collected all view templates in the file. This works.

But when I comment out the relevant code in the python that deletes the view templates it fails.
If I feed the script the results of the collector and leave the code in I can get it to return both lists - that fed to it, and the list it creates. They are both identical.

But when I comment out the internal code the whole script fails. I don’t understand why. Is there some obtuse formatting that I’m missing?

Result with both collector and internal lists:

Result after commenting out internal code:

The code (with internal collector code commented out):

  # This node has been made by Modelical
# www.modelical.com
# Edit by Antony McPhee 19/10/17
# Input requires list of view templates

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
from RevitServices.Persistence import DocumentManager

#The inputs to this node will be stored as a list in the IN variables.

bool = IN[1]
viewTemplatesInput = IN[0]
#viewTemplatesInput = IN[0]
output = "Set Toggle to TRUE to delete unused View Templates"

doc = DocumentManager.Instance.CurrentDBDocument
collector = FilteredElementCollector(doc)
allviews = collector.OfClass(View).ToElements()
viewlist = []

for v in allviews:
	if v.ViewType == ViewType.ThreeD:
		if not(v.IsTemplate):
			viewlist.append(v)
	else:
		viewlist.append(v)

views = UnwrapElement(viewlist)

viewTemplates = viewTemplatesInput

#viewTemplates = []
#for v in views:
#	if v.IsTemplate:
#		viewTemplates.append(v)
#

usedTemplates = []
for v in views:
	if v.ViewTemplateId.IntegerValue != -1:
		usedTemplates.append(v.Document.GetElement(v.ViewTemplateId))

uniqueUsedTemplates = []

if len(usedTemplates) != 0:
	uniqueUsedTemplates.append(usedTemplates[0])
	for used in usedTemplates:
		buffer = False
		for unique in uniqueUsedTemplates:
			if used.Name == unique.Name:
				buffer = False
				break
			else:
				buffer = True
		if buffer:
			uniqueUsedTemplates.append(used)

indexes = []
i = 0
for v in viewTemplates:
	for u in usedTemplates:
		if u.Name == v.Name:
			indexes.append(i)
			break
	i = i + 1

indexes.reverse()

for i in indexes:
	viewTemplates.pop(i)

viewTemplatesID = []
viewTempNames = []

if bool == True:
	for v in viewTemplates:
		viewTemplatesID.append(v.Id)

	TransactionManager.Instance.EnsureInTransaction(doc)

	for v in viewTemplatesID:
		viewTempNames.append(doc.GetElement(v).Name)
#		doc.Delete(v)

	TransactionManager.Instance.TransactionTaskDone()
	output = viewTemplatesInput,viewTemplates
#	output = viewTemplates


OUT = output

Looks like you mixed up your inputs: bool should be IN[0] and viewTemplatesInput should be IN[1]

I switched inputs so it should make no difference. Unless in python IN[0] has to come before IN[1]?

I found two errors, the first is that de viewtemplates aren’t unwrapped on line 20, so that should be:
viewTemplatesInput = UnwrapElement(IN[0])

and the second error I can’t really explain, but for some reason pop on line 78 doesn’t work, but you can replace that with RemoveAt, like so:

for i in indexes:
	viewTemplates.RemoveAt(i)

Thanks T_Pover. UnwrapElement worked.

RemoveAt(i) didn’t work, as it is a C+ function, not a python one (might have worked with right include).
I also tried del(i), but in the end pop(i) worked anyway.

Here is code for collecting View Templates, someone might find it useful. Keep in mind it doesn’t collect 3D view templates (when are they going to fix that?):

# Antony McPhee 20/10/17
# Collects all view templates, except 3D view templates

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
from RevitServices.Persistence import DocumentManager

#The inputs to this node will be stored as a list in the IN variables.

bool = IN[0]
output = "Set Toggle to TRUE to delete unused View Templates"

doc = DocumentManager.Instance.CurrentDBDocument
collector = FilteredElementCollector(doc)
allviews = collector.OfClass(View).ToElements()
viewlist = []

for v in allviews:
	if v.ViewType == ViewType.ThreeD:
		if not(v.IsTemplate):
			viewlist.append(v)
	else:
		viewlist.append(v)

views = UnwrapElement(viewlist)

viewTemplates = []
for v in views:
	if v.IsTemplate:
		viewTemplates.append(v)

viewTemplatesID = []
viewTempNames = []

if bool == True:
	for v in viewTemplates:
		viewTemplatesID.append(v.Id)

	for v in viewTemplatesID:
		viewTempNames.append(doc.GetElement(v).Name)
#		doc.Delete(v)

	output = viewTemplates, viewTempNames


OUT = output
1 Like