Automatic slider movement using python

Hi
I am trying to do something similar to what is happening here but using python.
I have come with this code so far, but when executed I receive the object expired during a solution error.

import Grasshopper as gh
import random

GrasshopperDocument = gh.Instances.ActiveCanvas.Document

#####
# Define the Functions
#####

def FindSliders():
    input = ghenv.Component.Params.Input[1]
    sliders = []
    for source in input.Sources:
        if source is not None:
          sliders.append(source)
    return sliders

def FindParam():
    input = ghenv.Component.Params.Input[0]
    for source in input.Sources:
        if source is not None:
            return source
        else:
            return None


def ScheduleCallback():
    for i in range(0, int(iterations)):
        for slider in sliders:
           slider.SetSliderValue(random.random() * 10)


#####
# Main Code
#####

running = False

if not running:
    running = True
    iterations = max(Runs, 1)
    sliders = FindSliders()
    param = FindParam()

if iterations > 0:
    GrasshopperDocument.ScheduleSolution(10, ScheduleCallback())
else:
    running = False

I can avoid the expired during solution window if I use slider.Slider.Value instead of slider.SetSliderValue(), but the sliders go crazy and do not stop changing values as expected per the number in Runs.
I know running should be decrementing with each loop, but putting a while loop with a decrement in ScheduleCallback() does not do work.

Any ideas on how to make my code work as expected?
Thanks

This might help:

1 Like

Thanks!
The updatable global variable did the trick. The code ended up like this:

"""Keeps Selecting a random value of a gh slider for a number of Runs

    The component has two inputs: on [0] Runs, on [1] Sliders
    You can connect only one slider to [0]
    You can connect multiple sliders to [1], but must set list access

    When you alter the value of [0], the iteration on [1] restarts
    """

import Grasshopper as gh
import random
        
        
GrasshopperDocument = gh.Instances.ActiveCanvas.Document
        

#####
# Define the Fucntions
#####        
        
def FindSliders():
    input = ghenv.Component.Params.Input[1]
    sliders = []
    for source in input.Sources:
        if source is not None:
            sliders.append(source)
    return sliders
        
        
def FindParam():
    input = ghenv.Component.Params.Input[0]
    for source in input.Sources:
        if source is not None:
            return source
        else:
            return None


def ScheduleCallback():
    for i in range(int(iterations)):
        for slider in sliders:
            slider.Slider.Value = random.random() * 10

#####
# Main Code
#####

if "iterations" not in globals():
    sliders = FindSliders()
    param = FindParam()
    iterations = max(1, int(Runs))
        
if iterations > 0:  
    GrasshopperDocument.ScheduleSolution(10, ScheduleCallback())
    iterations -= 1 # Does the countdown
else:
    if param.Slider.ValueChangedEventHandler:
        global iterations
        del iterations
1 Like