2

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.

NGE
  • 347
  • 2
  • 10

1 Answers1

4

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?

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125
NGE
  • 347
  • 2
  • 10
  • 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. – JJones Oct 06 '19 at 10:13