Loft 2 groups of curves

i make this script to loft 2 set of curves but some times it choos a wrong pire of curves or start loft from a wrong point ( the starting points for the two curves not alginment ) can any one help me to fix it .

import rhinoscriptsyntax as rs

def sort_curves_by_starting_curve(curves, starting_curve):
# Get the starting point of the initial curve
start_point = rs.CurveStartPoint(starting_curve)

# Create a list of tuples (curve, distance from start_point)
curves_with_distance = [(curve, rs.Distance(start_point, rs.CurveStartPoint(curve))) for curve in curves if curve != starting_curve]

# Sort the list of tuples by the distance (second element of the tuple)
sorted_curves = sorted(curves_with_distance, key=lambda x: x[1])

# Insert the starting curve at the beginning of the sorted list
sorted_curve_ids = [starting_curve] + [curve[0] for curve in sorted_curves]

return sorted_curve_ids

def loft_curves():
# Prompt user to select the group of curves for the first group
curves1 = rs.GetObjects(“Select all curves for the first group”, rs.filter.curve)
if not curves1:
print(“Selection was canceled.”)
return

# Prompt user to select the starting curve for the first group
starting_curve1 = rs.GetObject("Select the starting curve for the first group", rs.filter.curve)
if not starting_curve1:
    print("Selection was canceled.")
    return

# Sort the curves in the first group starting from the starting curve
group1 = sort_curves_by_starting_curve(curves1, starting_curve1)

# Prompt user to select the group of curves for the second group
curves2 = rs.GetObjects("Select all curves for the second group", rs.filter.curve)
if not curves2:
    print("Selection was canceled.")
    return

# Prompt user to select the starting curve for the second group
starting_curve2 = rs.GetObject("Select the starting curve for the second group", rs.filter.curve)
if not starting_curve2:
    print("Selection was canceled.")
    return

# Sort the curves in the second group starting from the starting curve
group2 = sort_curves_by_starting_curve(curves2, starting_curve2)

# Ensure both groups have the same number of curves
if len(group1) != len(group2):
    print("Both groups must have the same number of curves.")
    return

try:
    # Loft each pair of curves from the first and second groups in the order selected
    for i in range(len(group1)):
        curve1 = group1[i]
        curve2 = group2[i]
        print("Lofting curves: {} with {}".format(curve1, curve2))
        rs.AddLoftSrf([curve1, curve2])
    
    print("Loft surfaces created successfully.")
except Exception as e:
    print("An error occurred: {}".format(e))

if name == “main”:
loft_curves()

Maybe some of your curves have flipped start/end points? Can you post an example file where it’s not working?

By the way, you can simplify your sort routine to one line:

def sort_curves_by_starting_curve(curves, starting_curve):
    return sorted(curves, key=lambda curve: rs.Distance(rs.CurveStartPoint(curve), rs.CurveStartPoint(starting_curve)))

1.3dm (238.8 KB)

Try using _CrvSeam to adjust the start point and direction of the curves.

Your script can check for direction in the script with this. Adjusting the start point might be a bit more complicated, but could be done.

if not rs.CurveDirectionsMatch(curve1, curve2):
	rs.ReverseCurve(curve2)

this is the last script
Last-Loft 2 group of curves.py (6.1 KB)

i hope everyone will try this script and if there is any problem let me know