Create a Python script component for Grasshopper that follows best practices for stability, error handling, and performance. The component should use the direct script approach and handle all inputs and outputs properly.
"""
[COMPONENT_NAME]
[BRIEF_DESCRIPTION]
Inputs:
[INPUT_NAME_1]: [INPUT_DESCRIPTION_1]
[INPUT_NAME_2]: [INPUT_DESCRIPTION_2]
...
Outputs:
[OUTPUT_NAME_1]: [OUTPUT_DESCRIPTION_1]
[OUTPUT_NAME_2]: [OUTPUT_DESCRIPTION_2]
...
"""
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import ghpythonlib.components as ghc
# Import any other required libraries
# 1. Initialize all outputs to empty lists or None
[OUTPUT_1] = [] # or None if not a list
[OUTPUT_2] = [] # or None if not a list
try:
# 2. Validate inputs and set defaults
# Check for None values and provide sensible defaults
# Example:
if [INPUT_1] is None:
raise ValueError("Input required")
[INPUT_2] = [DEFAULT_VALUE] if [INPUT_2] is None else [INPUT_2]
# 3. Convert GUIDs to geometry if needed
# Example:
[GEOMETRY] = rs.coerce[TYPE]([INPUT])
if [GEOMETRY] is None:
raise ValueError("Invalid geometry")
# 4. Main logic
# Implement the component's functionality
# Use Rhino/Grasshopper built-in functions when possible
# Use list comprehensions for concise operations
# 5. Assign results to output variables
[OUTPUT_1] = [RESULT_1]
[OUTPUT_2] = [RESULT_2]
except Exception as e:
import Rhino
Rhino.RhinoApp.WriteLine(f"Error: {str(e)}")
# Reset outputs on error
[OUTPUT_1] = [] # or None if not a list
[OUTPUT_2] = [] # or None if not a list
Documentation
Input Handling
count = int(count) if count is not None else 10
GUID Conversion
curve = rs.coercecurve(curve_guid)
if curve is None: raise ValueError("Invalid curve")
rs.coerce3dpoint()
for pointsrs.coercecurve()
for curvesrs.coercesurface()
for surfacesrs.coercebrep()
for brepsrs.coercemesh()
for meshesError Handling
Rhino.RhinoApp.WriteLine(f"Error: {str(e)}")
raise ValueError("Invalid input")
Output Management
Code Efficiency
Variable Naming
list
, range
, str
)"""
Curve Divider
Divides a curve into equal segments and returns points and parameters
Inputs:
curve: Curve to divide
count: Number of divisions (default: 10)
include_ends: Include endpoints (default: True)
Outputs:
a: Points along the curve
b: Parameter values at division points
c: Tangent vectors at division points
"""
import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
# Initialize outputs
a = [] # points
b = [] # params
c = [] # tangents
try:
# Validate inputs with defaults
if curve is None:
raise ValueError("Curve input is required")
count = 10 if count is None else int(count)
include_ends = True if include_ends is None else bool(include_ends)
if count < 2:
raise ValueError("Count must be at least 2")
# Convert GUID to geometry if needed
curve_obj = rs.coercecurve(curve)
if curve_obj is None:
raise ValueError("Invalid curve geometry")
# Main logic
domain = curve_obj.Domain
# Calculate division parameters
if include_ends:
# Include start and end points
division_count = count - 1
t_values = [domain.ParameterAt(i / division_count) for i in range(count)]
else:
# Exclude start and end points
division_count = count + 1
t_values = [domain.ParameterAt((i + 1) / division_count) for i in range(count)]
# Calculate points and tangents at parameters
a = [curve_obj.PointAt(t) for t in t_values] # points
c = [curve_obj.TangentAt(t) for t in t_values] # tangents
b = t_values # params
except Exception as e:
import Rhino
Rhino.RhinoApp.WriteLine(f"Error: {str(e)}")
# Reset outputs on error
a = []
b = []
c = []