diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2018-03-14 20:07:01 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2018-03-14 20:10:31 +0300 |
commit | 9009622b5464f13cc9521663b43605bf85e8e0b5 (patch) | |
tree | 72d95313921af3c5687e13e58dcf6f9c4fe68232 /doc | |
parent | 9521b67ac0db5958df14ff92e5334bca07902481 (diff) | |
parent | b0823962e9b82d2bfa0551cb072bd61b67ddc773 (diff) |
Merge branch 'master' into blender2.8
Diffstat (limited to 'doc')
-rw-r--r-- | doc/python_api/examples/bpy.props.1.py | 52 | ||||
-rw-r--r-- | doc/python_api/examples/bpy.props.5.py | 36 |
2 files changed, 56 insertions, 32 deletions
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?") diff --git a/doc/python_api/examples/bpy.props.5.py b/doc/python_api/examples/bpy.props.5.py index 87741cbab8a..a9e79fa0272 100644 --- a/doc/python_api/examples/bpy.props.5.py +++ b/doc/python_api/examples/bpy.props.5.py @@ -1,13 +1,12 @@ """ -Get/Set Example -+++++++++++++++ +Getter/Setter Example ++++++++++++++++++++++ -Get/Set functions can be used for boolean, int, float, string and enum properties. +Getter/setter functions can be used for boolean, int, float, string and enum properties. If these callbacks are defined the property will not be stored in the ID properties -automatically, instead the get/set functions will be called when the property is -read or written from the API. +automatically. Instead, the `get` and `set` functions will be called when the property +is respectively read or written from the API. """ - import bpy @@ -65,25 +64,24 @@ def set_enum(self, value): bpy.types.Scene.test_enum = bpy.props.EnumProperty(items=test_items, get=get_enum, set=set_enum) -# Testing - +# Testing the properties: scene = bpy.context.scene scene.test_float = 12.34 -print(scene.test_float) +print('test_float:', scene.test_float) scene.test_array = (True, False) -print([x for x in scene.test_array]) +print('test_array:', tuple(scene.test_array)) # scene.test_date = "blah" # this would fail, property is read-only -print(scene.test_date) +print('test_date:', scene.test_date) scene.test_enum = 'BLUE' -print(scene.test_enum) - - -# >>> 12.34000015258789 -# >>> [True, False] -# >>> 2013-01-05 16:33:52.135340 -# >>> setting value 3 -# >>> GREEN +print('test_enum:', scene.test_enum) + +# The above outputs: +# test_float: 12.34000015258789 +# test_array: (True, False) +# test_date: 2018-03-14 11:36:53.158653 +# setting value 3 +# test_enum: GREEN |