Importing UFOs into masters

I’m wanting to import a set of UFOs into masters. Manually I know that I can open both UFOs, go to the primary font (Regular), go to Font Info… Masters, then Add Other Font and choose the additional font (Bold) to be used as a master. Is there any way to do that with a script?

A script that gives you an open dialog where you select multiple files, and they are automatically imported as Masters into a new Glyphs file?

Should be possible, yes.

That would be great, as long as it was done so that the clear regular/bold relationship was preserved. We’re opening a family of UFOs and want the style relationships preserved. However we also have another family where (for Windows family structure limitations) members have separate names but we’d like to be able to import them into a single family. So what we’re really looking for is some code examples for how we can import UFOs into masters. Then we can roll our own script to do it as we need to.

I sent a similar question to Georg recently, and my suggestion was that the script read a control file supplied by the user to control the order of import. In my email I mentioned David Berlow’s “Decovar” example variation font for reference. It has such a control file, or at least that’s what I think it is used for.

That would be a problem for the instances, not masters.

Yes - Decovar uses a .designspace file. We’re intending to also use a control file in the project to define family structure. We’ve considered using a .designspace but that may be overkill for us, esp. as most of these families don’t include interpolated instances.

Defining instances would be great, but for now we still can’t figure out how to script opening multiple UFOs into a single glyphs file. Once we have that we’ll then probably want to script the instances too, but we’re not that far. Would using .designspace help? I can’t find any direct support for it in Glyphs.

This should work with UFOs as well:

# Open Dialog:
files = GetOpenFile(allowsMultipleSelection=True)

newFont = None

for thisFile in files:
	# open each of the selected files:
	thisFont = Glyphs.open(thisFile, showInterface=False)
	
	if newFont is None:
		# if it is the first font opened, make a new font with its contents:
		newFont = GSFont(thisFont)
	else:
		# if it is 2nd or later font, add its masters to the newFont:
		for thisMaster in thisFont.masters:
			newFont.masters.append(thisMaster)

# open the font in the Glyphs UI:
Glyphs.fonts.append( newFont )

This is a possible structure. You may want to add checks at every stage, like is it not empty, is it really a font, etc. I took most of this from docu.glyphsapp.com.

Setting instances is pretty straight forward: Create a new GSInstance, set its properties and append it to thisFont.instances. That’s it.

If you open a .ufo, it should have the needed instances already. It would be good to have a (simplified) sample font.

Ah. Very nice. Many thanks!

Not quite working. I needed to change
newFont = GSFont(thisFont)
to
newFont = thisFont
to get the newFont to contain anything at all. Then ‘newFont.masters.append’ creates a new master in the list (can see that in Font Info), but the master has no glyphs in it.

The script would needs to do a lot more. I’ll refactor the function in Glyphs that you can use it from a script.

OK thanks!

Please try this in the latest version (2.4.2-986)

f1 = GSFont("path/to/font1.ufo")
f2 = GSFont("path/to/font2.ufo")
f1.addFontAsNewMaster_(f2.masters[0])
f1.instances.append(f2.instances[0].copy())
Glyphs.fonts.append(f1)

Works very nicely now - thanks!

1 Like