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

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, though.

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

Which app version?

Can you post your code?

the version is 2.6.1.

Here it is. (951 Bytes)

#MenuTitle: Show Component Property Value
# -*- coding: utf-8 -*-
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
                # 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
                #look up all smartComponets one more time
                for axis in component.component.smartComponentAxes:
                    #if there was one, then, it might had a maximum value
                        print('Glyph:%s, %s Value: %d' %(unichr(int(layer.parent.unicode,16)),propertyToLookFor,axis.topValue))
                #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))

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.

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.

I wonder if the bug has been solved.
I also found that when using pieceValueForKey (Key) to get the value of an attribute. If this property is set to a maximum value, the result is 9.22337203685e+18.

1 Like