Key error in GSComponent-smartComponentValues when a custom property has its top value


#1

When a smart component’s custom property has its top value, Glyphs doesn’t recognize the component.

For example, I added a property named ‘StemWidth’ in a smart component with a bottom value of ‘0’ and a top value of ‘240’. When the value was set at 220, I could access and print the value with component.smartComponentValues[‘StemWidth’]. However, when the value was set at 240, the KeyError Exception happened. Also, when I printed the dictionary, I couldn’t find the ‘StemWidth’ property. But it was still in the smartComponentAxes.name, though.

Plus, this error didn’t occur when the property was Height or Width.


#2

Which app version?


#3

Can you post your code?


#4

the version is 2.6.1.


#6

Here it is.

showComponentPropertyValue.zip (951 Bytes)

#MenuTitle: Show Component Property Value
# -*- coding: utf-8 -*-
__doc__="""
Glyphs script for showing the value of the requested smart component property
"""
# Author : todoi
# Date : 2019/02/01

from GlyphsApp import *


componentSearch ='_partT1.V-ddigeut-2a' # change here!
propertyToLookFor='Width' # change here!
# glyph selection is needed
if len(Glyphs.font.selectedLayers)==0:
    print('Select some glyphs first')

#for glyphs e.g.) 'Ddag-ko','Ddarh-ko',...
for layer in Glyphs.font.selectedLayers:
    #for components e.g.) dot, stem, ttigeut, ...
    for component in layer.components:
        if component.componentName==componentSearch:
            #try to return the value of seeked property
            try:
                # first %s: glyph name
                # second %s: property that you required
                # final %d: value of that property
                print('Glyph:%s, %s Value: %d' %(unichr(int(layer.parent.unicode,16)),propertyToLookFor,component.smartComponentValues[propertyToLookFor]))
            #KeyError occurs in 2 circumstances
            #1st: the value of the property is at the maximum: BUG
            #2nd: there's no such property
            except KeyError:
                #if there was actually a propery that you required
                myBoolean=True
                #look up all smartComponets one more time
                for axis in component.component.smartComponentAxes:
                    #if there was one, then, it might had a maximum value
                    if axis.name==propertyToLookFor:
                        print('Glyph:%s, %s Value: %d' %(unichr(int(layer.parent.unicode,16)),propertyToLookFor,axis.topValue))
                        myBoolean=False
                        break
                #if there is not, then you probably entered a wrong property name
                if myBoolean:
                    print('Glyph:%s, the glyph has no such property: %s'%(unichr(int(layer.parent.unicode,16)),propertyToLookFor))

#7

The script seems to work fine for me. Can you send me a font where it doesn’t.

One reason might be that Glyphs doesn’t store a value in the smartComponentValues if it is equal to the default.


#8

The script itself works fine, but I think the value that is equal to the default should be visible, too.

When the value is equal to the default, it is not stored in the smartComponentValues but the component axis name is still stored in the smartComponentAxes.names. Using that, I handled the KeyError making the script work fine. Still, I think the value should be stored in the smartComponentValues even if it has the default value, and thought it was kind of a bug.