[Bf-blender-cvs] [b76471c1f9f] master: Fix T54286: bpy.props operator example misses property access

Sybren A. Stüvel noreply at git.blender.org
Wed Mar 14 11:31:20 CET 2018


Commit: b76471c1f9f42fda7187194e857b01549c0131d5
Author: Sybren A. Stüvel
Date:   Wed Mar 14 11:31:14 2018 +0100
Branches: master
https://developer.blender.org/rBb76471c1f9f42fda7187194e857b01549c0131d5

Fix T54286: bpy.props operator example misses property access

The old example had two downsides:

- It promoted a blocking UI design, where the user is shown a popup
  before actually executing the operator.
- It didn't show how to actually use the property values.

The new code avoids these mistakes. 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.

I also extended the example to include a panel that sets multiple
properties of the operator, since I see questions about this relatively
frequently.

===================================================================

M	doc/python_api/examples/bpy.props.1.py

===================================================================

diff --git a/doc/python_api/examples/bpy.props.1.py b/doc/python_api/examples/bpy.props.1.py
index 51534628930..dd3a3ebc432 100644
--- a/doc/python_api/examples/bpy.props.1.py
+++ b/doc/python_api/examples/bpy.props.1.py
@@ -2,30 +2,56 @@
 Operator Example
 ++++++++++++++++
 
-A common use of custom properties is for python based :class:`Operator` classes.
+A common use of custom properties is for python based :class:`Operator`
+classes. Test this code by running it in the text editor, or by clicking the
+button in the 3D Viewport's Tools panel. The latter will show the properties
+in the Redo panel and allow you to change them.
 """
-
 import bpy
 
 
-class DialogOperator(bpy.types.Operator):
-    bl_idname = "object.dialog_operator"
+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):
-        print("Dialog Runs")
+        self.report({'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'}
 
-    def invoke(self, context, event):
-        wm = context.window_manager
-        return wm.invoke_props_dialog(self)
-
 
-bpy.utils.register_class(DialogOperator)
-
-# test call
-bpy.ops.object.dialog_operator('INVOKE_DEFAULT')
+class OBJECT_PT_property_example(bpy.types.Panel):
+    bl_idname = "object_PT_property_example"
+    bl_label = "Property Example"
+    bl_space_type = 'VIEW_3D'
+    bl_region_type = 'TOOLS'
+    bl_category = "Tools"
+
+    def draw(self, context):
+        # You can set the property values that should be used when the user
+        # presses the button in the UI.
+        props = self.layout.operator('object.property_example')
+        props.my_bool = True
+        props.my_string = "Shouldn't that be 47?"
+
+        # You can set properties dynamically:
+        if context.object:
+            props.my_float = context.object.location.x
+        else:
+            props.my_float = 327
+
+
+bpy.utils.register_class(OBJECT_OT_property_example)
+bpy.utils.register_class(OBJECT_PT_property_example)
+
+# Demo call. Be sure to also test in the 3D Viewport.
+bpy.ops.object.property_example(my_float=47, my_bool=True,
+                                my_string="Shouldn't that be 327?")



More information about the Bf-blender-cvs mailing list