bpy.props operator example misses property access #54286

Closed
opened 2018-03-10 13:14:22 +01:00 by Sybren A. Stüvel · 5 comments

The Operator Example for using properties in an operator shows this code:

class DialogOperator(bpy.types.Operator):
    bl_idname = "object.dialog_operator"
    bl_label = "Property Example"

    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):
        print("Dialog Runs")
        return {'FINISHED'}

    def invoke(self, context, event):
        wm = context.window_manager
        return wm.invoke_props_dialog(self)

This code has two downsides.

  1. It promotes a blocking UI design, where the user is shown a popup before actually executing the operator.
  2. It doesn't show how to actually use the property values.

I would suggest something like:

class OBJECT_OT_property_example(bpy.types.Operator):
    bl_idname = "object.property_example"
    bl_label = "Property Example"

    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):
        self.report({'INFO'}, 'F: %.2f  B: %s  S: %s' % (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'}

This actually shows how to use the properties in the code, and doesn't block the UI. A nice touch would be to add an explanation that the properties are also shown in the redo panel in the 3D view.

Note that I also changed the bl_idname, as this is an example about properties, not about dialogue boxes, and changed the class name to use the standard operator naming convention.

The [Operator Example](https://docs.blender.org/api/blender_python_api_master/bpy.props.html#operator-example) for using properties in an operator shows this code: ``` class DialogOperator(bpy.types.Operator): bl_idname = "object.dialog_operator" bl_label = "Property Example" 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): print("Dialog Runs") return {'FINISHED'} def invoke(self, context, event): wm = context.window_manager return wm.invoke_props_dialog(self) ``` This code has two downsides. 1. It promotes a blocking UI design, where the user is shown a popup before actually executing the operator. 2. It doesn't show how to actually use the property values. I would suggest something like: ``` class OBJECT_OT_property_example(bpy.types.Operator): bl_idname = "object.property_example" bl_label = "Property Example" 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): self.report({'INFO'}, 'F: %.2f B: %s S: %s' % (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'} ``` This actually shows how to use the properties in the code, and doesn't block the UI. A nice touch would be to add an explanation that the properties are also shown in the redo panel in the 3D view. Note that I also changed the `bl_idname`, as this is an example about properties, not about dialogue boxes, and changed the class name to use the standard operator naming convention.
Author
Member

Added subscriber: @dr.sybren

Added subscriber: @dr.sybren

Added subscriber: @VukGardasevic

Added subscriber: @VukGardasevic

I agree since the example with invoking a pop-up is already mentioned here on the Operator page.
The bpy.props page can include a link to it, where the three possibilities related to operator drawing / accessing properties can be explained in more depth, possibly including a notice about some drawbacks of each approach.

I agree since the example with invoking a pop-up is already mentioned [here ](https://docs.blender.org/api/blender_python_api_master/bpy.types.Operator.html?highlight=operator#custom-drawing) on the Operator page. The bpy.props page can include a link to it, where the three possibilities related to operator drawing / accessing properties can be explained in more depth, possibly including a notice about some drawbacks of each approach.
Sybren A. Stüvel self-assigned this 2018-03-14 11:27:06 +01:00

This issue was referenced by blender/blender@b76471c1f9

This issue was referenced by blender/blender@b76471c1f9f42fda7187194e857b01549c0131d5
Author
Member

Changed status from 'Open' to: 'Resolved'

Changed status from 'Open' to: 'Resolved'
Sign in to join this conversation.
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender-manual#54286
No description provided.