I am trying to find my way around events and modals and I would expect that a mouseclick would provide the appropriate context.
I've used this answer as a starting point but it looks like context.area
is always Ǹone
in a mouse event.
Full code here:
import bpy
from bpy_extras.view3d_utils import region_2d_to_vector_3d, region_2d_to_location_3d
class ModalTimerOperator(bpy.types.Operator):
"""Operator which runs its self from a timer"""
bl_idname = "wm.modal_timer_operator"
bl_label = "Add Sphere on Click"
_timer = None
def modal(self, context, event):
if event.type in {'RIGHTMOUSE', 'ESC'}:
self.cancel(context)
return {'CANCELLED'}
if event.type == 'LEFTMOUSE' and event.value == 'PRESS':
# left click
print(context.area.type)
if context.area.type == 'VIEW_3D':
region = context.region
r3d = context.space_data.region_3d
x, y = event.mouse_region_x, event.mouse_region_y
view = region_2d_to_vector_3d(region, r3d, (x, y))
loc = region_2d_to_location_3d(region, r3d, (x, y), view)
bpy.ops.mesh.primitive_uv_sphere_add(location=loc)
if event.type == 'TIMER':
#print("timer")
pass
return {'PASS_THROUGH'}
def execute(self, context):
if context.area.type != 'VIEW_3D':
print("Must use in a 3d region")
return {'CANCELLED'}
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
def register():
bpy.utils.register_class(ModalTimerOperator)
def unregister():
bpy.utils.unregister_class(ModalTimerOperator)
if __name__ == "__main__":
register()
# test call
bpy.ops.wm.modal_timer_operator()
As a result I keep getting an error
AttributeError: 'NoneType' object has no attribute 'type'
at the line containing
if context.area.type != 'VIEW_3D':
Why is that?