Awesome! Now that we have an optimized solution for shuffling the samples, we can certainly explore different sorting patterns like sine waves or other mathematical functions. Below, I’ll outline how to implement a few interesting sorting methods using sine wave patterns, linear patterns, and even custom noise patterns.
We can sort the values of the “ty” channel based on their corresponding sine values. This will create a smooth sine wave-like pattern in the samples.
# Clear any existing channels in scriptOp
inputCHOP = scriptOp.inputs[0]
# Check if the "ty" channel exists in the input CHOP
if 'ty' in [chan.name for chan in inputCHOP.chans()]:
ty_channel = inputCHOP['ty']
# Append the "ty" channel to the output
scriptOp.appendChan('ty')
# Convert the "ty" channel samples to a NumPy array for faster processing
samples = np.array(ty_channel.vals) # Get the samples as an array
# Generate a sine wave based on the number of samples
num_samples = inputCHOP.numSamples
sine_wave = np.sin(np.linspace(0, 2 * np.pi, num_samples))
# Sort the samples based on the sine wave values
sorted_indices = np.argsort(sine_wave)
sorted_samples = samples[sorted_indices]
# Assign the sorted samples back in one step (bulk processing)
scriptOp['ty'].vals = sorted_samples
np.sin(np.linspace(0, 2 * np.pi, num_samples)): This generates a smooth sine wave pattern.
np.argsort(sine_wave): This gets the indices that would sort the sine wave. We apply this ordering to the original sample data, creating a sine-like variation in the values.
If you want to simply sort the values in ascending or descending order, you can do this easily using np.sort() or reverse sorting.
# Clear any existing channels in scriptOp
inputCHOP = scriptOp.inputs[0]
# Check if the "ty" channel exists in the input CHOP
if 'ty' in [chan.name for chan in inputCHOP.chans()]:
ty_channel = inputCHOP['ty']
# Append the "ty" channel to the output
scriptOp.appendChan('ty')
# Convert the "ty" channel samples to a NumPy array for faster processing
samples = np.array(ty_channel.vals)
# Sort the samples in ascending order
sorted_samples = np.sort(samples)
# Alternatively, for descending order: sorted_samples = np.sort(samples)[::-1]
# Assign the sorted samples back in one step
scriptOp['ty'].vals = sorted_samples
np.sort(samples): Sorts the sample values in ascending order.
[::-1]: This reverses the sorted array, making it descending if needed.
We can use a noise pattern to sort the samples, which could give a more chaotic but structured result. Here, we use Perlin noise or any custom noise to generate the sorting order.
# Clear any existing channels in scriptOp
inputCHOP = scriptOp.inputs[0]
# Check if the "ty" channel exists in the input CHOP
if 'ty' in [chan.name for chan in inputCHOP.chans()]:
ty_channel = inputCHOP['ty']
# Append the "ty" channel to the output
scriptOp.appendChan('ty')
# Convert the "ty" channel samples to a NumPy array for faster processing
samples = np.array(ty_channel.vals)
# Generate a random noise pattern based on the number of samples
noise = np.random.rand(inputCHOP.numSamples) # You can replace with any other noise algorithm
# Sort the samples based on the noise values
sorted_indices = np.argsort(noise)
sorted_samples = samples[sorted_indices]
# Assign the sorted samples back in one step
scriptOp['ty'].vals = sorted_samples
np.random.rand(inputCHOP.numSamples): Generates random noise values, which we use to sort the samples.
- This can be replaced with more sophisticated noise generation algorithms like Perlin noise for a more organic pattern.
We can also sort the samples based on an exponential function, which will create a fast-growing curve in the data. This could be useful for effects where the data needs to grow or decay in intensity.
# Clear any existing channels in scriptOp
inputCHOP = scriptOp.inputs[0]
# Check if the "ty" channel exists in the input CHOP
if 'ty' in [chan.name for chan in inputCHOP.chans()]:
ty_channel = inputCHOP['ty']
# Append the "ty" channel to the output
scriptOp.appendChan('ty')
# Convert the "ty" channel samples to a NumPy array for faster processing
samples = np.array(ty_channel.vals)
# Generate an exponential curve for sorting
exp_curve = np.exp(np.linspace(0, 5, inputCHOP.numSamples)) # Exponential growth
# Sort the samples based on the exponential values
sorted_indices = np.argsort(exp_curve)
sorted_samples = samples[sorted_indices]
# Assign the sorted samples back in one step
scriptOp['ty'].vals = sorted_samples
np.exp(np.linspace(0, 5, inputCHOP.numSamples)): Generates an exponentially growing curve to sort the data. The range of np.linspace() can be adjusted to control the steepness of the curve.
- Sine Sorting: Creates a smooth, wave-like variation in the data.
- Linear Sorting: Simple ascending or descending sorting.
- Noise-Based Sorting: Introduces chaos but with a structured randomness.
- Exponential Sorting: Creates a fast-growing or decaying effect in the data.
You can experiment with these methods to create different effects in your project. Let me know which ones you find most interesting or if you want to explore more complex patterns!