# draw text always face the viewer in 3D view

I'd like to write a script to draw some text in 3D view, and I need it always face the viewer, how can I achieve this?

update: My own answer as below.

• What have you got so far? Where are you stuck and what didn't work as expected? – Duarte Farrajota Ramos Mar 16 '17 at 17:32
• What do you mean by blf? – Georges Mar 16 '17 at 17:41
• Do you expect someone to write a script for you, or have you started working on one? It would be useful if you share what you have so far, and explain what precisely is giving you trouble. – Timaroberts Mar 17 '17 at 0:35
• @DuarteFarrajotaRamos Sorry for my unclear question, I've update it. – NGE Mar 17 '17 at 2:32
• You should post that as an answer bellow instead, preferably with a short description of what you did wrong, and how you fixed it. – Duarte Farrajota Ramos Mar 17 '17 at 2:55

Thank you for all of you comments, and sorry for my rough question.

But I guess I have found the answer:

I've used wrong arguement for the bpy.types.SpaceView3D.draw_handler_add() function. 'POST_PIXEL' shold be used here as the 4th argument, but 'POST_VIEW' was used. I guess 'POST_PIXEL' is for 2D text, and 'POST_VIEW' is for 3D text.

Code and screenshoot as below:

import blf
import bgl
import bpy

RED = (1.0, 0.0, 0.0)
GREEN = (0.0, 1.0, 0.0)

position_1 = (50, 50, 0)
position_2 = (0, 0, 0)

def draw(position, color):
blf.position(0, *position)
blf.size(0, 20, 72)
bgl.glColor3f(*color)
blf.draw(0, "hello,world")

_handle = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, RED), 'WINDOW', 'POST_PIXEL')
_another_handle = bpy.types.SpaceView3D.draw_handler_add(draw, (position_2, GREEN), 'WINDOW', 'POST_VIEW')


update:

I've asked someone in our community, and I was given some tips about the 3rd argument, which maybe represent for region type and enumerate in 'WINDOW', 'HEADER', 'CHANNELS', 'TEMPORARY', 'UI', 'TOOLS', 'TOOL_PROPS', 'PREVIEW'.

Code and screenshoot as below for your reference:

import blf
import bgl
import bpy

RED = (1.0, 0.0, 0.0)
GREEN = (0.0, 1.0, 0.0)
BLUE = (0.0, 0.0, 1.0)

position_1 = (0, 0, 0)

def draw(position, color, text):
blf.position(0, *position)
blf.size(0, 20, 72)
bgl.glColor3f(*color)
blf.draw(0, text)

_handle_1 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, RED, 'WINDOW'), 'WINDOW', 'POST_PIXEL')

But I‘m still confused about the draw_handler_add funciton. I've read the api documentation for this, but is shows "Undocumented". Anyone can help me, or explain it for me?