1
$\begingroup$

I'm still quite new to the whole blender python API. One of the thing I struggle with is context. I have a concrete question in mind though: Consider the simple operator below. What's the difference context and bpy.context?

class SimpleOperator(bpy.types.Operator):
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    def execute(self, context):

        context.active_object
        bpy.context.active_object
        
        return {'FINISHED'}
$\endgroup$
6
  • 1
    $\begingroup$ Difference is that context is already passed to the execute method (comes in by default). This is pretty essential since in blender everything has context, recommend try to understand this concept before writing any operators: docs.blender.org/api/current/info_quickstart.html#context $\endgroup$
    – brockmann
    Commented Dec 1, 2020 at 21:14
  • $\begingroup$ Yeah. I know that I have been hacking around in my scripts. It works, but it's likely quite unstable. Thanks for the link, I'll definetly give it a look. But isn't bpy.context.active_object the "current" context and hence the same as the one just passed into the operator? $\endgroup$
    – DrDress
    Commented Dec 1, 2020 at 21:27
  • 1
    $\begingroup$ Just tried to explain it and get the ball rolling, don't take it personal. In other words: bpy.context is a new reference. $\endgroup$
    – brockmann
    Commented Dec 1, 2020 at 21:32
  • $\begingroup$ Oh I didn't take it personal. I just find it a quite confusing. But thinking of bpy.context as a new context and these are then passed to operators really makes sense. Thanks and I kept the ball rolling ;-) $\endgroup$
    – DrDress
    Commented Dec 1, 2020 at 22:46
  • 1
    $\begingroup$ AFAIK at the moment, nothing. Using the argument is good practice. Have not seen where the values differ, but this could change in the future. For example when overriding context of an operator, the devs could make a change such that the argument is the overridden context and bpy.context is the actual context. A better example is perhaps the scene argument of a handler, which is often ignored and bpy.context.scene is used instead, which will work as expected; till it doesn't @tokikake No, not necessarily. $\endgroup$
    – batFINGER
    Commented Dec 2, 2020 at 5:28

0

You must log in to answer this question.

Browse other questions tagged .