-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathwiggle_2.py
1362 lines (1230 loc) · 55.8 KB
/
wiggle_2.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
bl_info = {
"name": "Wiggle 2",
"author": "Steve Miller",
"version": (2, 2, 4),
"blender": (3, 00, 0),
"location": "3d Viewport > Animation Panel",
"description": "Simulate spring-like physics on Bone transforms",
"warning": "",
"wiki_url": "https://github.com/shteeve3d/blender-wiggle-2",
"category": "Animation",
}
### TO DO #####
# Basic object wiggle?
# handle inherit rotation?
# bugs:
# weird glitch when starting playback?
import bpy, math
from mathutils import Vector, Matrix, Euler, Quaternion, geometry
from bpy.app.handlers import persistent
#return m2 in m1 space
def relative_matrix(m1,m2):
return (m2.inverted() @ m1).inverted()
def flatten(mat):
dim = len(mat)
return [mat[j][i] for i in range(dim)
for j in range(dim)]
def reset_scene():
for wo in bpy.context.scene.wiggle.list:
reset_ob(bpy.data.objects.get(wo.name))
def reset_ob(ob):
wo = bpy.context.scene.wiggle.list.get(ob.name)
for wb in wo.list:
reset_bone(bpy.data.objects.get(wo.name).pose.bones.get(wb.name))
def reset_bone(b):
b.wiggle.position = b.wiggle.position_last = (b.id_data.matrix_world @ Matrix.Translation(b.tail)).translation
b.wiggle.position_head = b.wiggle.position_last_head = (b.id_data.matrix_world @ b.matrix).translation
b.wiggle.velocity = b.wiggle.velocity_head = b.wiggle.collision_normal = b.wiggle.collision_normal_head = Vector((0,0,0))
b.wiggle.matrix = flatten(b.id_data.matrix_world @ b.matrix)
def build_list():
bpy.context.scene.wiggle.list.clear()
for ob in bpy.context.scene.objects:
if ob.type != 'ARMATURE': continue
wigglebones = []
for b in ob.pose.bones:
if b.wiggle_tail or (b.wiggle_head and not b.bone.use_connect):
wigglebones.append(b)
b.wiggle_enable = True
else:
b.wiggle_enable = False
# continue
# wigglebones.append(b)
if not wigglebones:
ob.wiggle_enable = False
continue
ob.wiggle_enable = True
wo = bpy.context.scene.wiggle.list.add()
wo.name = ob.name
for b in wigglebones:
wb = wo.list.add()
wb.name = b.name
def update_prop(self,context,prop):
if prop in ['wiggle_mute','wiggle_enable']:
build_list()
if type(self) == bpy.types.PoseBone:
for b in context.selected_pose_bones:
b[prop] = self[prop]
if prop in ['wiggle_head', 'wiggle_tail']:
build_list()
for b in context.selected_pose_bones:
reset_bone(b)
#edge case where is_rendering gets stuck, the user fiddling with any setting should unstuck it!
context.scene.wiggle.is_rendering = False
def get_parent(b):
p = b.parent
if not p: return None
par = p if (p.wiggle_enable and (not p.wiggle_mute) and ((p.wiggle_head and not p.bone.use_connect) or p.wiggle_tail)) else get_parent(p)
return par
def length_world(b):
return (b.id_data.matrix_world @ b.head - b.id_data.matrix_world @ b.tail).length
def collider_poll(self, object):
return object.type == 'MESH'
def wind_poll(self, object):
return object.field and object.field.type =='WIND'
def collide(b,dg,head=False):
dt = bpy.context.scene.wiggle.dt
if head:
pos = b.wiggle.position_head
vel = b.wiggle.velocity_head
cp = b.wiggle.collision_point_head
co = b.wiggle.collision_ob_head
cn = b.wiggle.collision_normal_head
collider_type = b.wiggle_collider_type_head
wiggle_collider = b.wiggle_collider_head
wiggle_collection = b.wiggle_collider_collection_head
radius = b.wiggle_radius_head
sticky = b.wiggle_sticky_head
bounce = b.wiggle_bounce_head
friction = b.wiggle_friction_head
else:
pos = b.wiggle.position
vel = b.wiggle.velocity
cp = b.wiggle.collision_point
co = b.wiggle.collision_ob
cn = b.wiggle.collision_normal
collider_type = b.wiggle_collider_type
wiggle_collider = b.wiggle_collider
wiggle_collection = b.wiggle_collider_collection
radius = b.wiggle_radius
sticky = b.wiggle_sticky
bounce = b.wiggle_bounce
friction = b.wiggle_friction
colliders = []
if collider_type == 'Object' and wiggle_collider:
if wiggle_collider.name in bpy.context.scene.objects:
colliders = [wiggle_collider]
if collider_type == 'Collection' and wiggle_collection:
if wiggle_collection in bpy.context.scene.collection.children_recursive:
colliders = [ob for ob in wiggle_collection.objects if ob.type == 'MESH']
col = False
for collider in colliders:
cmw = collider.matrix_world
p = collider.closest_point_on_mesh(cmw.inverted() @ pos, depsgraph=dg)
n = (cmw.to_quaternion().to_matrix().to_4x4() @ p[2]).normalized()
i = cmw @ p[1]
v = i-pos
if (n.dot(v.normalized()) > 0.01) or (v.length < radius) or (co and (v.length < (radius+sticky))):
if n.dot(v.normalized()) > 0: #vec is below
nv = v.normalized()
else: #normal is opposite dir to vec
nv = -v.normalized()
pos = i + nv*radius
if co:
collision_point = co.matrix_world @ cp
pos = pos.lerp(collision_point, friction) # min(1,friction*60*dt))
col = True
co = collider
cp = relative_matrix(cmw, Matrix.Translation(pos)).translation
cn = nv
if not col:
co = None
# cp = cn = Vector((0,0,0))
if head:
b.wiggle.position_head = pos
b.wiggle.collision_point_head = cp
b.wiggle.collision_ob_head = co
b.wiggle.collision_normal_head = cn
else:
b.wiggle.position = pos
b.wiggle.collision_point = cp
b.wiggle.collision_ob = co
b.wiggle.collision_normal = cn
def update_matrix(b,last=False):
loc = Matrix.Translation(Vector((0,0,0)))
p = get_parent(b)
if p:
mat = p.wiggle.matrix @ relative_matrix(p.matrix, b.matrix)
if b.bone.inherit_scale == 'FULL':
m2 = mat
else:
diff = relative_matrix(p.matrix, b.matrix)
lo = Matrix.Translation((p.wiggle.matrix @ diff).translation)
ro = p.wiggle.matrix.to_quaternion().to_matrix().to_4x4() @ diff.to_quaternion().to_matrix().to_4x4()
sc = Matrix.LocRotScale(None,None,(b.id_data.matrix_world @ b.matrix).decompose()[2])
m2 = lo @ ro @ sc
else:
mat = b.id_data.matrix_world @ b.matrix
m2 = mat
if b.wiggle_head and not b.bone.use_connect:
m2 = Matrix.Translation(b.wiggle.position_head - m2.translation) @ m2
loc = Matrix.Translation(relative_matrix(mat, Matrix.Translation(b.wiggle.position_head)).translation)
mat = m2
vec = relative_matrix(m2, Matrix.Translation(b.wiggle.position)).translation
rxz = vec.to_track_quat('Y','Z')
rot = rxz.to_matrix().to_4x4()
if b.bone.inherit_scale == 'FULL':
l0 = b.bone.length
l1=relative_matrix(mat, Matrix.Translation(b.wiggle.position)).translation.length
sy = l1/l0
else:
par = b.parent
if par:
sy=(b.id_data.matrix_world @ par.matrix @ relative_matrix(par.matrix, b.matrix).translation - b.wiggle.position).length/length_world(b)
if p:
sy = (p.wiggle.matrix @ relative_matrix(p.matrix, b.matrix).translation - b.wiggle.position).length/length_world(b)
else:
sy = (b.id_data.matrix_world @ b.matrix.translation - b.wiggle.position).length/length_world(b)
if b.wiggle_head and not b.bone.use_connect:
sy = (b.wiggle.position_head - b.wiggle.position).length/length_world(b)
# if b.bone.inherit_scale == 'FULL':
# bpy.context.scene.cursor.location = b.wiggle.position
# l0=relative_matrix(mat, Matrix.Translation(b.wiggle.position)).translation.length
# l1=(b.wiggle.position_head - b.wiggle.position).length
# sy = sy*(l0/l1)
# if b.parent:
# sy = sy*(b.parent.length/b.parent.bone.length)
scale = Matrix.Scale(sy,4,Vector((0,1,0)))
if last:
const = False
for c in b.constraints:
if c.enabled: # and not (c.type in ['DAMPED_TRACK', 'TRACK_TO']):
const = True
if const:
b.matrix = b.bone.matrix_local @ b.matrix_basis @ loc @ rot @ scale
else:
b.matrix = b.matrix @ loc @ rot @ scale
b.wiggle.matrix = flatten(m2 @ rot @ scale)
def get_pin(b):
for c in b.constraints:
if c.type in ['DAMPED_TRACK','TRACK_TO','LOCKED_TRACK'] and c.target and not c.mute:
return c
return None
def pin(b):
c = get_pin(b)
if c:
goal = c.target.matrix_world
if c.subtarget:
goal = goal @ c.target.pose.bones[c.subtarget].matrix
b.wiggle.position = b.wiggle.position*(1-c.influence) + goal.translation*c.influence
#can include gravity, wind, etc
def move(b,dg):
dt = bpy.context.scene.wiggle.dt
dt2 = dt * dt
if dt:
if b.wiggle_tail:
damp = max(min(1-b.wiggle_damp*dt, 1),0)
b.wiggle.velocity=b.wiggle.velocity*damp
F = bpy.context.scene.gravity * b.wiggle_gravity
if b.wiggle_wind_ob:
dir = b.wiggle_wind_ob.matrix_world.to_quaternion().to_matrix().to_4x4() @ Vector((0,0,1))
fac = 1 - b.wiggle_wind_ob.field.wind_factor * abs(dir.dot((b.wiggle.position - b.wiggle.matrix.translation).normalized()))
F += dir * fac * b.wiggle_wind_ob.field.strength * b.wiggle_wind / b.wiggle_mass
b.wiggle.position += b.wiggle.velocity + F*dt2
pin(b)
collide(b,dg)
if b.wiggle_head and not b.bone.use_connect:
damp = max(min(1-b.wiggle_damp_head*dt,1),0)
b.wiggle.velocity_head = b.wiggle.velocity_head*damp
F = bpy.context.scene.gravity * b.wiggle_gravity_head
if b.wiggle_wind_ob_head:
dir = b.wiggle_wind_ob_head.matrix_world.to_quaternion().to_matrix().to_4x4() @ Vector((0,0,1))
F += dir * b.wiggle_wind_ob_head.field.strength * b.wiggle_wind_head / b.wiggle_mass_head
b.wiggle.position_head += b.wiggle.velocity_head + F*dt2
collide(b,dg,True)
update_matrix(b)
def constrain(b,i,dg):
dt = bpy.context.scene.wiggle.dt
def get_fac(mass1,mass2):
return 0.5 if mass1 == mass2 else mass1/(mass1+mass2)
def spring(target, position, stiff):
s = target - position
Fs = s * stiff / bpy.context.scene.wiggle.iterations
if (Fs*dt*dt).length > s.length:
return s
return Fs*dt*dt
def stretch(target, position, fac):
s = target - position
return s*(1-fac)
if dt:
p=get_parent(b)
if p:
mat = p.wiggle.matrix @ relative_matrix(p.matrix, b.matrix)
else:
mat = b.id_data.matrix_world @ b.matrix
update_p = False
#spring
if b.wiggle_head and not b.bone.use_connect:
target = mat.translation
s = spring(target, b.wiggle.position_head, b.wiggle_stiff_head)
if p and b.wiggle_chain_head:
if p.wiggle_tail:
fac = get_fac(b.wiggle_mass_head, p.wiggle_mass) if i else p.wiggle_stretch
p.wiggle.position -= s*fac
else:
fac = get_fac(b.wiggle_mass_head, p.wiggle_mass_head)
p.wiggle.position_head -= s*fac
b.wiggle.position_head += s*(1-fac)
else:
b.wiggle.position_head += s
mat = Matrix.LocRotScale(b.wiggle.position_head, mat.decompose()[1], b.matrix.decompose()[2])
target = mat @ Vector((0,b.bone.length,0))
if b.wiggle_tail:
s = spring(target, b.wiggle.position, b.wiggle_stiff)
if b.wiggle_chain:
fac = get_fac(b.wiggle_mass, b.wiggle_mass_head)
b.wiggle.position_head -= s*fac
b.wiggle.position += s*(1-fac)
else:
b.wiggle.position += s
else:
b.wiggle.position = target
else:
mat = Matrix.LocRotScale(mat.decompose()[0], mat.decompose()[1],b.matrix.decompose()[2])
target = mat @ Vector((0, b.bone.length,0))
s = spring(target, b.wiggle.position, b.wiggle_stiff)
if p and b.wiggle_chain and p.wiggle_tail: # and b.bone.use_connect:
fac = get_fac(b.wiggle_mass, p.wiggle_mass)# if i else p.wiggle_stretch
if get_pin(b): fac = 1 - b.wiggle_stretch
if i == 0: fac = p.wiggle_stretch
if p == b.parent and b.bone.use_connect: #direct parent optimization
p.wiggle.position -= s*fac
else:
tailpos = b.wiggle.matrix @ Vector((0,b.bone.length,0))
midpos = (b.wiggle.matrix.translation + tailpos)/2
v1 = midpos-p.wiggle.matrix.translation
tailpos -= s*fac
midpos = (b.wiggle.matrix.translation + tailpos)/2
v2 = midpos-p.wiggle.matrix.translation
sc = v2.length/v1.length
q = v1.rotation_difference(v2)
v3 = q @ (p.wiggle.position - p.wiggle.matrix.translation)
p.wiggle.position = p.wiggle.matrix.translation + v3*sc
b.wiggle.position += s*(1-fac)
update_p = True
else:
b.wiggle.position += s
#stretch
if b.wiggle_head and not b.bone.use_connect:
if p:
if b.parent == p and p.wiggle_tail:
target = p.wiggle.position + (b.wiggle.position_head - p.wiggle.position).normalized()*(b.id_data.matrix_world @ b.head - b.id_data.matrix_world @ p.tail).length
else: #indirect
targetpos = p.wiggle.matrix @ relative_matrix(p.matrix, b.parent.matrix) @ Vector((0,b.parent.length,0))
target = targetpos + (b.wiggle.position_head - targetpos).normalized()*(b.id_data.matrix_world @ b.head - b.id_data.matrix_world @ b.parent.tail).length
elif b.parent:
ptail = b.id_data.matrix_world @ b.parent.tail
target = ptail + (b.wiggle.position_head - ptail).normalized() * (b.id_data.matrix_world @ b.head - b.id_data.matrix_world @ b.parent.tail).length
else:
target = mat.translation
s = stretch(target, b.wiggle.position_head, b.wiggle_stretch_head)
if p and b.wiggle_chain_head:
if p.wiggle_tail:
fac = get_fac(b.wiggle_mass_head, p.wiggle_mass) if i else p.wiggle_stretch
tailpos = p.wiggle.matrix @ relative_matrix(p.matrix, b.parent.matrix) @ Vector((0,b.parent.length,0))
ratio = (p.wiggle.matrix.translation - p.wiggle.position).length/(p.wiggle.matrix.translation - tailpos).length
tailpos -= s*fac
p.wiggle.position -= s*ratio*fac
else: #DOES THIS ASSUME ANYTHING? (No, head only translates, no bone stretching)
fac = get_fac(b.wiggle_mass_head, p.wiggle_mass_head) if i else p.wiggle_stretch_head
p.wiggle.position_head -= s*fac
b.wiggle.position_head += s*(1-fac)
else:
b.wiggle.position_head += s
target = b.wiggle.position_head + (b.wiggle.position - b.wiggle.position_head).normalized()*length_world(b)
if b.wiggle_tail: #tail stretch only relative to head
s = stretch(target, b.wiggle.position, b.wiggle_stretch)
if b.wiggle_chain:
fac = get_fac(b.wiggle_mass, b.wiggle_mass_head) if i else b.wiggle_stretch_head
b.wiggle.position_head -= s*fac
b.wiggle.position += s*(1-fac)
else:
b.wiggle.position += s
else: b.wiggle.position = target
else: #tail stretch relative to parent or none
target = mat.translation + (b.wiggle.position - mat.translation).normalized()*length_world(b)
s = stretch(target, b.wiggle.position, b.wiggle_stretch)
if p and b.wiggle_chain and p.wiggle_tail: #ASSUMES P IS DIRECT PARENT?
# if p.wiggle_tail:
fac = get_fac(b.wiggle_mass, p.wiggle_mass) #if i else p.wiggle_stretch
if get_pin(b): fac = 1 - b.wiggle_stretch
if i == 0: fac = p.wiggle_stretch
# if (mat.translation - p.wiggle.matrix.translation).length == 0:
# fac = 0
if (p == b.parent and b.bone.use_connect): #optimization with direct parent tail
p.wiggle.position -= s*fac
else:
headpos = b.wiggle.matrix.translation
v1 = headpos - p.wiggle.matrix.translation
headpos -= s*fac
v2 = headpos - p.wiggle.matrix.translation
sc = v2.length/v1.length
q = v1.rotation_difference(v2)
v3 = q @ (p.wiggle.position - p.wiggle.matrix.translation)
p.wiggle.position = p.wiggle.matrix.translation + v3*sc
# tailpos = mat @ Vector((0,b.bone.length,0))
# midpos = (mat.translation + tailpos)/2
# v1 = midpos-p.wiggle.matrix.translation
# tailpos -= s*fac
# midpos = (mat.translation + tailpos)/2
# v2 = midpos-p.wiggle.matrix.translation
# sc = v2.length/v1.length
# q = v1.rotation_difference(v2)
# v3 = q @ (p.wiggle.position - p.wiggle.matrix.translation)
# p.wiggle.position = p.wiggle.matrix.translation + v3*sc
b.wiggle.position += s*(1-fac)
update_p = True
else:
b.wiggle.position += s
if update_p:
collide(p,dg)#would only be tail changing
update_matrix(p)
if b.wiggle_tail:
pin(b)
collide(b,dg)
if b.wiggle_head:
collide(b,dg,True)
update_matrix(b)
@persistent
def wiggle_pre(scene):
if (scene.wiggle.lastframe == scene.frame_current) and not scene.wiggle.reset: return
if scene.wiggle.is_rendering: return
if not scene.wiggle_enable:
reset_scene()
return
for wo in scene.wiggle.list:
if wo.name not in scene.objects:
build_list()
return
ob = scene.objects[wo.name]
if ob.wiggle_mute or ob.wiggle_freeze:
reset_ob(ob)
continue
for wb in wo.list:
if wb.name not in ob.pose.bones:
build_list()
return
b = ob.pose.bones[wb.name]
if b.wiggle_mute or not (b.wiggle_head or b.wiggle_tail):
reset_bone(b)
continue
if not b.wiggle.collision_col:
if b.wiggle_collider_collection:
b.wiggle_collider_collection = bpy.data.collections.get(b.wiggle_collider_collection.name)
b.wiggle.collision_col = scene.collection
elif b.wiggle_collider_collection_head:
bpy.data.collections.get(b.wiggle_collider_collection_head.name)
b.wiggle.collision_col = scene.collection
elif b.wiggle_collider:
bpy.data.objects.get(b.wiggle_collider.name)
b.wiggle.collision_col = scene.collection
elif b.wiggle_collider_head:
bpy.data.objects.get(b.wiggle_collider_head.name)
b.wiggle.collision_col = scene.collection
b.location = Vector((0,0,0))
b.rotation_quaternion = Quaternion((1,0,0,0))
b.rotation_euler = Vector((0,0,0))
b.scale = Vector((1,1,1))
bpy.context.view_layer.update()
@persistent
def wiggle_post(scene,dg):
if (scene.wiggle.lastframe == scene.frame_current) and not scene.wiggle.reset: return
if scene.wiggle.reset: return
if not scene.wiggle_enable: return
if scene.wiggle.is_rendering: return
lastframe = scene.wiggle.lastframe
if (scene.frame_current == scene.frame_start) and (scene.wiggle.loop == False) and (scene.wiggle.is_preroll == False):
bpy.ops.wiggle.reset()
return
if scene.frame_current >= lastframe:
frames_elapsed = scene.frame_current - lastframe
else:
e1 = (scene.frame_end - lastframe) + (scene.frame_current - scene.frame_start) + 1
e2 = lastframe - scene.frame_current
frames_elapsed = min(e1,e2)
if frames_elapsed > 4: frames_elapsed = 1 #handle large jumps?
if scene.wiggle.is_preroll: frames_elapsed = 1
scene.wiggle.dt = 1/scene.render.fps * frames_elapsed
scene.wiggle.lastframe = scene.frame_current
for wo in scene.wiggle.list:
ob = scene.objects[wo.name]
if ob.wiggle_mute or ob.wiggle_freeze: continue
bones = []
for wb in wo.list:
b = ob.pose.bones[wb.name]
if b.wiggle_mute or not (b.wiggle_head or b.wiggle_tail):
# reset_bone(b)
continue
bones.append(ob.pose.bones[wb.name])
for b in bones:
b.wiggle.collision_normal = b.wiggle.collision_normal_head = Vector((0,0,0))
move(b,dg)
for i in range(scene.wiggle.iterations):
for b in bones:
constrain(b, scene.wiggle.iterations-1-i,dg)
for b in bones:
update_matrix(b,True) #final update handling constraints?
if frames_elapsed:
for b in bones:
vb = Vector((0,0,0))
if b.wiggle.collision_normal.length:
vb = b.wiggle.velocity.reflect(b.wiggle.collision_normal).project(b.wiggle.collision_normal)*b.wiggle_bounce
b.wiggle.velocity = (b.wiggle.position - b.wiggle.position_last)/max(frames_elapsed,1) + vb
vb = Vector((0,0,0))
if b.wiggle.collision_normal_head.length:
vb = b.wiggle.velocity_head.reflect(b.wiggle.collision_normal_head).project(b.wiggle.collision_normal_head)*b.wiggle_bounce_head
b.wiggle.velocity_head = (b.wiggle.position_head - b.wiggle.position_last_head)/max(frames_elapsed,1) + vb
b.wiggle.position_last = b.wiggle.position
b.wiggle.position_last_head = b.wiggle.position_head
@persistent
def wiggle_render_pre(scene):
scene.wiggle.is_rendering = True
@persistent
def wiggle_render_post(scene):
scene.wiggle.is_rendering = False
@persistent
def wiggle_render_cancel(scene):
scene.wiggle.is_rendering = False
@persistent
def wiggle_load(scene):
build_list()
s = bpy.context.scene
s.wiggle.is_rendering = False
class WiggleCopy(bpy.types.Operator):
"""Copy active wiggle settings to selected bones"""
bl_idname = "wiggle.copy"
bl_label = "Copy Settings to Selected"
@classmethod
def poll(cls,context):
return context.mode in ['POSE'] and context.active_pose_bone and (len(context.selected_pose_bones)>1)
def execute(self,context):
b = context.active_pose_bone
# b.wiggle_enable = b.wiggle_enable
b.wiggle_mute = b.wiggle_mute
b.wiggle_head = b.wiggle_head
b.wiggle_tail = b.wiggle_tail
b.wiggle_head_mute = b.wiggle_head_mute
b.wiggle_tail_mute = b.wiggle_tail_mute
b.wiggle_mass = b.wiggle_mass
b.wiggle_stiff = b.wiggle_stiff
b.wiggle_stretch = b.wiggle_stretch
b.wiggle_damp = b.wiggle_damp
b.wiggle_gravity = b.wiggle_gravity
b.wiggle_wind_ob = b.wiggle_wind_ob
b.wiggle_wind = b.wiggle_wind
b.wiggle_collider_type = b.wiggle_collider_type
b.wiggle_collider = b.wiggle_collider
b.wiggle_collider_collection = b.wiggle_collider_collection
b.wiggle_radius = b.wiggle_radius
b.wiggle_friction = b.wiggle_friction
b.wiggle_bounce = b.wiggle_bounce
b.wiggle_sticky = b.wiggle_sticky
b.wiggle_chain = b.wiggle_chain
b.wiggle_mass_head = b.wiggle_mass_head
b.wiggle_stiff_head = b.wiggle_stiff_head
b.wiggle_stretch_head = b.wiggle_stretch_head
b.wiggle_damp_head = b.wiggle_damp_head
b.wiggle_gravity_head = b.wiggle_gravity_head
b.wiggle_wind_ob_head = b.wiggle_wind_ob_head
b.wiggle_wind_head = b.wiggle_wind_head
b.wiggle_collider_type_head = b.wiggle_collider_type_head
b.wiggle_collider_head = b.wiggle_collider_head
b.wiggle_collider_collection_head = b.wiggle_collider_collection_head
b.wiggle_radius_head = b.wiggle_radius_head
b.wiggle_friction_head = b.wiggle_friction_head
b.wiggle_bounce_head = b.wiggle_bounce_head
b.wiggle_sticky_head = b.wiggle_sticky_head
b.wiggle_chain_head = b.wiggle_chain_head
return {'FINISHED'}
class WiggleReset(bpy.types.Operator):
"""Reset scene wiggle physics to rest state"""
bl_idname = "wiggle.reset"
bl_label = "Reset Physics"
@classmethod
def poll(cls,context):
return context.scene.wiggle_enable and context.mode in ['OBJECT', 'POSE']
def execute(self,context):
context.scene.wiggle.reset = True
context.scene.frame_set(context.scene.frame_current)
context.scene.wiggle.reset = False
rebuild = False
for wo in context.scene.wiggle.list:
ob = context.scene.objects.get(wo.name)
if not ob:
rebuild = True
continue
for wb in wo.list:
b = ob.pose.bones.get(wb.name)
if not b:
rebuild = True
continue
reset_bone(b)
context.scene.wiggle.lastframe = context.scene.frame_current
if rebuild: build_list()
return {'FINISHED'}
class WiggleSelect(bpy.types.Operator):
"""Select wiggle bones on selected objects in pose mode"""
bl_idname = "wiggle.select"
bl_label = "Select Enabled"
@classmethod
def poll(cls,context):
return context.mode in ['POSE']
def execute(self,context):
bpy.ops.pose.select_all(action='DESELECT')
rebuild = False
for wo in context.scene.wiggle.list:
ob = context.scene.objects.get(wo.name)
if not ob:
rebuild = True
continue
for wb in wo.list:
b = ob.pose.bones.get(wb.name)
if not b:
rebuild = True
continue
b.bone.select = True
if rebuild: build_list()
return {'FINISHED'}
class WiggleBake(bpy.types.Operator):
"""Bake this object's visible wiggle bones to keyframes"""
bl_idname = "wiggle.bake"
bl_label = "Bake Wiggle"
@classmethod
def poll(cls,context):
return context.object
def execute(self,context):
def push_nla():
if context.scene.wiggle.bake_overwrite: return
if not context.scene.wiggle.bake_nla: return
if not context.object.animation_data: return
if not context.object.animation_data.action: return
action = context.object.animation_data.action
track = context.object.animation_data.nla_tracks.new()
track.name = action.name
track.strips.new(action.name, int(action.frame_range[0]), action)
push_nla()
bpy.ops.wiggle.reset()
#preroll
duration = context.scene.frame_end - context.scene.frame_start
preroll = context.scene.wiggle.preroll
context.scene.wiggle.is_preroll = False
bpy.ops.wiggle.select()
bpy.ops.wiggle.reset()
while preroll >= 0:
if context.scene.wiggle.loop:
frame = context.scene.frame_end - (preroll%duration)
context.scene.frame_set(frame)
else:
context.scene.frame_set(context.scene.frame_start)
context.scene.wiggle.is_preroll = True
preroll -= 1
#bake
if bpy.app.version[0] >= 4 and bpy.app.version[1] > 0:
bpy.ops.nla.bake(frame_start = context.scene.frame_start,
frame_end = context.scene.frame_end,
only_selected = True,
visual_keying = True,
use_current_action = context.scene.wiggle.bake_overwrite,
bake_types={'POSE'},
channel_types={'LOCATION','ROTATION','SCALE'})
else:
bpy.ops.nla.bake(frame_start = context.scene.frame_start,
frame_end = context.scene.frame_end,
only_selected = True,
visual_keying = True,
use_current_action = context.scene.wiggle.bake_overwrite,
bake_types={'POSE'})
context.scene.wiggle.is_preroll = False
context.object.wiggle_freeze = True
if not context.scene.wiggle.bake_overwrite:
context.object.animation_data.action.name = 'WiggleAction'
return {'FINISHED'}
class WigglePanel:
bl_category = 'Animation'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
@classmethod
def poll(cls,context):
return context.object
class WIGGLE_PT_Settings(WigglePanel, bpy.types.Panel):
bl_label = 'Wiggle 2'
def draw(self,context):
row = self.layout.row()
icon = 'HIDE_ON' if not context.scene.wiggle_enable else 'SCENE_DATA'
row.prop(context.scene, "wiggle_enable", icon=icon, text="",emboss=False)
if not context.scene.wiggle_enable:
row.label(text='Scene muted.')
return
if not context.object.type == 'ARMATURE':
row.label(text = ' Select armature.')
return
# row.label(icon='TRIA_RIGHT')
if context.object.wiggle_freeze:
row.prop(context.object,'wiggle_freeze',icon='FREEZE',icon_only=True,emboss=False)
row.label(text = 'Wiggle Frozen after Bake.')
return
icon = 'HIDE_ON' if context.object.wiggle_mute else 'ARMATURE_DATA'
row.prop(context.object,'wiggle_mute',icon=icon,icon_only=True,invert_checkbox=True,emboss=False)
if context.object.wiggle_mute:
row.label(text='Armature muted.')
return
if not context.active_pose_bone:
row.label(text = ' Select pose bone.')
return
# row.label(icon='TRIA_RIGHT')
icon = 'HIDE_ON' if context.active_pose_bone.wiggle_mute else 'BONE_DATA'
row.prop(context.active_pose_bone,'wiggle_mute',icon=icon,icon_only=True,invert_checkbox=True,emboss=False)
if context.active_pose_bone.wiggle_mute:
row.label(text='Bone muted.')
return
class WIGGLE_PT_Head(WigglePanel,bpy.types.Panel):
bl_label = ''
bl_parent_id = 'WIGGLE_PT_Settings'
bl_options = {'HEADER_LAYOUT_EXPAND'}
@classmethod
def poll(cls,context):
# return context.active_pose_bone and not context.active_pose_bone.bone.use_connect
return context.scene.wiggle_enable and context.object and not context.object.wiggle_mute and context.active_pose_bone and not context.active_pose_bone.wiggle_mute and not context.active_pose_bone.bone.use_connect
def draw_header(self,context):
row=self.layout.row(align=True)
row.prop(context.active_pose_bone, 'wiggle_head')
def draw(self,context):
b = context.active_pose_bone
if not b.wiggle_head: return
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False
def drawprops(layout,b,props):
for p in props:
layout.prop(b, p)
col = layout.column(align=True)
drawprops(col,b,['wiggle_mass_head','wiggle_stiff_head','wiggle_stretch_head','wiggle_damp_head'])
col.separator()
col.prop(b,'wiggle_gravity_head')
row=col.row(align=True)
row.prop(b,'wiggle_wind_ob_head')
sub = row.row(align=True)
sub.ui_units_x = 5
sub.prop(b, 'wiggle_wind_head', text='')
col.separator()
col.prop(b, 'wiggle_collider_type_head',text='Collisions')
collision = False
if b.wiggle_collider_type_head == 'Object':
row = col.row(align=True)
row.prop_search(b, 'wiggle_collider_head', context.scene, 'objects',text=' ')
if b.wiggle_collider_head:
if b.wiggle_collider_head.name in context.scene.objects:
collision = True
else:
row.label(text='',icon='UNLINKED')
else:
row = col.row(align=True)
row.prop_search(b, 'wiggle_collider_collection_head', bpy.data, 'collections', text=' ')
if b.wiggle_collider_collection_head:
if b.wiggle_collider_collection_head in context.scene.collection.children_recursive:
collision = True
else:
row.label(text='',icon='UNLINKED')
if collision:
col = layout.column(align=True)
drawprops(col,b,['wiggle_radius_head','wiggle_friction_head','wiggle_bounce_head','wiggle_sticky_head'])
layout.prop(b,'wiggle_chain_head')
class WIGGLE_PT_Tail(WigglePanel,bpy.types.Panel):
bl_label = ''
bl_parent_id = 'WIGGLE_PT_Settings'
bl_options = {'HEADER_LAYOUT_EXPAND'}
@classmethod
def poll(cls,context):
# return context.active_pose_bone
return context.scene.wiggle_enable and context.object and not context.object.wiggle_mute and context.active_pose_bone and not context.active_pose_bone.wiggle_mute
def draw_header(self,context):
row=self.layout.row(align=True)
row.prop(context.active_pose_bone, 'wiggle_tail')
def draw(self,context):
b = context.active_pose_bone
if not b.wiggle_tail: return
layout = self.layout
layout.use_property_split = True
layout.use_property_decorate = False
def drawprops(layout,b,props):
for p in props:
layout.prop(b, p)
col = layout.column(align=True)
drawprops(col,b,['wiggle_mass','wiggle_stiff','wiggle_stretch','wiggle_damp'])
col.separator()
col.prop(b,'wiggle_gravity')
row=col.row(align=True)
row.prop(b,'wiggle_wind_ob')
sub = row.row(align=True)
sub.ui_units_x = 5
sub.prop(b, 'wiggle_wind', text='')
col.separator()
col.prop(b, 'wiggle_collider_type',text='Collisions')
collision = False
if b.wiggle_collider_type == 'Object':
row = col.row(align=True)
row.prop_search(b, 'wiggle_collider', context.scene, 'objects',text=' ')
if b.wiggle_collider:
if b.wiggle_collider.name in context.scene.objects:
collision = True
else:
row.label(text='',icon='UNLINKED')
else:
row = col.row(align=True)
row.prop_search(b, 'wiggle_collider_collection', bpy.data, 'collections', text=' ')
if b.wiggle_collider_collection:
if b.wiggle_collider_collection in context.scene.collection.children_recursive:
collision = True
else:
row.label(text='',icon='UNLINKED')
if collision:
col = layout.column(align=True)
drawprops(col,b,['wiggle_radius','wiggle_friction','wiggle_bounce','wiggle_sticky'])
layout.prop(b,'wiggle_chain')
class WIGGLE_PT_Utilities(WigglePanel,bpy.types.Panel):
bl_label = 'Global Wiggle Utilities'
bl_parent_id = 'WIGGLE_PT_Settings'
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(cls,context):
return context.scene.wiggle_enable
def draw(self,context):
layout = self.layout
layout.use_property_split=True
layout.use_property_decorate=False
col = layout.column(align=True)
if context.object.wiggle_enable and context.mode == 'POSE':
col.operator('wiggle.copy')
col.operator('wiggle.select')
col.operator('wiggle.reset')
layout.prop(context.scene.wiggle, 'loop')
layout.prop(context.scene.wiggle, 'iterations')
class WIGGLE_PT_Bake(WigglePanel,bpy.types.Panel):
bl_label = 'Bake Wiggle'
bl_parent_id = 'WIGGLE_PT_Utilities'
bl_options = {"DEFAULT_CLOSED"}
@classmethod
def poll(cls,context):
return context.scene.wiggle_enable and context.object.wiggle_enable and context.mode == 'POSE'
def draw(self,context):
layout = self.layout
layout.use_property_split=True
layout.use_property_decorate=False
layout.prop(context.scene.wiggle, 'preroll')
layout.prop(context.scene.wiggle, 'bake_overwrite')
row = layout.row()
row.enabled = not context.scene.wiggle.bake_overwrite
row.prop(context.scene.wiggle, 'bake_nla')
layout.operator('wiggle.bake')
class WiggleBoneItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty(override={'LIBRARY_OVERRIDABLE'})
class WiggleItem(bpy.types.PropertyGroup):
name: bpy.props.StringProperty(override={'LIBRARY_OVERRIDABLE'})
list: bpy.props.CollectionProperty(type=WiggleBoneItem, override={'LIBRARY_OVERRIDABLE','USE_INSERTION'})
#store properties for a bone. custom properties for user editable. property group for internal calculations
class WiggleBone(bpy.types.PropertyGroup):
matrix: bpy.props.FloatVectorProperty(name = 'Matrix', size=16, subtype = 'MATRIX', override={'LIBRARY_OVERRIDABLE'})
position: bpy.props.FloatVectorProperty(subtype='TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
position_last: bpy.props.FloatVectorProperty(subtype='TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
velocity: bpy.props.FloatVectorProperty(subtype='VELOCITY', override={'LIBRARY_OVERRIDABLE'})
collision_point:bpy.props.FloatVectorProperty(subtype = 'TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
collision_ob: bpy.props.PointerProperty(type=bpy.types.Object, override={'LIBRARY_OVERRIDABLE'})
collision_normal: bpy.props.FloatVectorProperty(subtype = 'TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
collision_col: bpy.props.PointerProperty(type=bpy.types.Collection,override={'LIBRARY_OVERRIDABLE'})
position_head: bpy.props.FloatVectorProperty(subtype='TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
position_last_head: bpy.props.FloatVectorProperty(subtype='TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
velocity_head: bpy.props.FloatVectorProperty(subtype='VELOCITY', override={'LIBRARY_OVERRIDABLE'})
collision_point_head:bpy.props.FloatVectorProperty(subtype = 'TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
collision_ob_head: bpy.props.PointerProperty(type=bpy.types.Object, override={'LIBRARY_OVERRIDABLE'})
collision_normal_head: bpy.props.FloatVectorProperty(subtype = 'TRANSLATION', override={'LIBRARY_OVERRIDABLE'})
class WiggleObject(bpy.types.PropertyGroup):
list: bpy.props.CollectionProperty(type=WiggleItem, override={'LIBRARY_OVERRIDABLE'})
class WiggleScene(bpy.types.PropertyGroup):
dt: bpy.props.FloatProperty()
lastframe: bpy.props.IntProperty()
iterations: bpy.props.IntProperty(name='Quality', description='Constraint solver interations for chain physics', min=1, default=2, soft_max=10, max=100)
loop: bpy.props.BoolProperty(name='Loop Physics', description='Physics continues as timeline loops', default=True)
list: bpy.props.CollectionProperty(type=WiggleItem, override={'LIBRARY_OVERRIDABLE','USE_INSERTION'})
preroll: bpy.props.IntProperty(name = 'Preroll', description='Frames to run simulation before bake', min=0, default=0)
is_preroll: bpy.props.BoolProperty(default=False)
bake_overwrite: bpy.props.BoolProperty(name='Overwrite Current Action', description='Bake wiggle into current action, instead of creating a new one', default = False)
bake_nla: bpy.props.BoolProperty(name='Current Action to NLA', description='Move existing animation on the armature into an NLA strip', default = False)
is_rendering: bpy.props.BoolProperty(default=False)
reset: bpy.props.BoolProperty(default=False)
def register():
#WIGGLE TOGGLES
bpy.types.Scene.wiggle_enable = bpy.props.BoolProperty(
name = 'Enable Scene',
description = 'Enable wiggle on this scene',
default = False,
override={'LIBRARY_OVERRIDABLE'},
update=lambda s, c: update_prop(s, c, 'wiggle_enable')
)
bpy.types.Object.wiggle_enable = bpy.props.BoolProperty(
name = 'Enable Armature',
description = 'Enable wiggle on this armature',
default = False,
options={'HIDDEN'},
override={'LIBRARY_OVERRIDABLE'}
# update=lambda s, c: update_prop(s, c, 'wiggle_enable')
)
bpy.types.Object.wiggle_mute = bpy.props.BoolProperty(
name = 'Mute Armature',
description = 'Mute wiggle on this armature.',
default = False,
override={'LIBRARY_OVERRIDABLE'},
update=lambda s, c: update_prop(s, c, 'wiggle_mute')
)