Add stem values with script


I was wondering if there is a way to add stem values with a script.
Ideally I want to wrap everything up in a little tool with two buttons. You select two points and with the buttons you either add the x distance as a vertical stem or the y distance as horizontal stem.

I see that I can print out the stem values but I did not find any information about adding new values (or controlling if it’s vertical or horizontal)

So I guess it’s not possible?

It is possible:

stem = GSMetric()
stem.horizontal = False # or True = "Some Name" # name has to be unique
# font.stems.append(stem) # this is broken at the moment
font.addStem_(stem) # use this until version 3208
master.stems[] = 123

I added it to the docu.


Thanks a lot! :slight_smile:

So this has somewhat worked out.

I have a little tool with two buttons, I also figured out how to add stems.
The problem now is, when I am in the Thin master and select two points, it adds a stem with the value 10, but there are also stems added for all other masters with the value 0. (I get where that comes from, but its not really helpful).

Furthermore the script works not very reliable, meaning if I go through 3 masters where I each measure horizontal and vertical, in the best case I would end up with 2 stems. Usually I am in between 4-6 stems (most of them with a value of 0) and I don’t really get it. It’s like sometimes it takes in the value when creating a new stem and sometimes it doesn’t.
I tried updating the interface after every button press and I also closed and opened up the tool window but somehow it feels as if only a third of my actions seem to actually execute the whole script properly.

It is difficult to help without seeing your code.

What you probably need to do is to come up with a naming scheme so that you can check if a stem you like to set is already there.

Or, when you select two node and your script measures them to add as a stem, the script would go to all masters, find those two nodes in each and makes a stem value for each masters. That avoids the zero values.

Helpful function for this are indexPath = layer.indexPathOfNode_(node) and node = layer.nodeAtIndexPath_(indexPath)

I sent a message with script and test file :slight_smile:

This line is not correct:

        # check if a stem with that name is already present if so, update the value, if not, add it
        if in master.stems:

I send you the corrected script back.

stem = master.font.stemForName_(stem_name)
if not stem:
    print("stem not present yet!")
    stem = GSMetric() = stem_name
    stem.horizontal = horizontal
master.stems[] = value
1 Like