[Blender] Use the text drawing module from within the script

Characters can be displayed by creating a font image with only the OpenGL function explained in Using OpenGL from inside the [Blender] script. .. However, if you want to display characters for a little bit such as debugging, it will take some time to prepare the font image. For such a case, Blender provides a text drawing API to easily draw text from a script. For example, Blender's Official add-on, Screencast Keys, uses this feature to display the keys pressed in the last few seconds.

In this article, I will explain how to use Blender's text drawing API easily with samples.

sample

This is a sample to display a character string in "View 3D".

render_text.py


import bpy
import blf    #Text drawing module


bl_info = {
    "name": "Tutorial: Render text",
    "author": "Nutti",
    "version": (1, 0),
    "blender": (2, 74, 0),
    "location": "View3D > Tutorial: Render text with blf module",
    "description": "Tutorial: Render text with blf module.",
    "warning": "",
    "support": "COMMUNITY",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"
}


class TextRenderer(bpy.types.Operator):
    """Draw a string"""

    bl_idname = "view3d.text_renderer"
    bl_label = "Text renderer"

    __handle = None    #Drawing function

    #Register the drawing function in the "View3D" area
    @staticmethod
    def handle_add():
        TextRenderer.__handle = bpy.types.SpaceView3D.draw_handler_add(
            TextRenderer.render_text,
            (), 'WINDOW', 'POST_PIXEL')

    #Unregister the drawing function in the "View3D" area
    @staticmethod
    def handle_remove():
        if TextRenderer.__handle is not None:
            bpy.types.SpaceView3D.draw_handler_remove(
                TextRenderer.__handle, 'WINDOW')
            TextRenderer.__handle = None

    #Drawing function body in the "View3D" area
    @staticmethod
    def render_text():
        #Drawing the string "Suzanne on your View3D region"
        blf.size(0, 20, 72)            #Specify font size
        blf.position(0, 20, 150, 0)    #Specify drawing position
        blf.draw(0, "Suzanne on your View3D region")    #Drawing a string


#Processing when installing a script
def register():
    bpy.utils.register_module(__name__)
    TextRenderer.handle_add()


#Processing when uninstalling a script
def unregister():
    bpy.utils.unregister_module(__name__)
    TextRenderer.handle_remove()


if __name__ == "__main__":
    register()

How to use

  1. Install the script by referring to Blender Wiki page.
  2. Confirm that the string "Suzanne on your View3D region" is displayed in View3D.

20150730.png

Sample explanation

The basic explanation of the Blender script is introduced in the following article, so here we will focus on the explanation of the newly added elements. [\ Blender ] How to make a Blender plugin

import blf module

In order to use the text drawing API provided by Blender, you need to import the blf module.

import blf

Registration / deregistration of "View3D" drawing function

The following article explains how to register / unregister the function for drawing in the "View3D" area, so please refer to it. Use OpenGL from inside [Blender] script

"View3D" drawing function body

In the function body that draws in "View3D", the character string is drawn using the blf module.

First, the `` blf.size () `function is called to specify the font size to draw. Specify ** font size ** in the second argument and ** dpi ** in the third argument. The first argument is used when the original font is loaded using the blf module, but specify 0 when using the Blender standard font.

Then call the `blf.position ()` function to specify where to draw the string. The first argument is the same as the first argument of the `blf.size ()` function. In the 2nd to 4th arguments, specify ** the position to draw the character string ** (in the order of x coordinate, y coordinate, z coordinate).

Finally, the ``` blf.draw ()`` function draws the string. Specify ** the character string you want to draw ** in the second argument. The first argument is the same as the first argument of the ``blf.size ()` `` function.

    #Drawing function body in the "View3D" area
    @staticmethod
    def render_text():
        #Drawing the string "Suzanne on your View3D region"
        blf.size(0, 20, 72)            #Specify font size
        blf.position(0, 20, 150, 0)    #Specify drawing position
        blf.draw(0, "Suzanne on your View3D region")    #Drawing a string

blfThe module also provides various other useful apis. blfYou can check the api document provided by the module from the following url. If you want to know other APIs, please refer to it. http://www.blender.org/api/blender_python_api_2_60_6/blf.html

Recommended Posts

[Blender] Use the text drawing module from within the script
[Blender] Use OpenGL from inside the script
Use the nghttp2 Python module from Homebrew from pyenv's Python
How to use the optparse module
Use blender as a python module
Use the Flickr API from Python
How to use the ConfigParser module
Use Django from a local Python script
Get only the text from the Django form.
Download the image from the text file containing the URL
How to use the graph drawing library Bokeh
Use the e-paper module as a to-do list
Examine Python script bottlenecks with the cProfile module
Access the variables defined in the script from the REPL
How to pass arguments when invoking python script from blender on the command line