Move a node via scripting

This should be easy but I can’t figure it out.
I am looking for nodes (which will be in various paths) that are close to each other at a given y. If they are within two units, I want to move one atop the other.
I’ve succeeded in identifying them, but I can’t figure out how to move them.

cutHeight=324
MyLayers=Glyphs.font.selectedLayers
for layer in MyLayers:
	layer.connectAllOpenPaths()
 				
	intersections = layer.intersectionsBetweenPoints((0,cutHeight),(layer.width,cutHeight))
 	for i in range(1,len(intersections)-1):
 		xDiff = intersections[i+1].x-intersections[i].x
 		node=intersections[i]
 		if xDiff < 3:
			node.x += xDiff
			print "shifted", intersections[i], "by", xDiff

This gets me to the second to last line which nets a “can’t set attribute” error.

the “node” is not a node, it is an NSPoint that holds a x/y coordinate. layer.intersectionsBetweenPoints() just gives the intersection points between the paths and that line. It is not adding any points. To insert actual nodes you need to do something like this:

nodes = []
intersections = layer.intersectionsBetweenPoints((0, cutHeight), (layer.width, cutHeight))
for intersection in intersections:
	intersection = intersection.pointValue()
 	for path in layer.paths:
 		pointOnPath, pathTime = path.nearestPointOnPath_pathTime_(intersection, None)
 		if distance(intersection, pointOnPath) < 0.001 and pathTime > 0:
			node = path.insertNodeWithPathTime_(pathTime)
			nodes.append(node)
print(nodes)

That doesn’t do it; I think I have to explain my situation better. What I’m trying to do is “stitch” back together these different (open) paths that end on y=cutHeight (where the middle red guide is in the pic).


I don’t need to insert nodes because all the segments involved already end on that height. But can I go from knowing the coordinates of all these endpoints of various paths to accessing the nodes (through their paths?) to alter their positions?

Bigger picture, ideally this script will join those near-miss endpoints, then actually delete them but preserve the shape. (I gather the thing called removeNodeCheckKeepShape will be useful there, though I’m also confused abobut hwo to use that.)

I was wondering about your description and the code your shoed. It was doing something quite different.

nodes = []
for path in layer.paths:
	for node in path.nodes:
		if abs(node.y - cutHeight) < 0.1:
			nodes.append(node)
nodes.sort(key=lambda node: node.x)
print(nodes)
1 Like

Thanks Georg, the whole thing seems to be working now!

MyLayers=Glyphs.font.selectedLayers
for layer in MyLayers:
	layer.connectAllOpenPaths()
 	nodes = []
	for path in layer.paths:
		for node in path.nodes:
			if abs(node.y - cutHeight) < 0.1:
				nodes.append(node)
	nodes.sort(key=lambda node: node.x)
	print(nodes)

	for i in range(0,len(nodes)-1):
		thisNode = nodes[i]
		xDiff = nodes[i+1].x - thisNode.x
		if xDiff < 3:
			thisNode.x += xDiff
			print "shifted", thisNode, "by", xDiff
	layer.connectAllOpenPaths()