According to Blender's doc, this is how to define properties:

class OBJECT_OT_property_example(bpy.types.Operator):
    bl_idname = "object.property_example"
    bl_label = "Property Example"
    bl_options = {'REGISTER', 'UNDO'}

    my_float: bpy.props.FloatProperty(name="Some Floating Point")
    my_bool: bpy.props.BoolProperty(name="Toggle Option")
    my_string: bpy.props.StringProperty(name="String Value")

    def execute(self, context):
            {'INFO'}, 'F: %.2f  B: %s  S: %r' %
            (self.my_float, self.my_bool, self.my_string)
        print('My float:', self.my_float)
        print('My bool:', self.my_bool)
        print('My string:', self.my_string)
        return {'FINISHED'}

I don't understand this line:

my_float: bpy.props.FloatProperty(name="Some Floating Point")

Afaik, this is Python's type annotation syntax. But bpy.props.FloatProperty should be a type already, and bpy.props.FloatProperty(name="Some Floating Point") would create an instance. Why can it be used as a type too? Is this a part of standard Python, and if so, how it actually works?

  • 4
    $\begingroup$ bpy.props.FooProperty() is a method used for annotating a class with property definitions, that when registered build an operator with props akin to (old style for example sake) Foo.prop = property(setter, getter). $\endgroup$
    – batFINGER
    Jul 6 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.