Exporting layers?

I still see that = sign in your screenshot. Didn’t you remove it?

wrong screenshot sorry

You could change line 7 to:
`layers = Font.selectedLayers``

This should give you a list for all your selected Layers (or glyphs)

OR: you will have to index / loop over the glifo, because it’s a list itself, and you can only access the layers of each item of this list. The list itself doesn’t have an attribute layers, hence the error.

The traceback is a bit weird though, it shows a line of code which is not seen at all in your code. And the shown line (in red) should also actually work (in this case for the glyph “a” :confused:

Sorry, I know almost nothing about drawbot or python.

What I was trying to do is grab each glyph from the font (could also be selected glyphs in the UI) and loop through each glyph’s layers.
Would a nested loop do the job?

have you tried this one yet?

oh sorry!
yes I’ve tried. same error.

Font should work in the usual Glyphs python environment, it seems to not work in DrawBot. So replace this Font by Glyphs.font again.

Sorry for the bad debugging. I am not trying your code, just reading and typing. Maybe if you post the code, it’s easier to test it here.

No worries! Many thanks for your help! :blush:

Here’s the code that works but it only does the layers in one glyph:

from GlyphsApp import *

    Height = 1000
    extraSpace = 1.2

    layers = Glyphs.font.glyphs["a"].layers

    #print len(glifo) , ("glyphs in this font")
    print len(layers) , ("layers in this glyph")

    master = Glyphs.font.masters[Glyphs.font.masterIndex]

    Scale = Height / (Glyphs.font.upm * extraSpace)

    offsetY = -master.descender + 50


    for i in range(len(layers)):
        print(layers[i])
        newPage(Height, Height)
        save()
        scale(Scale)
        offsetX = ((Height / Scale) - layers[i].width) /2
        translate(offsetX, offsetY)
        drawPath(layers[i].completeBezierPath)
        restore()
        #saveImage("~/Documents/proto_offline/Karte/drawbot/export/test.png", 120) 

And here’s the broken code:

from GlyphsApp import *

Height = 1000
extraSpace = 1.2


glifo = Glyphs.font.glyphs
layers = glifo.layers

print len(glifo) , ("glyphs in this font")
print len(layers) , ("layers in this glyph")

master = Glyphs.font.masters[Glyphs.font.masterIndex]

Scale = Height / (Glyphs.font.upm * extraSpace)

offsetY = -master.descender + 50


for i in range(len(layers)):
    print(layers[i])
    newPage(Height, Height)
    save()
    scale(Scale)
    offsetX = ((Height / Scale) - layers[i].width) /2
    translate(offsetX, offsetY)
    drawPath(layers[i].completeBezierPath)
    restore()
    #saveImage("~/Documents/proto_offline/Karte/drawbot/export/test.png", 120)

As Mark said, you need to iterate over all glyphs and ask each for the layers.


for glyph in Glyphs.font.glyphs:
    layers = glyph.layers
    ....
1 Like

Thanks @GeorgSeifert that did the work.
However it is stopping on the letter “r”
Here’s the screenshot:

There is a non ASCII letter in the name of a layer in the “r” glyph. sometimes Python has a problem with that. Either rename the layer or remove the print layers[i] in line 19.

1 Like

Thanks! That did the job.
One more question:
Is it possible to call the glyph name or even the layer name in the exported file?
Would it be something like this?

saveImage("~/"Glyphs.font.glyph.layer".png", 300)

Sure. You got all the information, so you can reuse it anywhere.
Try sth. like:

saveImage("~/%s_%s_%s.png" % (font.familyName, glyph.name, layers[i].name) ) # I shortened your path here

Inside the quotes, each %s is a represantation/placeholder for a variable, after the quotes with the % you say: and now I tell you my variables. In the paranthesis, you have to put as many variables, as you got %s inside your quotes.

I wouldn’t recommend using periods in the file name. It works on an ordinary mac environment, but can still cause trouble in some cases.

1 Like

:raised_hands:
You guys are the best!
This has been a really great way to start with python and drawbot!
I will definitely keep learning it!

Hello There,

What a great Forum!
I came across this article and I am having the exact same issue that I am an absolute noob at coding in general.

I tried this code from this sub but I have error messages occurring:

I would want to export all the sketches from the layers of all the glyphs into one pdf with the metrics and name of the Layer in the pdf. (As if you would print out directly from glyphs)

Would anybody be so kind to have a look at this?

Thanks in advance!

It seems that you copied some extra white space that is causing trouble.

thank you so much for the answer! I could not fix it yet though because of my non existing programming knowledge…Did you mean in line 19 with the “print”.
Other than that I recopied it and tried to get rid of some white space from all the words or where do you think could the problem be? It also says the error message “newPage” is not defined?

The first problem is in the first like. Or in the second. Put cursor after the ‘f’ in the first line and remove by pressing a few times and then retype it. Do that with every line in the script and see if the error disappear.