import sys
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
doc = DocumentManager.Instance.CurrentDBDocument
name = IN[0]
elem = []
views = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements()
for v in views:
if v.IsTemplate:
if v.Name == name:
elem.append(v)
OUT = elem[0]
You can use Python like the following to get the template names:
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import*
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
views = FilteredElementCollector(doc).OfClass(View3D)
viewTemplates = []
for v in views:
if v.IsTemplate == True:
viewTemplates.append(v.Name)
OUT = viewTemplates
To get the templates themselves you’ll have to fiddle around with element wrappers. I believe there is a custom node tucked away somewhere that’ll do it but can’t recall the package. Edit: try Springs, but no promises.
To add to this and clarify why that node works, the issue here isn’t API related in Revit but in Dynamo specifically. You can get and use/access 3D view templates in Python/C# etc but once they go to canvas they return nulls so generally most nodes exclude them unless they use them all within a Python/C# node (e.g. get by name, which is a good workaround).
As @GavinCrump points out, it’s a limitation within Dynamo as there’s no included wrapper for them. You either need to keep all workings related to the template within Python, or get their Ids or names and work with those within the Dynamo graph as the element itself willjust reports nulls inside the graph environment. I did have a check and it is Springs that has a node for them, it appears he’s written a custom wrapper of some sort in there to handle them. (Excuse my poor terminology and wait for the big boys to respond if you want more accurate details, that’s the gist of it anyway).
I think I solved it; based on Springs ꟿ Collect.View3DTemplates
import clr
clr.AddReference('RevitAPI')
import Autodesk.Revit.DB as DB
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
name = IN[0]
ueWrapper = None
wrappers = clr.GetClrType(Revit.Elements.ElementWrapper).GetMethods()
for w in wrappers:
if w.ToString().startswith("Revit.Elements.UnknownElement"):
ueWrapper = w
break
fec = DB.FilteredElementCollector(doc).OfClass(DB.View3D)
OUT = []
for i in fec:
if i.IsTemplate and i.Name == name:
OUT.append(ueWrapper.Invoke(None, (i, True) ))
break
OUT = OUT[0]