Drawing instances with Drawbot plugin

I don’t know how to draw all instances with the Drawbot plugin… I have this:

from GlyphsApp import *

fill(0.4, 1.0)

myFont = Glyphs.font
selectedLayers = myFont.selectedLayers

for thisInstance in myFont.instances:
    newPage()
    print thisInstance
    for thisLayer in selectedLayers:
        drawPath(thisLayer.bezierPath)
        print thisLayer

it prints like this:

<GSInstance "1" width 100.0 weight 100.0>
<GSLayer "Light" (O)>
<GSInstance "2" width 100.0 weight 150.0>
<GSLayer "Light" (O)>
<GSInstance "3" width 100.0 weight 200.0>
<GSLayer "Light" (O)>

This draws current layer but not the instances…

The selectedLayers have nothing to do with the instances. Just by iterating over the instances array, you don’t change anything.

What you need to do is to ask the instances’ interpolatedFontProxy for the glyph and then for it’s layer:

from GlyphsApp import *
fill(0.4, 0.2)
myFont = Glyphs.font
selectedLayers = myFont.selectedLayers

for thisInstance in myFont.instances:
    newPage()
    instanceFont = thisInstance.interpolatedFontProxy
    for thisLayer in selectedLayers:
        instanceGlyph = instanceFont.glyphForName_(thisLayer.parent.name)
        instanceLayer = instanceGlyph.layers[instanceFont.fontMasterID()]
        drawPath(instanceLayer.bezierPath)
    

Thank you! Works great, although i’m quite confused how could i find such information for future questions. I can’t find it on the docu.glyphsapp.com.

The documentation is a constant work in progress and the interpolatedFontProxy was added recently.

1 Like

This little script is handy Georg, but I am wondering how I can specify which instance I want to draw, rather than cycle through all instances. I can’t figure out how to specify a weight or width value, or perhaps an interpolation value. Is this possible?

You can use pick an instance from the list. If you like to render something that you don’t have defined as an instance, You can do something like this

from GlyphsApp import *
import objc
GSInstance = objc.lookUpClass("GSInstance")
GSInterpolationFontProxy = objc.lookUpClass("GSInterpolationFontProxy")

instance = GSInstance()
instance.weightValue = 10
instance.widthValue = 100
instance.customValue = 0
instance.setFont_(myFont)
instanceFont = GSInterpolationFontProxy.alloc().initWithFont_instance_(myFont, instance)
instanceGlyph = instanceFont.glyphs["A"]
instanceLayer = instanceGlyph.layers[instanceFont.fontMasterID()]
drawPath(instanceLayer.bezierPath)
1 Like

I remember this used to work in previous versions but it is broken in 2.5. Did anything change? I now get the following error:

<untitled>:11: UninitializedDeallocWarning: leaking an uninitialized object of type GSInterpolatio    nFontProxy
Traceback (most recent call last):
  File "<untitled>", line 11, in <module>
AttributeError: 'GSInterpolationFontProxy' object has no attribute 'initWithFont_instance_'

the API has changed a bit:

replace this line:

instanceFont = GSInterpolationFontProxy.alloc().initWithFont_instance_(myFont, instance)

with this:

instanceFont = instance.interpolatedFontProxy
1 Like

I’m working on a DrawBot script and I used .interpolatedFont. On one computer this worked fine, but on another I got the following error (apologies for formatting):
File "GlyphsApp/GlyphsApp/__init__.py", line 3634, in <lambda>
File "GlyphsApp/GlyphsApp/__init__.py", line 3632, in Instance_FontObject
TypeError: 'NSKVONotifying_GSFont' object is not callable

If I use .interpolatedFontProxy it works again. As long as something works I don’t really care, but I thought that was weird.

Can you show your code? What version of Glyphs do you have on both machines?

Sure: https://pastebin.com/U0bMRkf6

I have version 2.5.1 (1140) on both computers. I work primarily on an iMac and also have a MacBook Pro that I don’t use often. I was updating everything on the MacBook yesterday and noticed I didn’t have the recommended modules. That button in the preferences window wasn’t working for the life of me so I downloaded and installed all three manually. Is it possible that is to blame?

Edit: I just checked again and .interpolatedFont doesn’t work on my iMac anymore, only .interpolatedFontProxy. I think the only thing that changed was I updated the DrawBot plugin.

You are right. There is a problem in the wrapper. I fixed it.