It seems like the issue might be related to the initialization of the script or the persistence of the previous_index variable. Let’s ensure the variable is properly initialized and maintained across calls. Additionally, ensure that no other part of the script or TouchDesigner setup is affecting the switch index.
Here’s a revised version of the script with more detailed comments to ensure everything is clear:
# Ensure these are stored at the module level to persist between calls
if 'previous_index' not in globals():
max_index = 7 # Maximum index, inclusive
def onValueChange(channel, sampleIndex, val, prev):
# Increment the previous index
# Loop back to 0 if the max index is exceeded
if previous_index > max_index:
previous_index = min_index
# Debugging: Print new index
#print(f"New index: {previous_index}")
# Update the Switch TOP's index parameter
op('switch1').par.index = previous_index
# The function onValueChange is triggered when a value changes in the monitored CHOP
# Make sure to set the channel's parameter to value change in the CHOP Execute DAT settings
Make sure you check the following in TouchDesigner:
- The script is correctly placed in a
DAT Execute DAT, and the onValueChange function is set to trigger on value change.
- The
previous_index variable is declared at the module level to persist between calls.
- No other scripts or operators are modifying the index parameter of
switch1.
If the issue persists, you might want to add print statements for debugging purposes to verify the flow of the script:
# Ensure these are stored at the module level to persist between calls
if 'previous_index' not in globals():
max_index = 7 # Maximum index, inclusive
def onValueChange(channel, sampleIndex, val, prev):
# Increment the previous index
# Loop back to 0 if the max index is exceeded
if previous_index > max_index:
previous_index = min_index
# Debugging: Print new index
print(f"New index: {previous_index}")
# Update the Switch TOP's index parameter
op('switch1').par.index = previous_index
# The function onValueChange is triggered when a value changes in the monitored CHOP
# Make sure to set the channel's parameter to value change in the CHOP Execute DAT settings
This will print the new index value to the console each time the function is called, allowing you to verify that the script is incrementing and setting the index correctly.