-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
232 lines (188 loc) · 4.94 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import bpy, re, mathutils
from bpy.app.handlers import persistent
from math import sqrt, pow, sin, cos, asin, acos
from math import pi as M_PI
M_PI_2 = M_PI * 2
bl_info = {
'name': 'Driver Functions',
'version': (1, 0, 0),
'author': 'passivestar',
'blender': (3, 0, 0),
'description': 'Adds useful functions to the driver namespace',
'category': 'Animation'
}
# @Easings
class easings(object):
def __init__(self):
pass
@staticmethod
def in_quad(p):
return p * p
@staticmethod
def out_quad(p):
return -(p * (p - 2))
@staticmethod
def in_out_quad(p):
if p < 0.5:
return 2 * p * p
return (-2 * p * p) + (4 * p) - 1
@staticmethod
def in_cubic(p):
return p * p * p
@staticmethod
def out_cubic(p):
f = p - 1
return f * f * f + 1
@staticmethod
def in_out_cubic(p):
if p < 0.5:
return 4 * p * p * p
else:
f = (2 * p) - 2
return 0.5 * f * f * f + 1
@staticmethod
def in_quart(p):
return p * p * p * p
@staticmethod
def out_quart(p):
f = p - 1
return f * f * f * (1 - p) + 1
@staticmethod
def in_out_quart(p):
if p < 0.5:
return 8 * p * p * p * p
else:
f = p - 1
return -8 * f * f * f * f + 1
@staticmethod
def in_quint(p):
return p * p * p * p * p
@staticmethod
def out_quint(p):
f = p - 1
return f * f * f * f * f + 1
@staticmethod
def in_out_quint(p):
if p < 0.5:
return 16 * p * p * p * p * p
else:
f = (2 * p) - 2
return 0.5 * f * f * f * f * f + 1
@staticmethod
def in_sin(p):
return sin((p - 1) * M_PI_2) + 1
@staticmethod
def out_sin(p):
return sin(p * M_PI_2)
@staticmethod
def in_out_sin(p):
return 0.5 * (1 - cos(p * M_PI))
@staticmethod
def in_circle(p):
return 1 - sqrt(1 - (p * p))
@staticmethod
def out_circle(p):
return sqrt((2 - p) * p)
@staticmethod
def in_out_circle(p):
if p < 0.5:
return 0.5 * (1 - sqrt(1 - 4 * (p * p)))
else:
return 0.5 * (sqrt(-((2 * p) - 3) * ((2 * p) - 1)) + 1)
@staticmethod
def in_exp(p):
return p if p == 0 else pow(2, 10 * (p - 1))
@staticmethod
def out_exp(p):
return p if p == 1 else 1 - pow(2, -10 * p)
@staticmethod
def in_out_exp(p):
if p == 0 or p == 1:
return p
if p < 0.5:
return 0.5 * pow(2, (20 * p) - 10)
else:
return -0.5 * pow(2, (-20 * p) + 10) + 1
@staticmethod
def in_elastic(p):
return sin(13 * M_PI_2 * p) * pow(2, 10 * (p - 1))
@staticmethod
def out_elastic(p):
return sin(-13 * M_PI_2 * (p + 1)) * pow(2, -10 * p) + 1
@staticmethod
def in_out_elastic(p):
if p < 0.5:
return 0.5 * sin(13 * M_PI_2 * (2 * p)) * pow(2, 10 * ((2 * p) - 1))
else:
return 0.5 * (sin(-13 * M_PI_2 * ((2 * p - 1) + 1)) * pow(2, -10 * (2 * p - 1)) + 2)
@staticmethod
def in_back(p):
return p * p * p - p * sin(p * M_PI)
@staticmethod
def out_back(p):
f = 1 - p
return 1 - (f * f * f - f * sin(f * M_PI))
@staticmethod
def in_out_back(p):
if p < 0.5:
f = 2 * p
return 0.5 * (f * f * f - f * sin(f * M_PI))
else:
f = (1 - (2 * p - 1))
return 0.5 * (1 - (f * f * f - f * sin(f * M_PI))) + 0.5
@staticmethod
def in_bounce(p):
return 1 - easings.out_bounce(1 - p)
@staticmethod
def out_bounce(p):
if p < 4 / 11:
return (121 * p * p) / 16
elif p < 8 / 11:
return (363 / 40 * p * p) - (99 / 10 * p) + 17 / 5
elif p < 9 / 10:
return (4356 / 361 * p * p) - (35442 / 1805 * p) + 16061 / 1805
else:
return (54 / 5 * p * p) - (513 / 25 * p) + 268 / 25
@staticmethod
def in_out_bounce(p):
if p < 0.5:
return 0.5 * easings.in_bounce(p * 2)
else:
return 0.5 * easings.out_bounce(p * 2 - 1) + 0.5
# @Functions
# Current object's index derived from the name
def i(obj):
match = re.search('(\d+)$', obj.name)
return int(match.group(1)) if match else 0
# Get object by name
def obj(name):
return bpy.data.objects[name]
# Transforms
def loc(name): return bpy.data.objects[name].location
def rot(name): return bpy.data.objects[name].rotation
def scale(name): return bpy.data.objects[name].scale
def dloc(name): return bpy.data.objects[name].delta_location
def drot(name): return bpy.data.objects[name].delta_rotation
def dscale(name): return bpy.data.objects[name].delta_scale
# Noise
def rand(n): return mathutils.noise.noise((n, 0, 0))
# How far into the timeline we are
def t():
scene = bpy.context.scene
return scene.frame_current / scene.frame_end
functions_and_classes = (
i, obj, loc, rot, scale, dloc, drot, dscale, rand, t, easings
)
@persistent
def load_handler(dummy):
dn = bpy.app.driver_namespace
for el in functions_and_classes:
dn[el.__name__] = el
# Shortcuts
dn['es'] = dn['easings']
def register():
load_handler(None)
bpy.app.handlers.load_post.append(load_handler)
def unregister():
bpy.app.handlers.load_post.remove(load_handler)
if __name__ == '__main__': register()