Beginner: Trouble understanding layout.prop() drawing

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 :slight_smile:

A property you can draw with layout.prop is always part of some container (e.g. a scene, an object, a custom PropertyGroup, …).

The first parameter to layout.prop is this container that contains the property.
The second parameter is the name of the property within this container.

What needs to be passed as first argument depends on how you declared the property.
When you used bpy.types.Scene.my_prop = BoolProperty(), then a scene (e.g. context.scene has to be passed as first argument).

Ok that cleared some things up. But now I have another question:

So I’m making this bool that will basically enable or disable a function in a script. So I don’t see why it should exist in a scene container, so I would probably use a custom PropertyGroup. Now what would I write in layout.prop if the bool was in a PropertyGroup?

I’ve seen some people write something like (self, “my_bool”).

Or that being said. How do I know what container I need to add my property to? When should it be Scene, Object or a PropertyGroup.

Thanks @jacqueslucke so far. I love your VS CODE extension by the way :), using it right now.

A property group also belongs to some Blender data structure.
In the end, all properties you display has to belong to some “object” of Blender.

If the setting you need is a global setting, you can consider to register your own AddonPreferences.

When people write layout.prop(self, "prop_name"), they usually reference a property of an operator within the operator.

Thanks, always nice to hear when someone is using the extension.

Alright, thanks for your help man I guess that’s all I need for now.