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.