1
$\begingroup$

I would like my users to select a font, essentially opening a folder so they can choose from a custom library of fonts. The fonts are located in a folder, such as C:\myfolder\fonts.

Is there an easy way to do this?

$\endgroup$

1 Answer 1

3
$\begingroup$

Update: Use bpy.data.fonts.load() instead -@Ray Mairlot

If I understand your question: You want to load a directory that contain font files. And the you can select those font in blender font selector?

Then use bpy.ops.font.open

In Blender 2.80 with Python 3.7.0:

import os
import bpy

dir_path = 'C:/myfolder/fonts'

directory = os.fsencode(dir_path)

for file in os.listdir(directory): #list file
     filename = os.fsdecode(file) #get filename from file
     bpy.ops.font.open(filepath=os.path.join(dir_path, filename)) #join the path

Update

for a button and panel in properties panel (Blender 2.8, Windows):

import bpy
import os

class OpenFontDir(bpy.types.Operator):
    bl_idname      = 'data.openfontdir'
    bl_label       = "load fontdir"

    directory : bpy.props.StringProperty(
        name="Directory of Font",
        default="",
        subtype='DIR_PATH')

    def execute(self,context):
        directory = os.fsencode(self.directory)

        for file in os.listdir(directory): #list file
             filename = os.fsdecode(file) #get filename from file
             bpy.data.fonts.load(os.path.join(self.directory, filename), check_existing=True) #join the path

        self.report({"INFO"},"load from: {!r}".format(self.directory))
        return {"FINISHED"}

    def invoke(self, context, event):
        context.window_manager.fileselect_add(self)
        return {'RUNNING_MODAL'}


class Dirfontpanel(bpy.types.Panel):
    bl_idname      = "CATEGORY_PT_Dirfontpanel"
    bl_label       = "Dir font panel"
    bl_space_type  = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context     = "data"

    def draw(self, context):
        layout = self.layout
        scene = context.scene
        layout.label(text="Open directory")
        layout.operator(OpenFontDir.bl_idname)

classes = (
    Dirfontpanel,
    OpenFontDir
)

def register():
    from bpy.utils import register_class
    for clss in classes:
        register_class(clss)

def unregister():
    from bpy.utils import unregister_class
    for clss in reversed(classes):
        unregister_class(clss)

if __name__ == "__main__":
    register()

Operator and panel

You can't directly add object on the properties > object > font panel.

The operator: enter image description here

$\endgroup$
6
  • $\begingroup$ Sorry , i can't get it to work. All it needs to do is open up the folder option on the font menu. Then user can navigate to my folder themselves. I have tried this, but it won't work. Taken from the info editor..... bpy.context.space_data.context = 'DATA' bpy.ops.font.open() $\endgroup$ May 25, 2019 at 12:15
  • 1
    $\begingroup$ Note: blender.stackexchange.com/questions/2848/… $\endgroup$ May 25, 2019 at 12:20
  • $\begingroup$ @RayMairlot Not aware of it, I apologies. But I can't find the replacement of it, could you please point out what did I miss of it? $\endgroup$
    – HikariTW
    May 25, 2019 at 13:10
  • 2
    $\begingroup$ bpy.data.fonts.load(), which, when supplied with a filepath, will return a font datablock. $\endgroup$ May 25, 2019 at 15:08
  • 1
    $\begingroup$ @MichaelTeiniker The thing you described is the class OpenFontDir(bpy.types.Operator) part. It will be register as a operator and can be used by Pie Menu Editor $\endgroup$
    – HikariTW
    May 26, 2019 at 0:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .