How to add new metric to a master with a script?

I want to add a new metric to a master using a script.

master = Font.selectedFontMaster

newMetric = GSMetricValue
newMetric.position = 100
newMetric.name = "Test"
newMetric.overshoot = 0
newMetric.filter = None
newMetric.metric = None

master.metrics.append(newMetric)
print(master.metrics)

But this doesn’t work. It seems that I can’t use GSMetricValue() instead of GSMetricValue

TypeError: Use class methods to instantiate new Objective-C objects

The error message already explains what you need to do:

newMetric = GSMetricValue.alloc().init()

It seems that I can’t set an attribute using GSMetricValue.alloc().init()

AttributeError: can't set attribute
newMetric = GSMetricValue.alloc().init()
newMetric.position = 100

print(newMetric)

Only “position” and “overshoot” work.
All others produce an AttributeError.

I’m still struggling with this issue.
I have found some methods to set position, overshoot, and filter, but none to set the name.

font = Glyphs.font
master = font.selectedFontMaster

new_metric = GSMetricValue.alloc().init()
new_metric.setPosition_(100)
new_metric.setOvershoot_(10)
print(new_metric)

How do I set the name attribute and append new_metric to master.metrics ?

The name (and actually also the .filter) are stored in the GSMetricin font.metrics. You can get to the corresponding object:new_metric.metric` but only after you added it to the font.

1 Like

And how to add it to the font ?

master.metrics.append(new_metric)

This don’t work.

Hello,

Have anyone solved this problem?
I couldn’t find any clue about adding GSMetricValue to the master.metrics.
As HugoJ has mentioned, I can’t add GSMetricValue with ‘append()’ even though master.metrics is a ‘list’.
This thread is the only one I can find about master.metrics in this forum.

Thanks in advance.

What exactly are you trying to do. Add a new metric to the list or set the value for an existing one?

Hello,

I’d like to add a new metrics to the list.
(I have tested setting value for an existing one already and gotten successful result.)

Thanks,

Does this help?

from GlyphsApp import GSMetricsTypeBodyHeight

metric = GSMetric.new()
metric.type = GSMetricsTypeBodyHeight
Font.addMetric_(metric)
master = Font.masters[0]
metricValue = GSMetricValue.new()
metricValue.position = 800
metricValue.overshoot = 20
master.setMetricValue_forId_(metricValue, metric.id)
1 Like

or

type = GSMetricsTypeBodyHeight
name = None # or set a name
filter = None # or add a filter like "script == greek"
master.setMetricPosition_overshoot_type_name_filter_(800, 20, type, name, filter)

If there is a metrics with matching type, name and filter, it will overwrite it, otherwise it adds a new metric

1 Like

It is really helpful! I appreciate your support.

@GeorgSeifert

Is there some way I can see a list of all possible the different type classes?

For example how would I set the type to “Other”?

You can find some infos here :
https://docu.glyphsapp.com/Core/Constants/GSMetricsType.html