How to re assign font in 2.79

i got some 2.79 file with let say 200 text objects
using several font like Bfont Bfont.006 ect.

now i need a little script to find all objects with Bfont and re assign to another font like Bfont.006

i got this code snippet but cannot re assign font


cnt = 0

for ob in bpy.data.objects:

#	print (' ob type = ', ob.type)
	if ob.type == 'FONT':
#		print ('ob = ' , ob , ' ob type  = ', ob.type)
		t1 = ob.data.body 
#		print ('txt body = ', t1)
#		print (' dir = ', dir ( ob.data ) )
		fnt = ob.data.font

#		print (' dir = ', dir ( ob.data.font) )
		if fnt.name=="Bfont" :
			print ('t1 =', t1 ,' font = ', fnt ,' name =', fnt.name )
#			fnt.name="Bfont.006"
			cnt +=1


print ()
print ('cnt =', cnt)


any help appreciated

thanks
happy bl

anyone can help with this ?

thanks
happy bl

This replaces all text objects.

import bpy

def replace_fonts(find_name, replace_name, font_type):

    fonts = bpy.data.fonts

    find_font = fonts.get(find_name)
    replace_font = fonts.get(replace_name)

    font_eval = "_%s" % font_type if font_type != 'regular' else ""

    if (find_font and replace_font and font_eval in 
        {'', '_bold', '_italic', '_bold_italic'}):

        for o in bpy.data.objects:
            if o.type == 'FONT':
                if getattr(o.data, "font%s" % font_eval) == find_font:
                    setattr(o.data, "font%s" % font_eval, replace_font)

# search for
find_name = "Bfont"

# replace with
replace_name = "Bfont.002"

# font type to replace: "regular", "bold", "italic", "bold_italic"
font_type = "bold_italic"

if __name__ == '__main__':
    replace_fonts(find_name, replace_name, font_type)

what value for font_type

is it like = Regular ?

I use this and get error on font type

replace_fonts( Bfont , Bfont.006 , font_type)

I just saw that I also get this error on font

Warning: LIB: VFont: ‘Bfont’ missing from 'H:\Users

so this font does not seem to even exist in 2.79
but the worst is that in 2.8 this crash 2.8 hard !

thanks
happy bl

here is sample file to test it

Font-ex1.blend (2.6 MB)

thanks
happy bl

beginning to work
all parameters are strings!

now this replace only one type of font at a time

I guess I need to call it 4 times to replace all type !

can you explain this line

font_eval = “_%s” % font_type if font_type != ‘regular’ else “”

does it gives a logical value ?

thanks
happy bl

font_eval just converts the strings regular, bold, italic and bold_italic into qualified property names, which are needed to access the properties using getattr and setattr.

So if you type bold in font_type, it gets converted to font_bold. When you type regular it is converted into just font.

If you want to replace all font types with just one font, here’s an expanded version of the same script with a new keyword all to replace all font types of the object:

import bpy

def replace_fonts(find_name, replace_name, font_type):

    fonts = bpy.data.fonts
    
    font_types = {"", "_bold", "_italic", "_bold_italic"}

    find_font = fonts.get(find_name)
    replace_font = fonts.get(replace_name)

    font_eval = "_%s" % font_type if font_type not in {'regular', 'all'} else ""

    if (find_font and replace_font and font_eval in 
        {'', '_bold', '_italic', '_bold_italic'}):

        for o in bpy.data.objects:
            if o.type == 'FONT':
                if font_type == 'all':
                    for ft in font_types:
                        setattr(o.data, "font%s" % ft, replace_font)
                    
                elif getattr(o.data, "font%s" % font_eval) == find_font:
                    setattr(o.data, "font%s" % font_eval, replace_font)

# search for
find_name = "Bfont.001"

# replace with
replace_name = "Bfont.002"

# font type to replace: "regular", "bold", "italic", "bold_italic", "all"
font_type = "all"

if __name__ == '__main__':
    replace_fonts(find_name, replace_name, font_type)

very usefull to get rid of this bad Bfont

now just have to modify 1000 files LOL

thanks
happy bl

1 Like