I am trying to write a script to create a sine wave from a surface. I have used ChatGPT to help me write the script. I’ve gone through several back and forths with the AI looking for a solution but it has hit a dead end.
I am consistently getting an ‘Input is not a valid surface’ error from the editor. However, I have correctly referenced the surface and confirmed this works by writing another simple script that just references the surface and it can correctly identify the surface as valid.
What should I change in my script? What am I doing wrong?
Here’s my script:
"""Provides a scripting component.
Inputs:
x: The x script variable
y: The y script variable
Output:
a: The a output variable"""
__author__ = "pennh"
__version__ = "2024.02.22"
import rhinoscriptsyntax as rsimport
import Rhino.Geometry as rg
import math
# Function to create sine wave points
def create_sine_wave(surface, frequency, amplitude, num_divisions):
# Get surface domain
u_domain = surface.Domain(0)
v_domain = surface.Domain(1)
# Initialize points list
points = []
# Generate points on the surface
for i in range(num_divisions + 1):
u = u_domain.T0 + (u_domain.T1 - u_domain.T0) * i / num_divisions
for j in range(num_divisions + 1):
v = v_domain.T0 + (v_domain.T1 - v_domain.T0) * j / num_divisions
point = surface.PointAt(u, v)
# Calculate sine wave offset
offset = amplitude * math.sin(frequency * u)
point += surface.NormalAt(u, v) * offset
# Append point to the list
points.append(point)
return points
# Main function
def main(surface, frequency, amplitude, num_divisions):
# Check if surface is valid
if not surface or not isinstance(surface, rg.Surface):
print("Input is not a valid surface.")
return None
# Create sine wave points
sine_wave_points = create_sine_wave(surface, frequency, amplitude, num_divisions)
return sine_wave_points
# Test the script
if __name__ == "__main__":
# Define inputs
surface = x # Surface input from Grasshopper
frequency = 1.0 # Adjust frequency as needed
amplitude = 10.0 # Adjust amplitude as needed
num_divisions = 20 # Adjust number of divisions as needed
# Call the main function
sine_wave_points = main(surface, frequency, amplitude, num_divisions)
# Output the points
if sine_wave_points:
print("Sine wave points generated successfully!")