Invalid surface input GhPython - Sine wave on surface

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!")

Hi Ryan, it’s a lot easier to debug if you upload the Grasshopper file. Remember to internalise the relevant geometry parameters, see this for reference:

And welcome on board :slight_smile:

1 Like

@Ryan_Penn If you are still struggling with that problem, it might be because your surface is coming in as a Brep in the Script component. You can change the input’s type hint to “Surface”, or get the surface from the Brep’s surfaces list, both options let me run your script all the way to “Sine wave points generated successfully!”