CybranM

Untitled

Jan 27th, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import bpy
  2.  
  3. def is_in_view3d_area(x,y):
  4.     for area in bpy.context.screen.areas:
  5.         if area.type == "VIEW_3D":
  6.             x_in_range = x >= area.x and x <= area.x + area.width
  7.             y_in_range = y >= area.y and y <= area.y + area.height
  8.             return x_in_range and y_in_range
  9.     return False
  10.  
  11. class SimpleMouseOperator(bpy.types.Operator):
  12.     bl_idname = "wm.mouse_position"
  13.     bl_label = "Mouse location"
  14.  
  15.     x : bpy.props.IntProperty()
  16.     y : bpy.props.IntProperty()
  17.  
  18.     def execute(self, context):
  19.         # rather then printing, use the report function,
  20.         # this way the message appears in the header,
  21.         in_range = is_in_view3d_area(self.x, self.y)
  22.         self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
  23.         self.report({'INFO'}, f"{in_range}")
  24.         return {'FINISHED'}
  25.  
  26.     def invoke(self, context, event):
  27.         self.x = event.mouse_x
  28.         self.y = event.mouse_y
  29.         return self.execute(context)
  30.    
  31.  
  32.  
  33. class MousePanel(bpy.types.Panel):
  34.     bl_label = "Mouse"
  35.     bl_space_type = "VIEW_3D"
  36.     bl_region_type = "UI"
  37.  
  38.     def draw(self, context):
  39.        
  40.         layout = self.layout
  41.        
  42.         #what I want to do:
  43.         #update this whenever the user moves the cursor outside or back inside the 3d viewport
  44.        
  45.         #if in_3dview:
  46.             #layout.label(text = "within the 3d viewport")
  47.         #else:
  48.             #layout.label(text = "outside the 3d viewport")
  49.        
  50.        
  51.        
  52.         #this doesnt work
  53.         if bpy.ops.wm.mouse_position():
  54.             layout.label(text="HELLO")
  55.  
  56. bpy.utils.register_class(SimpleMouseOperator)
  57. bpy.utils.register_class(MousePanel)
Add Comment
Please, Sign In to add comment