Docs: update examples for 2.8x

Use fields for properties
This commit is contained in:
Campbell Barton 2019-03-22 00:07:06 +11:00
parent bd1299f6a4
commit dd1132416e
13 changed files with 90 additions and 60 deletions

@ -15,13 +15,15 @@ class OBJECT_OT_property_example(bpy.types.Operator):
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")
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: %r' %
(self.my_float, self.my_bool, self.my_string))
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)
@ -53,5 +55,8 @@ 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?")
bpy.ops.object.property_example(
my_float=47,
my_bool=True,
my_string="Shouldn't that be 327?",
)

@ -10,15 +10,14 @@ import bpy
class MaterialSettings(bpy.types.PropertyGroup):
my_int = bpy.props.IntProperty()
my_float = bpy.props.FloatProperty()
my_string = bpy.props.StringProperty()
my_int: bpy.props.IntProperty()
my_float: bpy.props.FloatProperty()
my_string: bpy.props.StringProperty()
bpy.utils.register_class(MaterialSettings)
bpy.types.Material.my_settings = \
bpy.props.PointerProperty(type=MaterialSettings)
bpy.types.Material.my_settings = bpy.props.PointerProperty(type=MaterialSettings)
# test the new settings work
material = bpy.data.materials[0]

@ -9,18 +9,17 @@ Custom properties can be added to any subclass of an :class:`ID`,
import bpy
# Assign a collection
# Assign a collection.
class SceneSettingItem(bpy.types.PropertyGroup):
name = bpy.props.StringProperty(name="Test Prop", default="Unknown")
value = bpy.props.IntProperty(name="Test Prop", default=22)
name: bpy.props.StringProperty(name="Test Property", default="Unknown")
value: bpy.props.IntProperty(name="Test Property", default=22)
bpy.utils.register_class(SceneSettingItem)
bpy.types.Scene.my_settings = \
bpy.props.CollectionProperty(type=SceneSettingItem)
bpy.types.Scene.my_settings = bpy.props.CollectionProperty(type=SceneSettingItem)
# Assume an armature object selected
# Assume an armature object selected.
print("Adding 2 values!")
my_item = bpy.context.scene.my_settings.add()

@ -12,7 +12,7 @@ like Blender's existing properties.
import bpy
# Assign a custom property to an existing type.
bpy.types.Material.custom_float = bpy.props.FloatProperty(name="Test Prob")
bpy.types.Material.custom_float = bpy.props.FloatProperty(name="Test Property")
# Test the property is there.
bpy.data.materials[0].custom_float = 5.0

