Unable to access view template name for a specific view

Hello,
I am trying to generate a tool that iterates through the views of a project and for those that have a specific View Template prefix in the name, do something with the room tags.

My problem is that I am unable to obtain for a view, the view template name as a string in order to compare it to my list of prefixes.

Below my code, and the error.
Do I need any other method to obtain the name of the view template as a string?

import Autodesk.Revit.DB as DB

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

# Gel all floor plan views
all_floor_plan_views = DB.FilteredElementCollector(doc)\
    .OfCategory(DB.BuiltInCategory.OST_Views)\
    .WhereElementIsNotElementType().ToElements()

# Define the view template name prefixes to process
view_template_prefixes = ["BSE_03_PAP_"]

if __name__ == '__main__':

    t = DB.Transaction(doc,__title__) 
    t.Start()
    # Loop through all floor plan views
    for view in all_floor_plan_views:
        if view.ViewType == DB.ViewType.FloorPlan:
            view_template_name = DB.Element.Name.__get__(doc.GetElement(view.ViewTemplateId))

            # Check if the view template name starts with any of the specified prefixes
            if any(view_template_name.startswith(prefix) for prefix in view_template_prefixes):
                # Get all MEP Space Tags in the current view
                space_tags = DB.FilteredElementCollector(doc, view.Id) \
                    .OfCategory(DB.BuiltInCategory.OST_MEPSpaceTags) \
                    .WhereElementIsNotElementType().ToElements()

                # Loop through all MEP Space Tags
                for tag in space_tags:
                    # Get the associated MEP Space element
                    space = doc.GetElement(tag.GetSpace())

                    # Position the tag at the same location as the space's reference point
                    tag.Location.Point = space.Location.Point

                # Print a message for the processed view
                print("Processed view: " + view.ViewName)

    # Print a completion message
    print("Script completed successfully.")
    t.Commit()

But for the line

if any(view_template_name.startswith(prefix) for prefix in view_template_prefixes):

I receive the following error

line 86, in <module>
AttributeError: 'getset_descriptor' object has no attribute 'startswith'
1 Like

@TaytaRD

i found a bit time…

the methode startswith() works well. Here an example

uidoc = __revit__.ActiveUIDocument
doc   = __revit__.ActiveUIDocument.Document


# πŸ›’ Get All ViewTemplates
all_views_and_vt = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views)\
                .WhereElementIsNotElementType().ToElements()
# 🎯 get viewtemplate by name
viewName = [i for i in all_views_and_vt if i.Name.startswith("Querschnitt")]

# βœ… check result
for i in viewName:
    print("ViewName: {}".format(i.Name))

all_views = [v for v in all_views_and_vt if not v.IsTemplate]
all_vt    = [v for v in all_views_and_vt if v.IsTemplate]

# ❗ info general
print('\n There are {} ViewTemplates.'.format(len(all_vt)))
print('\n There are {} Views.'.format(len(all_views)))

This message tells you that the view_template_name variable is not a string.

This is the cause of the problem. Just out of curiosity, how did you come up with this?

The right way to get the name of an element starting from its Id is:

doc.GetElement(view.ViewTemplateId).Name

To make it clearer:

view_template = doc.GetElement(view.ViewTemplateId)
view_template_name = view_template.Name
1 Like