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.
Blender Stack Exchange is a question and answer site for people who use Blender to create 3D graphics, animations, or games. It only takes a minute to sign up.
Sign up to join this communityI'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.
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')
_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')
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?