Skip to content

Instantly share code, notes, and snippets.

@CGArtPython
Created August 26, 2022 16:23
Show Gist options
  • Save CGArtPython/0a116fd19d66ecf2bd3ad42c18dc1bd8 to your computer and use it in GitHub Desktop.
Save CGArtPython/0a116fd19d66ecf2bd3ad42c18dc1bd8 to your computer and use it in GitHub Desktop.
A Blender Python script that draws text next to the verts of a active mesh
import bpy
import blf
from bpy_extras.view3d_utils import location_3d_to_region_2d
def draw_text_on_verts():
font_id = 0
region = bpy.context.region
rv3d = bpy.context.space_data.region_3d
for vert in bpy.context.active_object.data.vertices:
# get the location of the vert on the Viewport
coord = vert.co
matrix_world = bpy.context.active_object.matrix_world
vector3d = matrix_world @ coord
x, y = location_3d_to_region_2d(region, rv3d, vector3d)
# draw text
blf.position(font_id, x, y, 0)
blf.size(font_id, 20, 72)
blf.color(font_id, 0.8, 0.0, 0.0, 1.0)
blf.draw(font_id, f"{vert.index} Hello Word")
def draw_text_callback():
if bpy.context.active_object.type == "MESH":
draw_text_on_verts()
def main():
run = True
if run:
handle = bpy.types.SpaceView3D.draw_handler_add(draw_text_callback, (), "WINDOW", "POST_PIXEL")
# save the handle so we can cleanup when `run` is set to False
bpy.types.Scene.draw_text_on_verts_handle_ref = handle
else:
if (
# make sure draw_text_on_verts_handle_ref exists in the bpy.types.Scene
hasattr(bpy.types.Scene, "draw_text_on_verts_handle_ref")
and bpy.types.Scene.draw_text_on_verts_handle_ref is not None
):
bpy.types.SpaceView3D.draw_handler_remove(bpy.types.Scene.draw_text_on_verts_handle_ref, "WINDOW")
bpy.types.Scene.draw_text_on_verts_handle_ref = None
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment