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