Passing, Self , Context , Event ( bpy.types.Event.mouse_region_x )

I discovered i lack of some basic knowledge.

I used the template of “simple operator” to do a test.
The operator “starts” passing to the main function the context.

def execute(self, context):

So from “main” i can just type context.selected_objects Without the bpy. to get the list, and do stuff.

def testShow(self):

But even if the context is not passed, like this,
i can still get that list from main2 writing the whole address

print (  len( bpy.context.selected_objects ) )

I used a lot the template of the raycast,
That template works using a “modal” that pass the Event,
so from that template i can easily access the mouse_x.

Today for Learning and practice ,
i decided to try to turn that “Modal-template”
into a simple operator, and i am fallen into oblivion.

my questions:
How i get the mouse x ? in the View_3d ?
Can i convert that template in a simple-operator ?
No because the mouse_x is only avaible in event & modal?
The Event passed in that template , from where came from?
Why is not accessible like " bpy.types.Event.mouse_region_x "
Making my own operator and functions how should i think about these “context”

This is a beginner question, but how is named this?
What should i search for understand this ?

Here my playcode- with the commented line

import bpy

print( "-------------------------------") 
print( "-------------------------------") 
print( "Direct access bpy.context   " + str( bpy.context.screen.areas[1].type ) )

#print( bpy.types.Event.mouse_region_x ) ### type object 'Event' has no attribute 'mouse_region_x'

def main(context):
    print( "Number of object selected   " + str( len( bpy.context.selected_objects ) ) )
    print( "-----ALL_AREA_INFO -------------") 
    for area in context.screen.areas: 
        print (  
             str (context.region.height) + " " +
             str (context.region.type) + 
              " "      + str (area.type) +    
              "    W " + str( area.width) +       
              " H "    + str( area.height)   
    for area in bpy.context.screen.areas:
        if area.type == "VIEW_3D" :
            print( "---- VIEW_3D ROT_LOC_MATRIX -------------")
#            print( )
#            print( area.spaces[0].region_3d.view_rotation)
#            print( area.spaces[0].region_3d.view_location) 	
class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator123"
    bl_label = "123 Simple Object Operator"

    def poll(cls, context):
        return context.active_object is not None
    def execute(self, context):
        return {'FINISHED'}

def register():
def unregister():

if __name__ == "__main__":
    # test call

To get the mouse position you need an event object. Only way to get an event is to use a modal operator.

Hi man,
but where did you read this important piece of info?

I have done 2 tests,

  1. Made the modal start, get the values and close, without looping, and i just start the the modal to get the values… look like a really dirty solution XD
  2. Use a modal to read the mousex,y and used to set a property in the scene and in world.
    But the process is really slow and you get the value on the property in like 5 seconds.

You should look at template In there you can see invoke function has event. Invoke function is used to gather necessary data before executing operator. You don’t need modal, you can use invoke to access event.
FYI modal is used if you want to keep doing stuff over time. If you want to execute your operator only once you dont need modal.
Now if you don’t want to create different properties in which you can store mouse position or other event class data as shown in template. You can do as below. but I don’t suggest you to do like this

import bpy

class ModalOperator(bpy.types.Operator):
    """Print mouse x position when operator executed"""
    bl_idname = "object.modal_operator"
    bl_label = "Simple Modal Operator"
    event = None
    def execute(self, context):
        return {'FINISHED'}

    def invoke(self, context, event):
        self.event = event
        return self.execute(context)
def register():

def unregister():

if __name__ == "__main__":

    # test call

Thanks man,
Good to know that invoke get the event too XD
Sorry man: why its not suggest to do like you done?
as i posted before, i tryed to use a modal to send endlessy mouse positions to a property i added in world and scene, but the value change 1 time every 5 seconds!

this thing of reverse learning is hard, often happen to read but dont “understand”