Variable as property?

Is it possible to pass a property as a variable?

vt = [i.by for i in collectedViewTemplate]

def listOfViewTemplates(by):
	
	collectedViewPlans = FilteredElementCollector(doc).OfClass(ViewPlan).ToElements()
	collectedViewTemplate = [i for i in collectedViewPlans if i.IsTemplate]
	vt = [i.by for i in collectedViewTemplate]
	return vt
	
	
OUT = listOfViewTemplates(Id)

NameError: name ‘Id’ is not defined

I can think of a way, but it will change some of your code.

Instead of accessing parameter values by i.by, use i.LookupParameter(paramName).AsDouble() or .AsString depending on the storage data type.

https://www.revitapidocs.com/2015/4400b9f8-3787-0947-5113-2522ff5e5de2.htm

1 Like

@aclarke What about something like that?

image

1 Like

@salvatoredragotta
Awesome Sauce, thanks Sal…
I don’t 100% understand what the exec() function is doing, could you give me a quick breakdown of what it is doing in this code?

def listOfViewTemplates(by="Object"):
	
	collectedViewPlans = FilteredElementCollector(doc).OfClass(ViewPlan).ToElements()
	collectedViewTemplates = [i for i in collectedViewPlans if i.IsTemplate]
	
	if by == "Object":
		vt = [i for i in collectedViewTemplates]
	else:
		try:		
			exec("vt = [i.%s for i in collectedViewTemplates]" %by)
		except:
			vt = "No Property by this name exists"
	return vt	
	
OUT = listOfViewTemplates(), listOfViewTemplates("Id"), listOfViewTemplates("Name")
2 Likes

@aclarke have a look below.
https://www.programiz.com/python-programming/methods/built-in/exec

1 Like

Thanks Sal, I did look up exec() before asking, it was the %s & %by that id did not understand, which I learned is formatting the string, allowing the property to be passed as a string, and exec() runs the string as code. Pretty slick

1 Like