Scripting reference parent property with parameter

Attempting to use Project Library script to change a parent component property.
The property name is supplied to Parameter prop.

The below script does not work I get an error stating the 'Object has not attribute 'Prop''

I have added a switch function using IF and ELIF which works except [props = value] does not set the property.

def propChange (comp, prop, dataset, event):
	par = event.source.parent
	compo = par.getComponent(comp)
	props = par.prop
	
	
	if props == 0:
		value = 1
	
	elif props == 1:
		value = 0
		
	props = value
	comp.setDatasetEnabled(dataset, value)

If you want to read a property (of a certain name) dynamically, use the Python builtin getattr:

def propChange (comp, prop, dataset, event):
	par = event.source.parent
	compo = par.getComponent(comp)
	props = getattr(par, prop)
	
	if props == 0:
		value = 1
	
	elif props == 1:
		value = 0
		
	props = value
	comp.setDatasetEnabled(dataset, value)

Assuming prop is passed in as something like "value".

1 Like

You could get rid of the if/elif statements.

# if you really need an integer value
value = int(not props)

# if a boolean will work
value = not props
2 Likes

That code corrected the first issue. Now I having the the second issue which is that

props = value

does not set the property. The property is Boolean the parameter prop is passed in as a string.

Found the solution. I need to also uses setattr

def propChange (comp, prop, dataset, event):
	par = event.source.parent
	compo = par.getComponent(comp)
	props = getattr(par, prop)
	
	value = not props
		
	setattr(par, prop, value)
	
	compo.setDatasetEnabled(dataset, value)
1 Like