@ -22,15 +22,15 @@ class ExampleAddonPreferences(AddonPreferences):
# when defining this in a submodule of a python package.
bl_idname = __name__
filepath = StringProperty(
filepath: StringProperty(
name="Example File Path",
subtype='FILE_PATH',
)
number = IntProperty(
number: IntProperty(
name="Example Number",
default=4,
)
boolean = BoolProperty(
boolean: BoolProperty(
name="Example Boolean",
default=False,
)

@ -28,8 +28,8 @@ class SimpleMouseOperator(bpy.types.Operator):
bl_idname = "wm.mouse_position"
bl_label = "Invoke Mouse Operator"
x = bpy.props.IntProperty()
y = bpy.props.IntProperty()
x: bpy.props.IntProperty()
y: bpy.props.IntProperty()
def execute(self, context):
# rather than printing, use the report function,

@ -21,7 +21,7 @@ class ExportSomeData(bpy.types.Operator):
bl_idname = "export.some_data"
bl_label = "Export Some Data"
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
@classmethod
def poll(cls, context):

@ -11,13 +11,15 @@ class DialogOperator(bpy.types.Operator):
bl_idname = "object.dialog_operator"
bl_label = "Simple Dialog Operator"
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")
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):
message = "Popup Values: %f, %d, '%s'" % \
message = (
"Popup Values: %f, %d, '%s'" %
(self.my_float, self.my_bool, self.my_string)
)
self.report({'INFO'}, message)
return {'FINISHED'}
@ -28,5 +30,5 @@ class DialogOperator(bpy.types.Operator):
bpy.utils.register_class(DialogOperator)
# test call
# Test call.
bpy.ops.object.dialog_operator('INVOKE_DEFAULT')

@ -16,11 +16,11 @@ class CustomDrawOperator(bpy.types.Operator):
bl_idname = "object.custom_draw"
bl_label = "Simple Modal Operator"
filepath = bpy.props.StringProperty(subtype="FILE_PATH")
filepath: bpy.props.StringProperty(subtype="FILE_PATH")
my_float = bpy.props.FloatProperty(name="Float")
my_bool = bpy.props.BoolProperty(name="Toggle Option")
my_string = bpy.props.StringProperty(name="String Value")
my_float: bpy.props.FloatProperty(name="Float")
my_bool: bpy.props.BoolProperty(name="Toggle Option")
my_string: bpy.props.StringProperty(name="String Value")
def execute(self, context):
print("Test", self)

@ -14,7 +14,7 @@ class SearchEnumOperator(bpy.types.Operator):
bl_label = "Search Enum Operator"
bl_property = "my_search"
my_search = EnumProperty(
my_search: EnumProperty(
name="My Search",
items=(
('FOO', "Foo", ""),

@ -28,8 +28,8 @@ import bpy
class MyPropertyGroup(bpy.types.PropertyGroup):
custom_1 = bpy.props.FloatProperty(name="My Float")
custom_2 = bpy.props.IntProperty(name="My Int")
custom_1: bpy.props.FloatProperty(name="My Float")
custom_2: bpy.props.IntProperty(name="My Int")
bpy.utils.register_class(MyPropertyGroup)

@ -15,12 +15,24 @@ class MESH_UL_vgroups_slow(bpy.types.UIList):
VGROUP_EMPTY = 1 << 0
# Custom properties, saved with .blend file.
use_filter_empty = bpy.props.BoolProperty(name="Filter Empty", default=False, options=set(),
description="Whether to filter empty vertex groups")
use_filter_empty_reverse = bpy.props.BoolProperty(name="Reverse Empty", default=False, options=set(),
description="Reverse empty filtering")
use_filter_name_reverse = bpy.props.BoolProperty(name="Reverse Name", default=False, options=set(),
description="Reverse name filtering")
use_filter_empty: bpy.props.BoolProperty(
name="Filter Empty",
default=False,
options=set(),
description="Whether to filter empty vertex groups",
)
use_filter_empty_reverse: bpy.props.BoolProperty(
name="Reverse Empty",
default=False,
options=set(),
description="Reverse empty filtering",
)
use_filter_name_reverse: bpy.props.BoolProperty(
name="Reverse Name",
default=False,
options=set(),
description="Reverse name filtering",
)
# This allows us to have mutually exclusive options, which are also all disable-able!
def _gen_order_update(name1, name2):
@ -28,12 +40,18 @@ class MESH_UL_vgroups_slow(bpy.types.UIList):
if (getattr(self, name1)):
setattr(self, name2, False)
return _u
use_order_name = bpy.props.BoolProperty(name="Name", default=False, options=set(),
description="Sort groups by their name (case-insensitive)",
update=_gen_order_update("use_order_name", "use_order_importance"))
use_order_importance = bpy.props.BoolProperty(name="Importance", default=False, options=set(),
description="Sort groups by their average weight in the mesh",
update=_gen_order_update("use_order_importance", "use_order_name"))
use_order_name: bpy.props.BoolProperty(
name="Name", default=False, options=set(),
description="Sort groups by their name (case-insensitive)",
update=_gen_order_update("use_order_name", "use_order_importance"),
)
use_order_importance: bpy.props.BoolProperty(
name="Importance",
default=False,
options=set(),
description="Sort groups by their average weight in the mesh",
update=_gen_order_update("use_order_importance", "use_order_name"),
)
# Usual draw item function.
def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index, flt_flag):

@ -5,25 +5,32 @@ than the bone.
"""
import bpy
from bpy.props import PointerProperty
from bpy.props import (
FloatProperty,
PointerProperty,
)
# define a nested property
# Define a nested property.
class MyPropGroup(bpy.types.PropertyGroup):
nested = bpy.props.FloatProperty(name="Nested", default=0.0)
nested: FloatProperty(name="Nested", default=0.0)
# register it so its available for all bones
# Register it so its available for all bones.
bpy.utils.register_class(MyPropGroup)
bpy.types.Bone.my_prop = PointerProperty(type=MyPropGroup,
name="MyProp")
bpy.types.Bone.my_prop = PointerProperty(
type=MyPropGroup,
name="MyProp",
)
# get a bone
# Get a bone.
obj = bpy.data.objects["Armature"]
arm = obj.data
# set the keyframe at frame 1
# Set the keyframe at frame 1.
arm.bones["Bone"].my_prop_group.nested = 10
arm.keyframe_insert(data_path='bones["Bone"].my_prop.nested',
frame=1,
group="Nested Group")
arm.keyframe_insert(
data_path='bones["Bone"].my_prop.nested',
frame=1,
group="Nested Group",
)