# Import kerning by classes fea by script

**URL:** <https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167>\
**Category:** Scripting\
**Created:** [September 20, 2025, 8:13pm UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167 "2025-09-20T20:13:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aminabedi68](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/aminabedi68/32/20105_2.png) [@aminabedi68](https://forum.glyphsapp.com/u/aminabedi68)\
**Post date:** [September 20, 2025, 8:13pm UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/1 "2025-09-20T20:13:53Z")

</div>

Hi

i was trying to find a universal solution to import kerning by classes from fontforge to Glyphs app using python script. i found @mekkablue ‘s script named “Import Kerning from .fea file“ and change it a bit to match the classes naming structure and script direction (i also used chatGPT).

this is the script:

```python
from __future__ import division, print_function, unicode_literals

import os
from GlyphsApp import Glyphs, GSGlyphsInfo

def importfea_file(Doc, filePath, DIR):
    if not os.path.isfile(filePath):
        print("File not found:", filePath)
        return

    with open(filePath, "r", encoding="utf-8") as f:
        Font = Doc.font
        FontMasterIndex = Doc.windowControllers()[0].masterIndex()
        FontMaster = Font.masters[FontMasterIndex]
        GlyphsInfo = GSGlyphsInfo.sharedManager()
        KerningLines = []

        Line = f.readline()
        while Line:
            Line = Line.strip()
            if not Line:
                Line = f.readline()
                continue

            if Line.startswith("@"):
                key = Line[:Line.find(" ")]
                key_name = key.split("_")[0][1:] + key.split("_")[-1]
                GlyphNames = Line[Line.find("[")+1:Line.find("]")].split(" ")
                Left = True
                Right = True
                if "first" in key:
                    Right = False
                elif "second" in key:
                    Left = False

                print("Creating group:", key_name, "Left:", Left, "Right:", Right)
                print(" Contains glyphs:", GlyphNames)

                for GlyphName in GlyphNames:
                    GlyphName = GlyphsInfo.niceGlyphNameForName_(GlyphName)
                    Glyph = Font.glyphForName_(GlyphName)
                    if Glyph:
                        if Left:
                            Glyph.setRightKerningGroup_(key_name)
                        if Right:
                            Glyph.setLeftKerningGroup_(key_name)

            elif Line.startswith("pos"):
                KerningLines.append(Line)

            Line = f.readline()

        if FontMaster.id not in Font.kerning:
            Font.kerning[FontMaster.id] = {}

        for Line in KerningLines:
            Keys = Line.split()
            if len(Keys) < 4:
                continue

            LeftKey = Keys[1]
            RightKey = Keys[2]
            Value = int(Keys[3].replace(";", ""))

            if LeftKey.startswith("@"):
                LeftKeyFinal = LeftKey
            else:
                LeftKeyFinal = Font.glyphs[LeftKey].id

            if RightKey.startswith("@"):
                RightKeyFinal = RightKey
            else:
                RightKeyFinal = Font.glyphs[RightKey].id

            if DIR == 'RTL':
                LeftKeyFinal, RightKeyFinal = RightKeyFinal, LeftKeyFinal
            print(f"Setting kerning: {LeftKeyFinal} - {RightKeyFinal} = {Value}")
            Font.kerning[FontMaster.id][(LeftKeyFinal, RightKeyFinal)] = Value

def main():
    Doc = Glyphs.currentDocument
    kern_fea_file_LTR = "/Users/amin/Desktop/kern_LTR.fea"
    kern_fea_file_RTL = "/Users/amin/Desktop/kern_RTL.fea"
    importfea_file(Doc, kern_fea_file_LTR, LTR)
    importfea_file(Doc, kern_fea_file_RTL, RTL)
if __name__ == " __main__":
    main()

```

and this is the kerning file sample:

```fea
#DIR:LTR
@kc58_first_1 = [A Aacute Agrave];
@kc58_first_2 = [E Eacute Egrave];
@kc58_first_3 = [O Oacute Ograve];

@kc58_second_1 = [V W];
@kc58_second_2 = [Y Yacute];

pos @kc58_first_1 @kc58_second_1 -80;
pos @kc58_first_1 @kc58_second_2 -70;
pos @kc58_first_2 @kc58_second_1 -60;
pos @kc58_first_3 @kc58_second_2 -65;

```

classes created in Glyphs app without any kerning values. kernings window show no kerning and i got this error when i trying to save the file:

 ![Screenshot 2025-09-20 at 23.27.43](https://forum.glyphsapp.com/uploads/default/original/3X/c/0/c0103865ad5c49cf3ed70ff7a2c3c6729bb097ea.jpeg)

i know there is some mistakes with the script and script direction implementation but i haven’t any clue.

---

<div class="post-metadata">

**Author:** ![aminabedi68](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/aminabedi68/32/20105_2.png) [@aminabedi68](https://forum.glyphsapp.com/u/aminabedi68)\
**Post date:** [September 21, 2025, 8:22pm UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/2 "2025-09-21T20:22:18Z")

</div>

i really need this, please answer.

i add a test file here too:

[Estedad.glyphs](https://forum.glyphsapp.com/uploads/short-url/rTROxwHH0bmU1gyZYhAFOSw8w9G.glyphs) (25.3 KB)

---

<div class="post-metadata">

**Author:** ![mekkablue](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/mekkablue/32/23_2.png) [@mekkablue](https://forum.glyphsapp.com/u/mekkablue)\
**Post date:** [September 22, 2025, 5:35am UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/3 "2025-09-22T05:35:25Z")

</div>

I’ll have a look. Though I was about to retire that script.

---

<div class="post-metadata">

**Author:** ![aminabedi68](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/aminabedi68/32/20105_2.png) [@aminabedi68](https://forum.glyphsapp.com/u/aminabedi68)\
**Post date:** [September 22, 2025, 7:59am UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/4 "2025-09-22T07:59:04Z")

</div>

Thank you so much 🙏

---

<div class="post-metadata">

**Author:** ![mekkablue](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/mekkablue/32/23_2.png) [@mekkablue](https://forum.glyphsapp.com/u/mekkablue)\
**Post date:** [September 22, 2025, 9:32am UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/5 "2025-09-22T09:32:58Z")

</div>

I updated the script. It works on your files for me in the latest Glyphs beta.

---

<div class="post-metadata">

**Author:** ![aminabedi68](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/aminabedi68/32/20105_2.png) [@aminabedi68](https://forum.glyphsapp.com/u/aminabedi68)\
**Post date:** [September 22, 2025, 5:37pm UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/6 "2025-09-22T17:37:29Z")

</div>

it’s working now. thank you 🙂

 ![Screenshot 2025-09-22 at 20.57.26](https://forum.glyphsapp.com/uploads/default/original/3X/1/0/102146fe3c67dea0dcf5f38d281c4584e31a3d81.jpeg)

i didn’t check the python api documentation descriptions. i thought the function `setKerningForPair(`_`fontMasterId`_`, `_`leftKey`_`, `_`rightKey`_`, `_`value`_`[, `_`direction=GSLTR`_`])` only working for single pairs.

this function has also supports direction. i have the arabic kernings too(witch is RTL obviously), i can hack your script for myself to do that but i suggest to consider direction support option in the script too.

thank you again 🙌

---

<div class="post-metadata">

**Author:** ![mekkablue](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/mekkablue/32/23_2.png) [@mekkablue](https://forum.glyphsapp.com/u/mekkablue)\
**Post date:** [September 23, 2025, 10:10am UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/7 "2025-09-23T10:10:37Z")

</div>

You can do a Pull Request if you find a solution that also works for RTL.

---

<div class="post-metadata">

**Author:** ![aminabedi68](https://forum.glyphsapp.com/user_avatar/forum.glyphsapp.com/aminabedi68/32/20105_2.png) [@aminabedi68](https://forum.glyphsapp.com/u/aminabedi68)\
**Post date:** [September 25, 2025, 5:32am UTC](https://forum.glyphsapp.com/t/import-kerning-by-classes-fea-by-script/35167/8 "2025-09-25T05:32:42Z")

</div>

PR sent. Please check it.
