Context in layout.prop() - why is it required?

Hi! For some reason layout.prop() drawing seems kind of weird to me. My goal is to create a custom bool property, so I figured out how to define the custom bool itself:

my_bool = bpy.props.BoolProperty(name=“BoolName”, default=False)

…But we get to the drawing part which I don’t understand.

So layout.prop() takes 2 arguments(as I understand): context (or something) and property variable (“my_bool”)

And for that to work I’m obligated to type in some thing like: context.scene or context.object in the first argument.

Now the property works, but I really want to know what I’m doing and I don’t understand why does it need to belong to some context. Why can’t it be drawn like an operator for which you only need to pass the id and maybe some text

Because right now I’m just writing context.scene just to make it work, but I don’t understand what it adds to it or what would happen if changed it to context.object. It just doesn’t make sense to my why that’s required.

So help me understand people


Context is part of nearly all base methods because it’s embedded in the superclass. When you subclass from an operator, you often conveniently get the context argument for free, you don’t have to provide it from somewhere.

Context is used for many reasons, but one is to shorten the lookup path of a property. This is efficient because blender polls its value for every ui redraw.

Another reason is that you can have several editors open, each having its own instance of a prop (eg. several viewports, each with their own settings), and Blender magically provides the correct data path using only the context.

Context itself is a way of accessing members that is relevant to the area you’re in. If you were in the viewport, context.space_data will point to the absolute data path where viewport-specific props are stored. If you were in the text editor, the context.space_data would point to a different absolute path.

1 Like