user: What is Bpy.Types.Operator and how is it used in Blender Python scripting?

assistant: . import bpy class SimpleMouseOperator ( bpy . types . Operator ): """ This operator shows the mouse location, this string is used for the tooltip and API docs """ bl_idname = "wm.mouse_position" bl_label = "Invoke Mouse Operator" x : bpy . props . IntProperty () y : bpy . props . IntProperty () def execute ( self , context ): # Rather than printing, use the report function, # this way the message appears in the header. self . report ({ &#39;INFO&#39; }, "Mouse coords are {:d} {:d} " . format ( self . x , self . y )) return { &#39;FINISHED&#39; } def invoke ( self , context , event ): self . x = event . mouse_x self . y = event . mouse_y return self . execute ( context ) # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator ( SimpleMouseOperator . bl_idname , text = "Simple Mouse Operator" ) # Register and add to the view menu (required to also use F3 search "Simple Mouse Operator" for quick access). bpy . utils . register_class ( SimpleMouseOperator ) bpy . types . VIEW3D_MT_view . append ( menu_func ) # Test call to the newly defined operator. # Here we call the operator and invoke it, # meaning that the settings are taken from the mouse. bpy . ops . wm . mouse_position ( &#39;INVOKE_DEFAULT&#39; ) # Another test call, this time call execute() directly with pre-defined settings. bpy . ops . wm . mouse_position ( &#39;EXEC_DEFAULT&#39; , x = 20 , y = 66 ) Calling a File Selector ¶ This example shows how an operator can use the file selector. Notice the invoke function calls a window manager method and returns {'RUNNING_MODAL'} , this means the file selector stays open and the operator does not exit immediately after invoke finishes. The file selector runs the operator, calling Operator.execute when the user confirms. The Operator.poll function is optional, used to check if the operator can run. import bpy class ExportSomeData ( bpy . types . Operator ): """Test exporter which just writes hello world""" bl_idname = "export.some_data" bl_label = "Export Some Data" filepath : bpy . props . StringProperty ( subtype = "FILE_PATH" ) @classmethod def poll ( cls , context ): return context . object is not None def execute ( self , context ): file = open ( self . filepath , &#39;w&#39; ) file . write ( "Hello World " + context . object . name ) return { &#39;FINISHED&#39; } def invoke ( self , context , event ): context . window_manager . fileselect_add ( self ) return { &#39;RUNNING_MODAL&#39; } # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator_context = &#39;INVOKE_DEFAULT&#39; self . layout . operator ( ExportSomeData . bl_idname , text = "Text Export Operator" ) # Register and add to the file selector (required to also use F3 search "Text Export Operator" for quick access). bpy . utils . register_class ( ExportSomeData ) bpy . types . TOPBAR_MT_file_export . append ( menu_func ) # Test call. bpy . ops . export . some_data ( &#39;INVOKE_DEFAULT&#39; ) Dialog Box ¶ This operator uses its Operator.invoke function to call a popup. import bpy class DialogOperator ( bpy . types . Operator ): bl_idname = "object.dialog_operator" bl_label = "Simple Dialog Operator" my_float : bpy . props . FloatProperty ( name = "Some Floating Point" ) my_bool : bpy . props . BoolProperty ( name = "Toggle Option" ) my_string : bpy . props . StringProperty ( name = "String Value" ) def execute ( self , context ): message = "Popup Values: {:f} , {:d} , &#39; {:s} &#39;" . format ( self . my_float , self . my_bool , self . my_string , ) self . report ({ &#39;INFO&#39; }, message ) return { &#39;FINISHED&#39; } def invoke ( self , context , event ): wm = context . window_manager return wm . invoke_props_dialog ( self ) # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator ( DialogOperator . bl_idname , text = "Dialog Operator" ) # Register and add to the object menu (required to also use F3 search "Dialog Operator" for quick access). bpy . utils . register_class ( DialogOperator ) bpy . types . VIEW3D_MT_object . append ( menu_func ) # Test call. bpy . ops . object . dialog_operator ( &#39;INVOKE_DEFAULT&#39; ) Custom Drawing ¶ By default operator properties use an automatic user interface layout. If you need more control you can create your own layout with a Operator.draw function. This works like the Panel and Menu draw functions, its used for dialogs and file selectors. import bpy class CustomDrawOperator ( bpy . types . Operator ): bl_idname = "object.custom_draw" bl_label = "Simple Modal Operator" filepath : bpy . props . StringProperty ( subtype = "FILE_PATH" ) my_float : bpy . props . FloatProperty ( name = "Float" ) my_bool : bpy . props . BoolProperty ( name = "Toggle Option" ) my_string : bpy . props . StringProperty ( name = "String Value" ) def execute ( self , context ): print ( "Test" , self ) return { &#39;FINISHED&#39; } def invoke ( self , context , event ): wm = context . window_manager return wm . invoke_props_dialog ( self ) def draw ( self , context ): layout = self . layout col = layout . column () col . label ( text = "Custom Interface!" ) row = col . row () row . prop ( self , "my_float" ) row . prop ( self , "my_bool" ) col . prop ( self , "my_string" ) # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator ( CustomDrawOperator . bl_idname , text = "Custom Draw Operator" ) # Register and add to the object menu (required to also use F3 search "Custom Draw Operator" for quick access). bpy . utils . register_class ( CustomDrawOperator ) bpy . types . VIEW3D_MT_object . append ( menu_func ) # Test call. bpy . ops . object . custom_draw ( &#39;INVOKE_DEFAULT&#39; ) Modal Execution ¶ This operator defines a Operator.modal function that will keep being run to handle events until it returns {'FINISHED'} or {'CANCELLED'} . Modal operators run every time a new event is detected, such as a mouse click or key press. Conversely, when no new events are detected, the modal operator will not run. Modal operators are especially useful for interactive tools, an operator can have its own state where keys toggle options as the operator runs. Grab, Rotate, Scale, and Fly-Mode are examples of modal operators. Operator.invoke is used to initialize the operator as being active by returning {'RUNNING_MODAL'} , initializing the modal loop. Notice __init__() and __del__() are declared. For other operator types they are not useful but for modal operators they will be called before the Operator.invoke and after the operator finishes. Also see the class construction and destruction section . import bpy class ModalOperator ( bpy . types . Operator ): bl_idname = "object.modal_operator" bl_label = "Simple Modal Operator" bl_options = { &#39;REGISTER&#39; , &#39;UNDO&#39; } def __init__ ( self , * args , ** kwargs ): super () . __init__ ( * args , ** kwargs ) print ( "Start" ) def __del__ ( self ): print ( "End" ) super () . __del__ () def execute ( self , context ): context . object . location . x = self . value / 100.0 return { &#39;FINISHED&#39; } def modal ( self , context , event ): if event . type == &#39;MOUSEMOVE&#39; : # Apply. self . value = event . mouse_x self . execute ( context ) elif event . type == &#39;LEFTMOUSE&#39; : # Confirm. return { &#39;FINISHED&#39; } elif event . type in { &#39;RIGHTMOUSE&#39; , &#39;ESC&#39; }: # Cancel. # Revert all changes that have been made context . object . location . x = self . init_loc_x return { &#39;CANCELLED&#39; } return { &#39;RUNNING_MODAL&#39; } def invoke ( self , context , event ): self . init_loc_x = context . object . location . x self . value = event . mouse_x self . execute ( context ) context . window_manager . modal_handler_add ( self ) return { &#39;RUNNING_MODAL&#39; } # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator ( ModalOperator . bl_idname , text = "Modal Operator" ) # Register and add to the object menu (required to also use F3 search "Modal Operator" for quick access). bpy . utils . register_class ( ModalOperator ) bpy . types . VIEW3D_MT_object . append ( menu_func ) # Test call. bpy . ops . object . modal_operator ( &#39;INVOKE_DEFAULT&#39; ) Enum Search Popup ¶ You may want to have an operator prompt the user to select an item from a search field, this can be done using bpy.types.Operator.invoke_search_popup . import bpy from bpy.props import EnumProperty class SearchEnumOperator ( bpy . types . Operator ): bl_idname = "object.search_enum_operator" bl_label = "Search Enum Operator" bl_property = "my_search" my_search : EnumProperty ( name = "My Search" , items = ( ( &#39;FOO&#39; , "Foo" , "" ), ( &#39;BAR&#39; , "Bar" , "" ), ( &#39;BAZ&#39; , "Baz" , "" ), ), ) def execute ( self , context ): self . report ({ &#39;INFO&#39; }, "Selected:" + self . my_search ) return { &#39;FINISHED&#39; } def invoke ( self , context , event ): context . window_manager . invoke_search_popup ( self ) return { &#39;RUNNING_MODAL&#39; } # Only needed if you want to add into a dynamic menu. def menu_func ( self , context ): self . layout . operator ( SearchEnumOperator . bl_idname , text = "Search Enum Operator" ) # Register and add to the object menu (required to also use F3 search "Search Enum Operator" for quick access). bpy . utils . register_class ( SearchEnumOperator ) bpy . types . VIEW3D_MT_object . append ( menu_func ) # Test call. bpy . ops . object . search_enum_operator ( &#39;INVOKE_DEFAULT&#39; ) base class — bpy_struct class bpy.types. Operator ( bpy_struct ) ¶ Storage of an operator being executed, or registered after execution bl_cursor_pending ¶ Cursor to use when waiting for the user to select a location to activate the operator (when bl_options has DEPENDS_ON_CURSOR set) Type : enum in Window Cursor Items , default 'DEFAULT' bl_description ¶ Type : string, default “”, (never None) bl_idname ¶ Type : string, default “”, (never None) bl_label ¶ Type : string, default “”, (never None) bl_options ¶ Options for this operator type Type : enum set in Operator Type Flag Items , default set() bl_translation_context ¶ Type : string, default “Operator”, (never None) bl_undo_group ¶ Type : string, default “”, (never None) has_reports ¶ Operator has a set of reports (warnings and errors) from last execution Type : boolean, default False, (readonly) layout ¶ Type : UILayout , (readonly) macros ¶ Type : bpy_prop_collection of Macro , (readonly) name ¶ Type : string, default “”, (readonly, never None) options ¶ Runtime options Type : OperatorOptions , (readonly, never None) properties ¶ Type : OperatorProperties , (readonly, never None) bl_property ¶ The name of a property to use as this operators primary property. Currently this is only used to select the default property when expanding an operator into a menu. Type : str report ( type , message ) ¶ report Parameters : type (enum set in Wm Report Items ) – Type message ( string , ( never None ) ) – Report Message is_repeat ( ) ¶ is_repeat Returns : result Return type : boolean classmethod poll ( context ) ¶ Test if the operator can be called or not Return type : boolean execute ( context ) ¶ Execute the operator Returns : result Return type : enum set in Operator Return Items check ( context ) ¶ Check the operator settings, return True to signal a change to redraw Returns : result Return type : boolean invoke ( context , event ) ¶ Invoke the operator Returns : result Return type : enum set in Operator Return Items modal ( context , event ) ¶ Modal operator function Returns : result Return type : enum set in Operator Return Items draw ( context ) ¶ Draw function for the operator cancel ( context ) ¶ Called when the operator is canceled classmethod description ( context , properties ) ¶ Compute a description string that depends on parameters Returns : result Return type : string as_keywords ( * , ignore = () ) ¶ Return a copy of the properties as a dictionary. classmethod bl_rna_get_subclass ( id , default = None , / ) ¶ Parameters : id ( str ) – The RNA type identifier. Returns : The RNA type or default when not found. Return type : bpy.types.Struct subclass classmethod bl_rna_get_subclass_py ( id , default = None , / ) ¶ Parameters : id ( str ) – The RNA type identifier. Returns : The class or default when not found. Return type : type classmethod poll_message_set ( message , * args ) ¶ Set the message to show in the tool-tip when poll fails. When message is callable, additional user defined positional arguments are passed to the message function. Parameters : message ( str | Callable [ ... , str | None ] ) – The message or a function that returns the message. args ( Any ) – A sequence of arguments to pass to message , if it’s a callable, otherwise argument is not available. Inherited Properties ¶ bpy_struct.id_data Inherited Functions ¶ bpy_struct.as_pointer bpy_struct.driver_add bpy_struct.driver_remove bpy_struct.get bpy_struct.id_properties_clear bpy_struct.id_properties_ensure bpy_struct.id_properties_ui bpy_struct.is_property_hidden bpy_struct.is_property_overridable_library bpy_struct.is_property_readonly bpy_struct.is_property_set bpy_struct.items bpy_struct.keyframe_delete bpy_struct.keyframe_insert bpy_struct.keys bpy_struct.path_from_id bpy_struct.path_from_module bpy_struct.path_resolve bpy_struct.pop bpy_struct.property_overridable_library_set bpy_struct.property_unset bpy_struct.rna_ancestors bpy_struct.type_recast bpy_struct.values References ¶ bpy.context.active_operator SpaceFileBrowser.active_operator SpaceFileBrowser.operator Window.modal_operators WindowManager.fileselect_add WindowManager.invoke_confirm WindowManager.invoke_popup WindowManager.invoke_props_dialog WindowManager.invoke_props_popup WindowManager.invoke_search_popup WindowManager.modal_handler_add WindowManager.operators Next OperatorFileListElement(PropertyGroup) Previous OceanModifier(Modifier) Copyright &#169; Blender Authors Made with Furo Report issue on this page On this page Operator(bpy_struct) Basic Operator Example Modifying Blender Data & Undo Invoke Function Calling a File Selector Dialog Box Custom Drawing Modal Execution Enum Search Popup Operator Operator.bl_cursor_pending Operator.bl_description Operator.bl_idname Operator.bl_label Operator.bl_options Operator.bl_translation_context Operator.bl_undo_group Operator.has_reports Operator.layout Operator.macros Operator.name Operator.options Operator.properties Operator.bl_property Operator.report() Operator.is_repeat() Operator.poll() Operator.execute() Operator.check() Operator.invoke() Operator.modal() Operator.draw() Operator.cancel() Operator.description() Operator.as_keywords() Operator.bl_rna_get_subclass() Operator.bl_rna_get_subclass_py() Operator.poll_message_set() Inherited Properties Inherited Functions References