0
$\begingroup$

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.

enter description here

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.

$\endgroup$
3
  • $\begingroup$ you might looking for the blf module: docs.blender.org/api/current/blf.html $\endgroup$
    – X Y
    Nov 20, 2023 at 13:21
  • $\begingroup$ @XY I used BLF module but ran into some errors. $\endgroup$
    – nikhil
    Nov 21, 2023 at 9:30
  • $\begingroup$ To convert 3d location to 2d screen, you can use 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$
    – X Y
    Nov 21, 2023 at 11:21

1 Answer 1

1
$\begingroup$

I made this using geometry nodes, so this isn't a python solution. But it works. It uses a vertex group as input and displays the length of the selection.

enter image description here

Note that you'll have to use a curve to mesh node if you want the numbers to show up in the render

enter image description here

$\endgroup$

You must log in to answer this question.

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