1

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).

Ray Mairlot
  • 29,192
  • 11
  • 103
  • 125

1 Answers1

5

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(
                   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             
dc = DrawingClass(context, "Draw This On Screen")

#dc.remove_handle()  # will remove the handle and stop drawing
batFINGER
  • 84,216
  • 10
  • 108
  • 233
  • 1
    to handle the handles, from now on I shall use this on my colleagues :D – Jaroslav Jerryno Novotny Oct 27 '15 at 19:17
  • 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? – brockmann Oct 10 '17 at 14:35
  • Give it global scope to update method of some scene property, or what I find a bulletproof "hack" bpy.app.driver_namespace["foo"] = bar – batFINGER Oct 10 '17 at 14:45
  • 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! – brockmann Oct 11 '17 at 09:04