0
$\begingroup$

How to display various and custom info text in viewport with script and clean with another script and /or shortcut in blender 2.8+(im using 2.9).

SCRIPT FOR DISPLAY

import bpy
import blf


class DrawingClass:
    def __init__(self, context, prop):
        self.prop = prop
        self.handle = bpy.types.SpaceView3D.draw_handler_add(
                   self.draw_text_callback,(context,),
                   'WINDOW', 'POST_PIXEL')

    def draw_text_callback(self, context):
        font_id = 0  # XXX, need to find out how best to get this.

        # draw some text
        blf.position(font_id, 15, 50, 0)
        blf.size(font_id, 20, 72)
        blf.draw(font_id, "%s %s" % (context.scene.name, self.prop))

    def remove_handle(self):
         bpy.types.SpaceView3D.draw_handler_remove(self.handle, 'WINDOW')

context = bpy.context             
dns = bpy.app.driver_namespace
dns["dc"] = DrawingClass(context, "Draw This On Screen")

SCRIPT FOR CANCEL TEXT DRAW

import bpy
dns = bpy.app.driver_namespace
dc = dns.get("dc")
dc.remove_handle()
$\endgroup$
13
  • $\begingroup$ Hi! Bare minimum is to show some effort (research, code, minimal example etc.). Please read: blender.stackexchange.com/help/how-to-ask $\endgroup$ – brockmann Jul 23 '20 at 18:24
  • $\begingroup$ Also blender.stackexchange.com/questions/135123/… $\endgroup$ – batFINGER Jul 23 '20 at 20:00
  • 1
    $\begingroup$ @RayMairlot It's good enough AFAIC. OP deleted prior question after it was marked as a dupe of question their illustration came from re centering text on screen. . Hence OP is aware of code to write text on screen. If OP can explain why the answer to question marked as dupe doesn't work for them yeah perhaps re-open. At the moment it shows very little effort (the other close vote above) . Will attempt to hunt down a better dupe. $\endgroup$ – batFINGER Jul 24 '20 at 12:56
  • 1
    $\begingroup$ @RayMairlot Somehow (due to me too) original dupe comes from blender.stackexchange.com/questions/135123/… chose to cut out middle man. The second one re middle of the viewport is the OP's previous question (self removed) after marked dupe One I perhaps could have used as primary dupe again. Do we need a question for bottom left, top left, slightly left of center, there is one re using 3d coords. $\endgroup$ – batFINGER Jul 24 '20 at 13:41
  • 1
    $\begingroup$ @batFINGER I don't think different alignments requires different questions as those would be very similar. Would suggest just adding other alignment options to brockman's answer (if you or brockman have time) and maybe edit the question to something more general like "How do I align modal draw text in the viewport?" so it is not focused on 'middle'. Thoughts? $\endgroup$ – Ray Mairlot Jul 24 '20 at 15:27