It seems that the sample() function is still receiving the values in a way it doesn’t expect. The issue is that in TouchDesigner, when working with TOPs, the sampling might require slightly different handling, especially when working with color data.
We will approach this differently by using a TOP to CHOP to extract the color data from the Ramp TOP and then use the CHOP data to drive the Constant TOPs. This method is more robust and avoids issues with sample().
Here’s a new approach using Python, but this time we’ll handle it using a TOP to CHOP:
-
Add a TOP to CHOP Node:
- Connect the
ramp5 TOP to a TOP to CHOP node.
- In the
TOP to CHOP parameters, set the resolution to 5 (width 5, height 1) so you sample 5 pixels evenly across the ramp.
-
Python Script to Set Colors Dynamically:
Here’s the Python script that will now fetch color values from the TOP to CHOP and apply them to the constant TOPs.
# Get references to the TOP to CHOP node
top_to_chop = op('toptochop1') # Assume you named the TOP to CHOP node 'toptochop1'
# Get references to the Constant TOPs
# Loop through each constant TOP and set their color values
for i in range(5): # We have 5 colors to extract from the CHOP
# Fetch RGBA values from the TOP to CHOP (assuming channels are named 'r', 'g', 'b', 'a')
r = top_to_chop['chan1r'][i] # Red channel for ith pixel
g = top_to_chop['chan1g'][i] # Green channel for ith pixel
b = top_to_chop['chan1b'][i] # Blue channel for ith pixel
a = top_to_chop['chan1a'][i] # Alpha channel for ith pixel
# Set the corresponding Constant TOP's color
constant_tops[i].par.colorr = r
constant_tops[i].par.colorg = g
constant_tops[i].par.colorb = b
constant_tops[i].par.colora = a
- TOP to CHOP: Converts the
Ramp TOP into CHOP channels, so we can access individual pixel color values as CHOP data.
- Loop through 5 positions: We are extracting the colors from 5 positions on the ramp (using
TOP to CHOP), and each position provides RGBA values. These values are then assigned to the Constant TOPs.
- Connect your
ramp5 TOP to the TOP to CHOP.
- Make sure the resolution in the
TOP to CHOP is set to 5 (so it samples 5 evenly spaced colors along the ramp).
- Add the Script TOP and place the script inside it.
- Ensure that the
TOP to CHOP is named correctly (as per the script, it’s toptochop1).
- Make sure the constant TOPs are named correctly (
constant8 to constant12).
With this approach, you’ll have dynamic color updates as the ramp changes. This method avoids issues with the sample() function and uses TOP to CHOP to reliably extract color data.
Let me know how this works out for you!