Here's my understanding of bpy.props.BoolProperty
which offers additional functionality and integration with Blender's UI and data system.
Using
bpy.props.BoolProperty
allows you to specify various parameters such as the name, description, default value, and other settings that are important for properties used in Blender's interface.bpy.props
module defines properties to extend Blender’s internal data. The result of these functions is used to assign properties to classes registered with Blender and can’t be used directly.
Like in this example where I define a simple addon with a panel and one checkbox. I intend to add more properties in this group:
import bpy
class MyProperties(bpy.types.PropertyGroup):
my_prop_1: bpy.props.BoolProperty(
name="My Checkbox",
description="Some checkbox description",
default=False,
)
class OBJECT_PT_my_panel(bpy.types.Panel):
bl_label = f"My Simple Panel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Some Simple Panel'
def draw(self, context):
layout = self.layout
properties = context.scene.my_panel_properties
layout.prop(properties, "my_prop_1")
def register():
bpy.utils.register_class(MyProperties)
bpy.types.Scene.my_panel_properties = bpy.props.PointerProperty(type=MyProperties)
bpy.utils.register_class(OBJECT_PT_my_panel)
def unregister():
bpy.utils.unregister_class(MyProperties)
del bpy.types.Scene.my_panel_properties
bpy.utils.unregister_class(OBJECT_PT_my_panel)
register()
I'm on VSCode using Pylance and everything was working fine with no syntax problems and then all of a sudden overnight I noticed that, even though the code still works perfectly when run with Blender python, Pylance is now marking my property as incorrect syntax with the following error:
Call expression not allowed in type expression Pylance(reportInvalidTypeForm)
So I thought at first this was a bug and reported it in pylance-release/issues/5455 but got a reply:
Pylance is behaving correctly here. The return value of a function cannot be used as a type annotation. Pyright, the type checker that Pylance is built on top of, has recently been working to adhere more closely to the typing spec. If this is a recent change in behavior, my guess is it is related to that work.
There's more detail on valid type expressions for type annotations here: https://github.com/python/typing/blob/main/docs/spec/annotations.rst#valid-type-expression-forms.
And this reply from another developer:
The values of type annotations are ignored at runtime. They are used for static type checking only, and there are rules about what expression forms are allowed within type annotations. You're using an expression form here that is expressly disallowed by the Python static type specification.
So I double checked the examples in the bpy.props
documentation and they are now also all incorrect syntax. I assume this is now the wrong way to write my addon. How then do you rewrite the script to correctly add UI elements such as checkboxes (bpy.props.BoolProperty
), value sliders (bpy.props.FloatProperty
), drop down lists (bpy.props.EnumProperty
), and so on?
=
instead of:
, I don't know exactly why they chose to switch but it must have been a very good reason because it broke many, many scripts and still to this day frequently causes questions on this site :) $\endgroup$