Skip to content

Instantly share code, notes, and snippets.

@mekkablue
Created July 27, 2019 16:05
Show Gist options
  • Save mekkablue/dce4eefb8211abfb58678673c9785211 to your computer and use it in GitHub Desktop.
Save mekkablue/dce4eefb8211abfb58678673c9785211 to your computer and use it in GitHub Desktop.
Create Center Guideline
#MenuTitle: Create Center Guideline
# -*- coding: utf-8 -*-
__doc__="""
Creates a guideline, horizontally or vertically centered between two selected nodes.
"""
Font = Glyphs.font
FontMaster = Font.selectedFontMaster
selectedLayers = Font.selectedLayers
selectedLayer = selectedLayers[0]
try:
# until v2.1:
selection = selectedLayer.selection()
except:
# since v2.2:
selection = selectedLayer.selection
def addAndSelectGuideline( thisLayer, originPoint, angle ):
"""Adds a guideline in thisLayer at originPoint, at angle."""
try:
myGuideline = GSGuideLine()
myGuideline.position = originPoint
myGuideline.angle = angle
thisLayer.addGuideLine_( myGuideline )
thisLayer.clearSelection()
thisLayer.addSelection_( myGuideline )
return True
except Exception as e:
print e
return False
if len(selectedLayers) == 1 and len(selection) == 2:
thisLayer = selectedLayers[0]
thisGlyph = thisLayer.parent
firstNode = selection[0]
secondNode = selection[1]
centerX = ( firstNode.x + secondNode.x ) / 2.0
centerY = ( firstNode.y + secondNode.y ) / 2.0
guidelineOrigin = NSPoint( centerX, centerY )
guidelineAngle = 90.0
xDiff = firstNode.x - secondNode.x
yDiff = firstNode.y - secondNode.y
if xDiff < yDiff:
guidelineAngle = 0.0
thisGlyph.beginUndo()
if not addAndSelectGuideline( thisLayer, guidelineOrigin, guidelineAngle ):
print "Error: Could not add guideline."
thisGlyph.endUndo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment