bpy blf api example update

With changes from Campbell Barton as well.
This commit is contained in:
Dalai Felinto 2018-04-17 12:25:51 +02:00
parent aff71a7fdc
commit c558d8fa7c

@ -2,43 +2,44 @@
Hello World Text Example Hello World Text Example
++++++++++++++++++++++++ ++++++++++++++++++++++++
Blender Game Engine example of using the blf module. For this module to work we Example of using the blf module. For this module to work we
need to use the OpenGL wrapper :class:`~bgl` as well. need to use the OpenGL wrapper :class:`~bgl` as well.
""" """
# import game engine modules
from bge import render
from bge import logic
# import stand alone modules # import stand alone modules
import bgl import bgl
import blf import blf
import bpy
font_info = {
"font_id": 0,
"handler": None,
}
def init(): def init():
"""init function - runs once""" """init function - runs once"""
# create a new font object, use external ttf file import os
font_path = logic.expandPath('//Zeyada.ttf') # Create a new font object, use external ttf file.
# store the font indice - to use later font_path = bpy.path.abspath('//Zeyada.ttf')
logic.font_id = blf.load(font_path) # Store the font indice - to use later.
if os.path.exists(font_path):
font_info["font_id"] = blf.load(font_path)
else:
# Default font.
font_info["font_id"] = 0
# set the font drawing routine to run every frame # set the font drawing routine to run every frame
scene = logic.getCurrentScene() font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(
scene.post_draw = [write] draw_callback_px, (None, None), 'WINDOW', 'POST_PIXEL')
def write(): def draw_callback_px(self, context):
"""write on screen""" """Draw on the viewports"""
width = render.getWindowWidth()
height = render.getWindowHeight()
# OpenGL setup
bgl.glMatrixMode(bgl.GL_PROJECTION)
bgl.glLoadIdentity()
bgl.gluOrtho2D(0, width, 0, height)
bgl.glMatrixMode(bgl.GL_MODELVIEW)
bgl.glLoadIdentity()
# BLF drawing routine # BLF drawing routine
font_id = logic.font_id font_id = font_info["font_id"]
blf.position(font_id, (width * 0.2), (height * 0.3), 0) blf.position(font_id, 2, 80, 0)
blf.size(font_id, 50, 72) blf.size(font_id, 50, 72)
blf.draw(font_id, "Hello World") blf.draw(font_id, "Hello World")
if __name__ == '__main__':
init()