Change Font of Text Object?

Hi there!

How would one go about changing the font of a text object via the python api? I’ve searched the web far and wide and can’t seem to find any solution to this problem.

I found a thread on ba from 2011, but unsurprisingly, the solution no longer seems to work.

I load in the font using
bpy.ops.font.open(filepath="path-to-font")

this works, but thereafter, I have no idea how to assign that font to a text object.

Any help would be an absolute saving grace <3

Thanks in advance

i would recommend using bpy.data.fonts.load and you will get vector font data.

import bpy

# load font
data_font = bpy.data.fonts.load('C:\\WINDOWS\\Fonts\\JetBrainsMono-Regular.ttf')

# add text object
bpy.ops.object.text_add(enter_editmode=False, align='WORLD', location=(0, 0, 0), scale=(1, 1, 1))

# get text object
text_object = bpy.context.selected_objects[0]

# change font
text_object.data.font = data_font

more information : https://docs.blender.org/api/current/bpy.types.TextCurve.html

This worked beautifully! Thank you!