3
$\begingroup$

I'm working on a part of my plugin easyText. (http://ranincodes.wix.com/easytext)


I want the import Font part to automaticly check if the Font is already imported. How do I get the name of the selected file (=filebuff)? If it is, the font should not be imported


This is the part of the code wich is important for it

#Atribute
atr = bpy.types.Scene
atr.font = StringProperty(name = "Text Font", description = "Font of the Text")
atr.filebuff = StringProperty(name = "", description = "Choose Font to Import", subtype="FILE_PATH")

#window
class windowFont (bpy.types.Panel):
    bl_space_type = "VIEW_3D"
    bl_region_type = "TOOLS"
    bl_label = "Import Font"
    bl_category = "easyText"

    def draw(self, context):
        scene = bpy.context.scene
        layout = self.layout
        layout.label("Choose + Import Font")
        col = layout.column(align = True)
        layout.prop(scene, "filebuff")
        layout.operator("load.font", text="Import", icon="LOAD_FACTORY")

#button
class OBJECT_OT_buttonLoad(bpy.types.Operator):
    bl_label = "Load Font"
    bl_idname = "load.font"
    bl_description = "Import the choosen Font"

    def execute(self, context):
        scene = bpy.context.scene
        fontb = scene.filebuff
        bpy.ops.font.open(filepath=fontb, relative_path=False)
        return{'FINISHED'}  

UI:enter image description here


Thank you!

| improve this question | | | | |
$\endgroup$
3
$\begingroup$

Instead of using the bpy.fonts.open(... operator you can use the load() command for Font Datablocks which has a check_existing property built in, which should do exactly what you want:

bpy.data.fonts.load(filepath=fontb, check_existing=True)

When check_existing is set to True if a font datablock already exists in the blend file from that path then it won't be loaded, otherwise it will.

For more information on why it is generally better avoid bpy.ops commands read here:

Why avoid bpy.ops?

| improve this answer | | | | |
$\endgroup$

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.