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?
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?
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
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()
You can't directly add object on the properties > object > font panel.
bpy.data.fonts.load(), which, when supplied with a filepath, will return a font datablock. – Ray Mairlot May 25 '19 at 15:08class 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