Do anyone knows how can I render the text or number like 0.618m in 3d viewport on the selected mesh/object. I am calculating the distance between two vertices using python script and I want render the distance on the active object between these two vertices.
After Using BLF module And used google bard to search for code, In Bard's prompt I wrote this print text on object using blf module with python script in blender 3.6 ,Bard gave this code
import bpy
import blf
def print_text_on_object(obj, text):
# Get the object's bounding box corners
corners = obj.bound_box.corners
# Extract the coordinates of each corner
x1, y1, z1 = corners[0]
x2, y2, z2 = corners[2]
# Convert the bounding box coordinates to screen coordinates
screen_coords = blf.points_to_screen((x1, y1), (x2, y2))
# Load the desired font
font_id = blf.load("fonts/Arial.ttf")
# Set the font size and color
blf.size(font_id, 24)
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
# Draw the text using the screen coordinates and font
blf.draw_text(font_id, int(screen_coords[0]), int(screen_coords[1]), text)
# Get the active object
obj = bpy.context.active_object
# Print the text on the active object
print_text_on_object(obj, "Hello World!")
And
import bpy
import blf
def print_text_on_object(obj, text):
# Get the object's bounding box points
points = obj.bound_box.points
# Extract the coordinates of the top-left and bottom-right corner points
x1, y1, z1 = points[0]
x2, y2, z2 = points[6]
# Convert the bounding box coordinates to screen coordinates
screen_coords = blf.points_to_screen((x1, y1), (x2, y2))
# Load the desired font
font_id = blf.load("fonts/Arial.ttf")
# Set the font size and color
blf.size(font_id, 24)
blf.color(font_id, 1.0, 1.0, 1.0, 1.0)
# Draw the text using the screen coordinates and font
blf.draw_text(font_id, int(screen_coords[0]), int(screen_coords[1]), text)
# Get the active object
obj = bpy.context.active_object
# Print the text on the active object
print_text_on_object(obj, "Hello World!")
But Blender Console shows error because obj.bound_box doesn't have .points and .corners methods in Blender 3.6 or 4.0 Python API.



bpy_extras.view3d_utils.location_3d_to_region_2d. And this is for tool development, if you use for rendering just need add a text object to scene:bpy.ops.object.text_add()$\endgroup$