How to get checkbox state from UI panel?

(Qai) #1

In my file I create a custom panel. I display the checkbox in that panel class using row.prop(scene, ‘checkbox’) and then register it:

bpy.types.Scene.checkbox = bpy.props.BoolProperty(
name = ‘checkbox’,
description = ‘checkbox’,
default = False)

If I print(bpy.types.Scene.checkbox), then I get:

(<built-in function BoolProperty>, {‘attr’: ‘checkbox’, ‘name’: ‘checkbox’, ‘description’: ‘checkbox’, ‘default’: False})

So my other files are seeing that the checkbox is registered and can access it. I just don’t know how to access the actual checked state. Any help?

I’m using 2.79.

(iceythe) #2

We add properties to ID types such as bpy.types.Scene so that the property propagates to all scene instances (like the scenes in, bpy.context.scene, etc). The property on the base class itself (in bpy.types) is not initialized, but rather instantiates new ones with a default value.

In other words, try accessing the property on an actual scene like bpy.context.scene.


val = getattr(bpy.context.scene, 'checkbox', None)
if val is not None:
(Qai) #3

That makes things so much more clearer. I was trying to getattr ‘checkbox’ on types itself, to which it would error out saying no member. Now it makes sense why.

Thanks! All is working now.