0
$\begingroup$

I have figured out how to draw on view 3d but I'm still confused about how to update the handler with the help of property. I have already created an example of what I am looking for in the second gif. I want to update the size of the handler from the size property.

Thanks in advance

enter image description here

enter image description here

Here is the code:

import bpy
import blf

class CUSTOM_PT_PANEL(bpy.types.Panel):
    bl_label = "Custom"
    bl_idname = "CUSTOM_PT_PANEL"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Custom".upper()
    
    def draw(self,context):
        layout = self.layout
        row = layout.row(align=True)
        row.operator(ADD_CUSTOM_PT_OPERATOR.bl_idname,icon="ADD")
        row.operator(REMOVE_CUSTOM_PT_OPERATOR.bl_idname,icon="REMOVE")
        column = layout.column(align=True)
        column.use_property_split = True
        column.use_property_decorate = False
        column.prop(context.scene,'custom_text',text="Text:")
        column.prop(context.scene,'custom_size',text="Size:")
        if context.object:
            column.separator(factor=4)
            column.prop(context.scene, 'custom_obj_location',text=f"{context.object.name} Location:")
        
main_handler = None

def call_back(self,context,text,size):
    font = 0
    blf.position(font,20,80,0)
    blf.size(font,size,200)
    blf.draw(font,text)
    
class ADD_CUSTOM_PT_OPERATOR(bpy.types.Operator):
    bl_label = "Add"
    bl_idname = "custom.add_text"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}
    
    def execute(self,context):
        context.area.tag_redraw()
        text = context.scene.custom_text
        size = context.scene.custom_size
        global main_handler
        main_handler = bpy.types.SpaceView3D.draw_handler_add(call_back,(self,context,text,size),"WINDOW","POST_PIXEL")
        return {"FINISHED"}

class REMOVE_CUSTOM_PT_OPERATOR(bpy.types.Operator):
    bl_label = "Remove"
    bl_idname = "custom.remove_text"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}
    
    def execute(self,context):
        context.area.tag_redraw()
        global main_handler
        bpy.types.SpaceView3D.draw_handler_remove(main_handler,"WINDOW")
        return {"FINISHED"}
    
    
def object_location_update(self,context):
    context.object.location.y = self.custom_obj_location   
    
classes = [CUSTOM_PT_PANEL,ADD_CUSTOM_PT_OPERATOR,REMOVE_CUSTOM_PT_OPERATOR]

def register():
    for cls in classes:
        bpy.utils.register_class(cls)
    
    bpy.types.Scene.custom_text = bpy.props.StringProperty(name="")
    bpy.types.Scene.custom_size = bpy.props.IntProperty(min=1,default=50)
    bpy.types.Scene.custom_obj_location = bpy.props.FloatProperty(update=object_location_update)
    
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    
    del bpy.types.Scene.custom_text  
    del bpy.types.Scene.custom_size  
    del bpy.types.Scene.custom_obj_location
    
if __name__ == "__main__":
    register()
$\endgroup$

1 Answer 1

1
$\begingroup$
import bpy
import blf


class CUSTOM_PT_PANEL(bpy.types.Panel):
    bl_label = "Custom"
    bl_idname = "CUSTOM_PT_PANEL"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Custom".upper()
    
    def draw(self,context):
        layout = self.layout
        row = layout.row(align=True)
        row.operator(ADD_CUSTOM_PT_OPERATOR.bl_idname,icon="ADD")
        row.operator(REMOVE_CUSTOM_PT_OPERATOR.bl_idname,icon="REMOVE")
        column = layout.column(align=True)
        column.use_property_split = True
        column.use_property_decorate = False
        column.prop(context.scene,'custom_text',text="Text:")
        column.prop(context.scene,'custom_size',text="Size:")
        if context.object:
            column.separator(factor=4)
            column.prop(context.scene, 'custom_obj_location',text=f"{context.object.name} Location:")


main_handler = None


class ADD_CUSTOM_PT_OPERATOR(bpy.types.Operator):
    bl_label = "Add"
    bl_idname = "custom.add_text"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}
    
    @classmethod
    def poll(cls, context):
        return not main_handler

    def call_back(self, context):
        font_id = 0
        blf.position(font_id, 20, 80, 0)
        blf.size(font_id, self.size, 200)
        blf.draw(font_id, self.text)

    def invoke(self, context, event):
        self.text = context.scene.custom_text
        self.size = context.scene.custom_size

        global main_handler
        main_handler = bpy.types.SpaceView3D.draw_handler_add(self.call_back, (context, ), "WINDOW", "POST_PIXEL")

        context.window_manager.modal_handler_add(self)
        context.area.tag_redraw()
        return {'RUNNING_MODAL'}

    def modal(self, context, event):
        self.text = context.scene.custom_text
        self.size = context.scene.custom_size
        context.area.tag_redraw()
        return {'PASS_THROUGH'}


class REMOVE_CUSTOM_PT_OPERATOR(bpy.types.Operator):
    bl_label = "Remove"
    bl_idname = "custom.remove_text"
    bl_description = ""
    bl_options = {"REGISTER", "UNDO"}
    
    @classmethod
    def poll(cls, context):
        return main_handler

    def execute(self,context):
        global main_handler
        bpy.types.SpaceView3D.draw_handler_remove(main_handler, "WINDOW")
        context.area.tag_redraw()
        main_handler = None
        return {"FINISHED"}


def object_location_update(self,context):
    context.object.location.y = self.custom_obj_location


classes = (
    CUSTOM_PT_PANEL,
    ADD_CUSTOM_PT_OPERATOR,
    REMOVE_CUSTOM_PT_OPERATOR,
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.custom_text = bpy.props.StringProperty(name="")
    bpy.types.Scene.custom_size = bpy.props.IntProperty(min=1, default=50)
    bpy.types.Scene.custom_obj_location = bpy.props.FloatProperty(update=object_location_update)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)

    del bpy.types.Scene.custom_text
    del bpy.types.Scene.custom_size
    del bpy.types.Scene.custom_obj_location
    

if __name__ == "__main__":
    register()
$\endgroup$
1
  • $\begingroup$ Thanks. I've got it. $\endgroup$
    – Muzammil
    Commented Jun 11, 2023 at 9:48

You must log in to answer this question.

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