Skip to main content
more detail and example
Source Link
Fox
  • 2.1k
  • 22
  • 49

So whatWhat i really need is bpy.Types.event, unfortunately, it is only accessible from an Operator invoke() or modal()

so here's the trick

_event = None  

class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""

    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}

def get_event():
    global _event
    bpy.ops.scatter5.get_event('INVOKE_DEFAULT')
    return _event
event = get_event()

event = get_event() willto get event from anywhere in your script

what would have been be nice is something like bpy.context.window_manager.eventbpy.context.window_manager.event, unfortunately this API does not exist. not (not sure why eventthis type is only accessible from an operator..operators?)

Also, it would be ideal if there was a built-in python module that can detect keyboard input, unfortunately, I didn't find one.

feel free to propose an alternative method


example of implementation by @batFINGER press alt/shift/ctrl/oskey while interacting with foo prop

import bpy
​
_event = None  
​
class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""
​
    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}
​
def get_event(exec_context='INVOKE_DEFAULT'):
    global _event
    bpy.ops.scatter5.get_event(exec_context)
    return _event
​
def foo(self, context):
    event = get_event('INVOKE_REGION_WIN')
    print(
        "Shift" if event.shift else "",
        "Alt" if event.alt else "",
        "Ctrl" if event.ctrl else "",
        "OSKey" if event.oskey else "",
        event.value)
    
def draw(self, context):
    ob = context.object
    if ob:
        self.layout.prop(ob, "foo")
​
def register():
    bpy.types.Object.foo = bpy.props.IntProperty(
            update=foo,
            )
    bpy.utils.register_class(SCATTER5_OT_get_event)
    bpy.types.TEXT_HT_footer.prepend(draw)
    
if __name__ == "__main__":
    register()

So what i really need is bpy.Types.event, unfortunately it is only accessible from an Operator invoke() or modal()

so here's the trick

_event = None  

class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""

    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}

def get_event():
    global _event
    bpy.ops.scatter5.get_event('INVOKE_DEFAULT')
    return _event

event = get_event() will get event from anywhere in your script

what would be nice is something like bpy.context.window_manager.event, unfortunately this API does not exist. not sure why event is only accessible from an operator...

What i really need is bpy.Types.event, unfortunately, it is only accessible from an Operator invoke() or modal()

so here's the trick

_event = None  

class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""

    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}

def get_event():
    global _event
    bpy.ops.scatter5.get_event('INVOKE_DEFAULT')
    return _event
event = get_event()

to get event from anywhere in your script

what would have been be nice is something like bpy.context.window_manager.event, unfortunately this API does not exist (not sure why this type is only accessible from operators?)

Also, it would be ideal if there was a built-in python module that can detect keyboard input, unfortunately, I didn't find one.

feel free to propose an alternative method


example of implementation by @batFINGER press alt/shift/ctrl/oskey while interacting with foo prop

import bpy
​
_event = None  
​
class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""
​
    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}
​
def get_event(exec_context='INVOKE_DEFAULT'):
    global _event
    bpy.ops.scatter5.get_event(exec_context)
    return _event
​
def foo(self, context):
    event = get_event('INVOKE_REGION_WIN')
    print(
        "Shift" if event.shift else "",
        "Alt" if event.alt else "",
        "Ctrl" if event.ctrl else "",
        "OSKey" if event.oskey else "",
        event.value)
    
def draw(self, context):
    ob = context.object
    if ob:
        self.layout.prop(ob, "foo")
​
def register():
    bpy.types.Object.foo = bpy.props.IntProperty(
            update=foo,
            )
    bpy.utils.register_class(SCATTER5_OT_get_event)
    bpy.types.TEXT_HT_footer.prepend(draw)
    
if __name__ == "__main__":
    register()
Source Link
Fox
  • 2.1k
  • 22
  • 49

So what i really need is bpy.Types.event, unfortunately it is only accessible from an Operator invoke() or modal()

so here's the trick

_event = None  

class SCATTER5_OT_get_event(bpy.types.Operator):
    bl_idname  = "scatter5.get_event"
    bl_label   = ""

    def invoke(self, context, event):
        global _event
        _event = event
        return {'FINISHED'}

def get_event():
    global _event
    bpy.ops.scatter5.get_event('INVOKE_DEFAULT')
    return _event

event = get_event() will get event from anywhere in your script

what would be nice is something like bpy.context.window_manager.event, unfortunately this API does not exist. not sure why event is only accessible from an operator...