correctPathDirection() only for selected path(s)

I do a path mirroring (flip) with the script. And after that I need:

  1. Set the first node on the lowest left position.
  2. Correct the transformed path(s) direction.

This is exactly what layer.correctPathDirection() doing, but I need it only for the paths in a selection (not for the whole layer). Something like … path.correctPathDirection() ?


I found this comment but looks like path.direction = 0 doesn’t work in Glyphs 3 and it lead to an error: AttributeError: can't set attribute. Anyway I’m not sure this command doing the same complex things as correctPathDirection()

path.reverse()

and

path.makeNodeFirst_(node)
1 Like

Thanks @GeorgSeifert!

So first of all I need to find the best candidate for the new first node for every path in a selection. And it works!

layer = Glyphs.font.selectedLayers[0]
selection = layer.selection

if len(selection) > 0:
	for path in layer.paths:
		if path.nodes[0] in selection:
			candidate = {
				'node': False,
				'x': False,
				'y': False
			}
			# find the lowest left node
			for node in path.nodes:
				if type(node) == GSNode and node.type != OFFCURVE:
					# compare node to candidate
					candidateIsEmpty = candidate['x'] == False and candidate['y'] == False
					if candidateIsEmpty or int(node.y) < candidate['y'] or (int(node.y) == candidate['y'] and int(node.x) < candidate['x']):
						# save the better candidate
						candidate['node'] = node
						candidate['x'] = int(node.x)
						candidate['y'] = int(node.y)
			# set the new first node
			path.makeNodeFirst_(candidate['node'])
			# correct path direction
			if path.direction != -1:
				path.reverse()