Dynamic Disable/Enable based on boolean in ghPython?

Hey Folks,
First off, I have a kind of hacky problem: I want to dynamically disable/enable certain clusters based on a boolean value from a toggle. I know this has been discussed extensively but I haven’t found a working solution in Python and I am quite lost and frustrated by now.

My code looks like this at the moment:

thisDoc = ghenv.Component.OnPingDocument()

# define callback function
def toggleLocked(owner, state):
    owner.Locked = state

# reverse the input boolean
E = not E
print(">>> Enable is set to " + str(E))

paramsources = ghenv.Component.Params.Input[1].Sources
if paramsources and len(paramsources) > 0:
    owner = paramsources[0].Attributes.Parent.DocObject

if owner and owner.Locked != E:
    thisDoc.ScheduleSolution(1, toggleLocked(owner, E))

I have read lots about ExpireSolution(True), why it should not be used to expire components during a solution et cetera. So I tried to “do it right” by scheduling a new solution via a callback function. Still, I get lots of “Component Expired during a Solution” errors. I would like to fix this and I am hoping for your input :slight_smile:

PS: I know this exists in MetaHopper but it’s not possible for me to implement any AddOns so I would like to come up with a working ghPython-only implementation.