Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import bpy
- def is_in_view3d_area(x,y):
- for area in bpy.context.screen.areas:
- if area.type == "VIEW_3D":
- x_in_range = x >= area.x and x <= area.x + area.width
- y_in_range = y >= area.y and y <= area.y + area.height
- return x_in_range and y_in_range
- return False
- class SimpleMouseOperator(bpy.types.Operator):
- bl_idname = "wm.mouse_position"
- bl_label = "Mouse location"
- x : bpy.props.IntProperty()
- y : bpy.props.IntProperty()
- def execute(self, context):
- # rather then printing, use the report function,
- # this way the message appears in the header,
- in_range = is_in_view3d_area(self.x, self.y)
- self.report({'INFO'}, "Mouse coords are %d %d" % (self.x, self.y))
- self.report({'INFO'}, f"{in_range}")
- return {'FINISHED'}
- def invoke(self, context, event):
- self.x = event.mouse_x
- self.y = event.mouse_y
- return self.execute(context)
- class MousePanel(bpy.types.Panel):
- bl_label = "Mouse"
- bl_space_type = "VIEW_3D"
- bl_region_type = "UI"
- def draw(self, context):
- layout = self.layout
- #what I want to do:
- #update this whenever the user moves the cursor outside or back inside the 3d viewport
- #if in_3dview:
- #layout.label(text = "within the 3d viewport")
- #else:
- #layout.label(text = "outside the 3d viewport")
- #this doesnt work
- if bpy.ops.wm.mouse_position():
- layout.label(text="HELLO")
- bpy.utils.register_class(SimpleMouseOperator)
- bpy.utils.register_class(MousePanel)
Add Comment
Please, Sign In to add comment