2
$\begingroup$

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.

$\endgroup$
7
  • 2
    $\begingroup$ What have you got so far? Where are you stuck and what didn't work as expected? $\endgroup$ Commented Mar 16, 2017 at 17:32
  • $\begingroup$ What do you mean by blf? $\endgroup$
    – Georges D
    Commented Mar 16, 2017 at 17:41
  • $\begingroup$ 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. $\endgroup$
    – Timaroberts
    Commented Mar 17, 2017 at 0:35
  • $\begingroup$ @DuarteFarrajotaRamos Sorry for my unclear question, I've update it. $\endgroup$
    – NGE
    Commented Mar 17, 2017 at 2:32
  • $\begingroup$ You should post that as an answer bellow instead, preferably with a short description of what you did wrong, and how you fixed it. $\endgroup$ Commented Mar 17, 2017 at 2:55

1 Answer 1

4
$\begingroup$

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')

screenshoot for result

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')
_handle_2 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, GREEN, 'HEADER'), 'HEADER', 'POST_PIXEL')
_handle_3 = bpy.types.SpaceView3D.draw_handler_add(draw, ((100, 0, 0), BLUE, 'CHANNELS'), 'CHANNELS', 'POST_PIXEL')
_handle_4 = bpy.types.SpaceView3D.draw_handler_add(draw, ((250, 0, 0), RED, 'TEMPORARY'), 'TEMPORARY', 'POST_PIXEL')
_handle_5 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, GREEN, 'UI'), 'UI', 'POST_PIXEL')
_handle_6 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, BLUE, 'TOOLS'), 'TOOLS', 'POST_PIXEL')
_handle_7 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, RED, 'TOOL_PROPS'), 'TOOL_PROPS', 'POST_PIXEL')
_handle_8 = bpy.types.SpaceView3D.draw_handler_add(draw, ((400, 0, 0), GREEN, 'PREVIEW'), 'PREVIEW', 'POST_PIXEL')
_handle_1 = bpy.types.SpaceView3D.draw_handler_add(draw, (position_1, RED, 'WINDOW'), 'WINDOW', 'POST_VIEW')

another screenshoot

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?

$\endgroup$
1
  • $\begingroup$ I'm sure you've got this figured out by now, but I think this link would be handy here. By calling draw_handler_add you are providing the instructions which are used every time the SpaceView3D needs to be redrawn ( due to a zoom, pan, window resize etc ). The problem then becomes how to cancel these instructions. $\endgroup$
    – JJones
    Commented Oct 6, 2019 at 10:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .