0
$\begingroup$

I would like my users to select a font, essentially opening a folder, so they can choose. My fonts will be loaded in a special folder, such as C:/myfolder/fonts. Is there an easy script to do this?

$\endgroup$
0
$\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$
  • $\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$ – Michael Teiniker May 25 at 12:15
  • 1
    $\begingroup$ Note: blender.stackexchange.com/questions/2848/… $\endgroup$ – Ray Mairlot May 25 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$ – Hikariztw May 25 at 13:10
  • 2
    $\begingroup$ bpy.data.fonts.load(), which, when supplied with a filepath, will return a font datablock. $\endgroup$ – Ray Mairlot May 25 at 15:08
  • $\begingroup$ 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. $\endgroup$ – Michael Teiniker May 25 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.