I'm new to Python scripting, and is confused with the design pattern it's used so I do not quite understand how this class works, here is a code example,

class HelloWorldOperator(bpy.types.Operator):
    bl_idname = "wm.hello_world"
    bl_label = "Minimal Operator"

    def execute(self, context):
        print("Hello World")
        return {'FINISHED'}

From the execute() function, how this function is called by Blender and where this 'self' and 'context' comes from remains a mysterious, it has to be somewhere in Blender to call this function. To sum up a bit, the user defines the real function (the function to be follow a predefined pattern, the function name pattern, the return pattern etc.), then being called by Blender, what design pattern this is?


The HelloWorldOperator derives from the bpy.types.Operator base-class.

The self variable is a Pythonic class pattern, it is similar to this in C++ like languages. The class variables declared in the class header, e.g. bl_idname and bl_lable, are shared across all instances of the class. The self dictionary contains the per-instance variables and is always the first argument in a Python class method. It is automatically supplied on method calls.

The context comes from the Blender environment and contains pointers to the active object, collection, GUI mode and other things.

Classes deriving from bpy.types.Operator are high-level functions designed to be used as GUI callback functions. As such they only return GUI status.

  • $\begingroup$ Thanks for the heads up, where I do not understand is "to be used as GUI callback functions", is there any pure Python implementation of such pattern I can refer to, so I can have an intuitive understanding of how this call back is called $\endgroup$ Mar 24 at 7:13
  • $\begingroup$ Have you read docs.blender.org/api/current/bpy.ops.html? $\endgroup$
    – Ron Jensen
    Mar 24 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.