Could it be useful to add an attribute to GSNode to access the segments linked to it?
GSNode.segment could run :
def get_node_segment(node):
segments_with_node = []
# Helper function to extract point coordinates
def get_point_coords(point):
return (point.x, point.y)
# Determine the segment to which the node belongs based on its type
if node.type != "offcurve":
pos = get_point_coords(node)
for path in node.layer.paths:
for segment in path.segments:
start_point = get_point_coords(segment.pointAtIndex_(0))
end_point = get_point_coords(segment.lastPoint())
if pos == start_point or pos == end_point:
segments_with_node.append(segment)
else:
nextCurve_node = node.nextNode if node.nextNode.type != "offcurve" else None
prevCurve_node = node.prevNode if node.prevNode.type != "offcurve" else None
for path in node.layer.paths:
for segment in path.segments:
start_point = get_point_coords(segment.pointAtIndex_(0))
end_point = get_point_coords(segment.lastPoint())
if nextCurve_node and get_point_coords(nextCurve_node) == end_point:
segments_with_node.append(segment)
if prevCurve_node and get_point_coords(prevCurve_node) == start_point:
segments_with_node.append(segment)
return segments_with_node
EDIT : This one also return single segment for offcurve node
The code would be pretty slow. e.g. path.segments is iterating all nodes and makes a list of new GSPathSegment objects every time you call it. If you really need the segment, the method could look like this:
If it is performance critical, don’t use .prevNode and .nextNode. Better use the method I showed above (by keeping track of the index). At least store the result from those properties.