I want to display a property in the viewport without using a modal operator. is it possible? I found this, but I did not understand how I can use (I started with python).


1 Answer 1


Extending the code from Draw and update text in viewport without modal operator in blender

I suggest using a class to handle the handles, and pass context to the callback method.

import bpy
import blf

class DrawingClass:
    def __init__(self, context, prop):
        self.prop = prop
        self.handle = bpy.types.SpaceView3D.draw_handler_add(
                   '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             
dc = DrawingClass(context, "Draw This On Screen")

#dc.remove_handle()  # will remove the handle and stop drawing
  • 1
    $\begingroup$ to handle the handles, from now on I shall use this on my colleagues :D $\endgroup$ Oct 27, 2015 at 19:17
  • $\begingroup$ Nice approach, but how we can store the reference to the class in order to remove the handle at later time e.g. based on a scene property enabled or disabled by the user? $\endgroup$
    – brockmann
    Oct 10, 2017 at 14:35
  • $\begingroup$ Give it global scope to update method of some scene property, or what I find a bulletproof "hack" bpy.app.driver_namespace["foo"] = bar $\endgroup$
    – batFINGER
    Oct 10, 2017 at 14:45
  • $\begingroup$ Sorry, wasn't notified @batFINGER. Updating a scene property requires another static variable in the class which ends up in a mess referencing static variables. I really like your idea, in theory it's super nice but in a real scenario for a addon or something, it does not really help IMHO. Maybe I'm too stupid. Anyway thanks for your hacky batTRICK! $\endgroup$
    – brockmann
    Oct 11, 2017 at 9:04

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