Variable as property?

Is it possible to pass a property as a variable?

vt = [ for i in collectedViewTemplate]

def listOfViewTemplates(by):
	collectedViewPlans = FilteredElementCollector(doc).OfClass(ViewPlan).ToElements()
	collectedViewTemplate = [i for i in collectedViewPlans if i.IsTemplate]
	vt = [ 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, use i.LookupParameter(paramName).AsDouble() or .AsString depending on the storage data type.

1 Like

@aclarke What about something like that?


1 Like

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]
			exec("vt = [i.%s for i in collectedViewTemplates]" %by)
			vt = "No Property by this name exists"
	return vt	
OUT = listOfViewTemplates(), listOfViewTemplates("Id"), listOfViewTemplates("Name")

@aclarke have a look below.

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