blf.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. Hello World Text Example
  3. ++++++++++++++++++++++++
  4. Example of using the blf module. For this module to work we
  5. need to use the OpenGL wrapper :class:`~bgl` as well.
  6. """
  7. # import stand alone modules
  8. import blf
  9. import bpy
  10. font_info = {
  11. "font_id": 0,
  12. "handler": None,
  13. }
  14. def init():
  15. """init function - runs once"""
  16. import os
  17. # Create a new font object, use external ttf file.
  18. font_path = bpy.path.abspath('//Zeyada.ttf')
  19. # Store the font indice - to use later.
  20. if os.path.exists(font_path):
  21. font_info["font_id"] = blf.load(font_path)
  22. else:
  23. # Default font.
  24. font_info["font_id"] = 0
  25. # set the font drawing routine to run every frame
  26. font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(
  27. draw_callback_px, (None, None), 'WINDOW', 'POST_PIXEL')
  28. def draw_callback_px(self, context):
  29. """Draw on the viewports"""
  30. # BLF drawing routine
  31. font_id = font_info["font_id"]
  32. blf.position(font_id, 2, 80, 0)
  33. blf.size(font_id, 50, 72)
  34. blf.draw(font_id, "Hello World")
  35. if __name__ == '__main__':
  36. init()