~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to doc/python_api/examples/bpy.props.5.py

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Get/Set Example
 
3
++++++++++++++
 
4
 
 
5
Get/Set functions can be used for boolean, int, float, string and enum properties.
 
6
If these callbacks are defined the property will not be stored in the ID properties
 
7
automatically, instead the get/set functions will be called when the property is
 
8
read or written from the API.
 
9
"""
 
10
 
 
11
import bpy
 
12
 
 
13
 
 
14
# Simple property reading/writing from ID properties.
 
15
# This is what the RNA would do internally.
 
16
def get_float(self):
 
17
    return self["testprop"]
 
18
 
 
19
 
 
20
def set_float(self, value):
 
21
    self["testprop"] = value
 
22
 
 
23
bpy.types.Scene.test_float = bpy.props.FloatProperty(get=get_float, set=set_float)
 
24
 
 
25
 
 
26
# Read-only string property, returns the current date
 
27
def get_date(self):
 
28
    import datetime
 
29
    return str(datetime.datetime.now())
 
30
 
 
31
bpy.types.Scene.test_date = bpy.props.StringProperty(get=get_date)
 
32
 
 
33
 
 
34
# Boolean array. Set function stores a single boolean value, returned as the second component.
 
35
# Array getters must return a list or tuple
 
36
# Array size must match the property vector size exactly
 
37
def get_array(self):
 
38
    return (True, self["somebool"])
 
39
 
 
40
 
 
41
def set_array(self, values):
 
42
    self["somebool"] = values[0] and values[1]
 
43
 
 
44
bpy.types.Scene.test_array = bpy.props.BoolVectorProperty(size=2, get=get_array, set=set_array)
 
45
 
 
46
 
 
47
# Enum property.
 
48
# Note: the getter/setter callback must use integer identifiers!
 
49
test_items = [
 
50
    ("RED", "Red", "", 1),
 
51
    ("GREEN", "Red", "", 2),
 
52
    ("BLUE", "Red", "", 3),
 
53
    ("YELLOW", "Red", "", 4),
 
54
    ]
 
55
 
 
56
 
 
57
def get_enum(self):
 
58
    import random
 
59
    return random.randint(1, 4)
 
60
 
 
61
 
 
62
def set_enum(self, value):
 
63
    print("setting value", value)
 
64
 
 
65
bpy.types.Scene.test_enum = bpy.props.EnumProperty(items=test_items, get=get_enum, set=set_enum)
 
66
 
 
67
 
 
68
# Testing
 
69
 
 
70
scene = bpy.context.scene
 
71
 
 
72
scene.test_float = 12.34
 
73
print(scene.test_float)
 
74
 
 
75
scene.test_array = (True, False)
 
76
print([x for x in scene.test_array])
 
77
 
 
78
#scene.test_date = "blah"   # this would fail, property is read-only
 
79
print(scene.test_date)
 
80
 
 
81
scene.test_enum = 'BLUE'
 
82
print(scene.test_enum)
 
83
 
 
84
 
 
85
# >>> 12.34000015258789
 
86
# >>> [True, False]
 
87
# >>> 2013-01-05 16:33:52.135340
 
88
# >>> setting value 3
 
89
# >>> GREEN