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