Trouble with blf.size()! (FONT SIZE IN BLF MODULE)

I finally got a script working for creating text in game - But now, I’m having trouble with font size…

import bge
import blf
from bge import logic

def TextRender():

    text = "Hi"
    blf.position(0, 0, 0, 0) 
    blf.size(0, 12, 72)
    blf.draw(0, text)

logic.getCurrentScene().post_draw = [TextRender]

If you run this in blender - you’ll see it’s hard to manipulate the blf.size() values to get something decent. Anyone know a way around this?

yes you can. there are different ways of doing it:

  1. using blf python module to write straight to the screen. (blender 2.5)
  2. using Blender Font objects and the .text python property on them. (blender 2.5)
  3. using bitmap font planes and change their obj.[“Text”] property (blender 2.49 and 2.5)

Hey, cool. I would like to learn how to do the blf python module. If you can code a snippet quickly, it would be appreciated! I’m searching for a tutorial in the meanwhile… this thread has a promising future to be SOLVED!

Can someone help me code a snippet using… http://www.blender.org/documentation/blender_python_api_2_56_0/blf.html

Guessing:



import blf

font = blf.Load('Arial.ttf') # Load the font
text = "Hi, there!"            # Set the string to write (I like Python's strings; so much simpler than C++)
blf.Position(font, 0, 0, 0) # Position the font at point 0, 0, 0
blf.Draw(font, text)         # And draw the text onscreen


That should load the font, position it at the zero point, and then draw it onscreen. However, this is untested from me, so it may not work. It would be nice if the blf module was updated to put these functions inside of the returned font object (i.e. font.position = [x, y, z], font.Draw(text), etc).

EDIT: It should be that simple, but I really don’t know.

When using the blf module, I get no errors but I also see no text!

I tried

import bge
import blf
from bge import logic

blf.draw(0, 'Hello World!')
blf.position(0, 3, 3, 3)

But nothing happens! Note: blf.draw((fontid), (string)) , blf.position((fontid), x,y,z)

Ouu, I’m trying this out now!

SolarLune, it didn’t work… but it’s only because you typed in the methods with capitals. lol… It’s blf.load(), blf.position(), blf.draw()…
Also - the ‘Arial.ttf’ font is was not able to find - How do I get it? And how do I get it to find it?

EDIT: Who the heck made the blf module - it’s terribly organized as you said SolarLune.

Oh. I thought that the functions were capitalized, for some reason, heh.

As to the ‘Arial.ttf’ font, it was just an example - you can find it in your Windows’ fonts folder, though. As to whether you can use it and redistribute it in your (commercial) game or not is a different story - you can find free fonts online. EDIT: Like from this site. Find one, download it, and put it into a navigable folder (like next to your blend file, or in a ‘Resources’ folder, or something to that effect), and use that instead.

Ouu - ok. Thank you again SolarLune!

I tried again - and failed miserably… I have the font file and the blend file all in the same file - but it still cannot find the font file…

import bge
import blf
from bge import logic

logic.addScene('HUD', 1)

font = blf.load('arial.ttf')
text = "Hi, there!"
blf.position(font, 0, 0, 3)
blf.draw(font, text)

Hm. Maybe you need to expand the path? Try this instead of the font = line:


path = logic.expandPath('//font.ttf') # Expand the path out to the font file.
font = blf.load(path)

That might work. If there’s still an error, try printing out path before loading the font, so that you can see if the path is correct.

By the way, that site has some nice pixel and kingly (Black Letter) fonts available. You can never have too much good stuff.

This is strange…

import bge
import blf
from bge import logic

#logic.addScene('HUD', 1)

path = logic.expandPath('//arial.ttf')
font = blf.load(path)
text = "Hi, there!"
blf.position(font, 0, 0, 3)
blf.draw(font, text)

This gives no errors - but still! Nothing is seen on screen!

Has anyone ever confirmed that the blf module for writing text works? so far - I’ve had no luck what so ever. does blender recognized .ttf files?

I believe it should work… Maybe you should check out BGUI - Moguri made it. It’s a method to draw images, videos, and text onscreen with the BGE. It seems to work pretty well - I’m going to check it out, myself. It appears to also come with an example (which is good, because I didn’t see one while looking around).

Blargh. I just checked it out, but it’s pretty complex. There’s got to be an easy way to do this…

NEW problem… :frowning:

The text size seems very hard to manipulate, anyone know a way around this?
Also, blf.aspect(fontid, float) doesn’t seem to do anything?

import bge
import blf
from bge import logic

def TextRender():

    text = "Hi"
    blf.position(0, 0, 0, 0) 
    blf.size(0, 50, 72)
    blf.draw(0, text)

logic.getCurrentScene().post_draw = [TextRender]

OH! Ok, I know what you are talking about… I understand this method very well - I am an avid user of it. What I want to do however is to only script it. Thank you for the suggestion however Rubbernuke!

Oddly, I can’t get any text to draw, even with your example script. Are you sure that’s all that’s in there? Is there anything else that I could be missing?

Well - nope… that script has everything…