1

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?

brockmann
  • 12,613
  • 4
  • 50
  • 93
Michael Teiniker
  • 1,094
  • 1
  • 12
  • 26

1 Answers1

3

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

HikariTW
  • 7,821
  • 2
  • 18
  • 37
  • 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() – Michael Teiniker May 25 '19 at 12:15
  • 1
    Note: https://blender.stackexchange.com/questions/2848/why-avoid-bpy-ops?r=SearchResults – Ray Mairlot May 25 '19 at 12:20
  • @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? – HikariTW May 25 '19 at 13:10
  • 2
    bpy.data.fonts.load(), which, when supplied with a filepath, will return a font datablock. – Ray Mairlot May 25 '19 at 15:08
  • I'm using the pie menu editor, so I have my button on the panel menu already. Once you click it, all I need is for the file browser to open up. Like when you press the folder in the font properties. No need for it to open my folder or choose a font. Sorry if I haven't been clear enough. The user can then navigate to the font filder themselves. – Michael Teiniker May 25 '19 at 22:38
  • 1
    @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 – HikariTW May 26 '19 at 00:59