Getting Element from a list by a specific name with API

Hi All,

I’m trying to retrieve an element from a list by specifying its name, but I’m encountering the following error:

What is strange is that I can append the Names list with item names by typing the code Names.append(shaft.Name) , as shown in the output below. However, I cannot retrieve the specific item I’m looking for using the if statement ( see the 2nd image and the code below ) !?

all_Shafts = FilteredElementCollector(doc).OfCategory(BuiltInCategory. OST_StructuralFraming).WhereElementIsNotElementType().ToElements()

for shaft in all_Shafts:
    if shaft.Name == Shaft2:
        final_shaft = shaft
    else:
        final_shaft = all_Shafts[-1]
        
OUT = all_Shafts, final_shaft

Thanks in advance for your help.

hello
if shaft.Name == 'Shaft2': # shaft.Name is a string and 'Shaft2' is a string Shaft2 is a variable
cordially
christian.stan

2 Likes

@christian.stan

Oh, my bad!.. Your correction is logical. I forgot to put commas to be able to compare string to string!

Your correction solved my issue.

Thanks.

1 Like