4
$\begingroup$

I want to show some Info in the viewport something like vertex indices

enter image description here

you can see select a vertex and press N to see vertex position but it's hard.

enter image description here

so I liked to write a script to show the vertex position

enter image description here

I tried to use opengl but text was blurry and I couldn't make what I want.

# import stand alone modules
import blf
import bpy

font_info = {
    "font_id": 0,
    "handler": None,
}


def init():
    """init function - runs once"""
    import os
    # Create a new font object, use external ttf file.
    font_path = bpy.path.abspath('//Zeyada.ttf')
    # Store the font indice - to use later.
    if os.path.exists(font_path):
        font_info["font_id"] = blf.load(font_path)
    else:
        # Default font.
        font_info["font_id"] = 0

    # set the font drawing routine to run every frame
    font_info["handler"] = bpy.types.SpaceView3D.draw_handler_add(
        draw_callback_px, (None, None), 'WINDOW', 'POST_VIEW')


def draw_callback_px(self, context):

    """Draw on the viewports"""
    # BLF drawing routine
    font_id = font_info["font_id"]
    #blf.position(font_id, pos.x, pos.y, pos.z)
    #blf.size(font_id, 100,100)
    #blf.draw(font_id, "Hello World")

    pos = bpy.context.object.location
    v3d = context.space_data
    rv3d = v3d.region_3d
    pos_text = location_3d_to_region_2d(region, rv3d, context._center)
    
    blf.position(font_id, pos_text[0], pos_text[1], 0)
    blf.size(font_id, 100,100)
    blf.draw(font_id, "Hello World")


if __name__ == '__main__':
    init()
$\endgroup$
  • $\begingroup$ Related blender.stackexchange.com/questions/139831/… Suggest this could be marked as dupe of blender.stackexchange.com/questions/137816/… If you are sourcing code from elsewhere add a link for context. $\endgroup$ – batFINGER Sep 3 at 23:31
  • $\begingroup$ @batFINGER thank you it's what I want. I just want to show text in 3d viewport I'm wondering why I didn't find that post because I searched a lot! $\endgroup$ – Seyed Morteza Kamali Sep 4 at 4:32
  • $\begingroup$ @batFINGER I edited my question based on links that you sent me but I don't know why I can't see text in the viewport!!! $\endgroup$ – Seyed Morteza Kamali Sep 4 at 4:59
  • $\begingroup$ Convert 3d global coords to region coords. The global location of an object is always ob.matrix_world.translation which may or may not be ob.location See blender.stackexchange.com/questions/6155/… New edit are passing None as context argument, yet trying to use it, which will throw errors to console. $\endgroup$ – batFINGER Sep 4 at 5:33
2
$\begingroup$

Well, you are passing no context

(None, None)

but then using some context in draw_callback_px that is None

you should use POST_PIXEL because it isn't 3d

bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, (bpy.context, None), 'WINDOW', 'POST_PIXEL')

you forgot to import location_3d_to_region_2d libraries

import bpy
from bpy_extras.view3d_utils import location_3d_to_region_2d

Go to Window > Open System Console and you will see all the errors so you can have something to debug what is happening there

enter image description here

convert coordinates from vertex to world-space

Multiply the world matrix by the object-space vector for world space coordinate:

import bpy

ob = bpy.data.objects['Cube']

v = ob.data.vertices[0].co
mat = ob.matrix_world

# Multiply matrix by vertex
loc = mat @ v

import blf
import bpy
from bpy_extras.view3d_utils import location_3d_to_region_2d


def draw_callback_px(context, dummy):
    ob = bpy.data.objects['Cube']

    v = ob.data.vertices[0].co
    mat = ob.matrix_world

    # Multiply matrix by vertex
    pos = mat @ v
    
    v3d = context.space_data
    rv3d = v3d.region_3d
    pos_text = location_3d_to_region_2d(context.region, rv3d, pos)

    blf.position(0, pos_text[0], pos_text[1], 0)
    blf.size(0, 1,1)
    blf.draw(0, str(pos))


bpy.types.SpaceView3D.draw_handler_add(draw_callback_px, (bpy.context, None), 'WINDOW', 'POST_PIXEL')

special thanks for @jfranmatheu and @batFINGER

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Good one you got there in the end. Note using me.vertices will most likely not work as expected in edit mode if you start moving them. Check out blender.stackexchange.com/a/160913/15543 $\endgroup$ – batFINGER Sep 4 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.