Guides Through All Selected Nodes

I am wondering why this script returns two sets of guides, when just two nodes on a straight lines are selected. Not really sure what I should change to fix it.

Which script? If you want to discuss scripting issues, please post the code. If you have a bug report for a script, it is advisable to post an issue on GitHub.

Not sure, how to post an issue on GitHub. But here is the script.

#MenuTitle: Guides through All Selected Nodes
# -*- coding: utf-8 -*-
__doc__="""
Creates guides through all selected nodes.
"""

import math

thisFont = Glyphs.font # frontmost font
selectedLayers = thisFont.selectedLayers # active layers of selected glyphs

def angle( firstPoint, secondPoint ):
	"""
	Returns the angle (in degrees) of the straight line between firstPoint and secondPoint,
	0 degrees being the second point to the right of first point.
	firstPoint, secondPoint: must be NSPoint or GSNode
	"""
	xDiff = secondPoint.x - firstPoint.x
	yDiff = secondPoint.y - firstPoint.y
	return math.degrees(math.atan2(yDiff,xDiff))

def newGuide( position, angle ):
	newGuide = GSGuideLine()
	newGuide.position = position
	newGuide.angle = angle
	return newGuide

def isThereAlreadyAGuideWithTheseProperties(thisLayer,guideposition,guideangle):
	for thisGuide in thisLayer.guides:
		if thisGuide.angle == guideangle:
			if thisGuide.position == guideposition:
				return True
			elif angle(guideposition,thisGuide.position) % 180 == guideangle % 180:
				return True
	return False

if len(selectedLayers) == 1:
	thisLayer = selectedLayers[0]
	thisGlyph = thisLayer.parent
	currentPointSelection = [point.position for point in thisLayer.selection if type(point) == GSNode]
	
	# clear selection:
	Layer.clearSelection()
	
	thisGlyph.beginUndo() # begin undo grouping
	
	if len(currentPointSelection) > 1:
		currentPointSelection.append(currentPointSelection[0])
		for i,j in enumerate(range(1,len(currentPointSelection))):
			point1 = currentPointSelection[i]
			point2 = currentPointSelection[j]
			angleBetweenPoints = angle(point1,point2)
			middlePoint = addPoints(point1,point2)
			middlePoint.x *= 0.5
			middlePoint.y *= 0.5
			
			# create guide and add it to layer:
			if not isThereAlreadyAGuideWithTheseProperties(thisLayer, middlePoint, angleBetweenPoints):
				guideBetweenPoints = newGuide(middlePoint, angleBetweenPoints)
				thisLayer.guides.append( guideBetweenPoints )
			
			# select it:
			thisLayer.selection.append(guideBetweenPoints)
			
			
	thisGlyph.endUndo()   # end undo grouping

Two identical guides should not happen. I will have a look.

In the repository, you will find an Issues tab.

1 Like

Sorry, but there are some problems with my access to github. Can you tell me if you were able to reproduce the double guidelines issue?

I could and I am having a look.

1 Like

Were you able to sort it out? I checked the code, but am unsure why two guidelines appear instead of one?

you know that if you have two nodes selected and click “Add Guide” in the context menu, you get a line through the two nodes? (it works with two nodes only).

Certainly. The whole point of this is to have a shortcut, since Add Guide doesn’t support one, I am grateful to have Mekka’s Script. However, currently it adds two guides instead of one when two nodes are selected.

Thank you, Rainer!!! :kissing:

I fixed the script.

2 Likes

Oh. Double thanks than.