-
Notifications
You must be signed in to change notification settings - Fork 61
/
car_rig.py
1458 lines (1212 loc) · 58.7 KB
/
car_rig.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
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
# <pep8 compliant>
import bpy
import math
import bpy_extras
import mathutils
import re
from math import inf
from rna_prop_ui import rna_idprop_ui_create
CUSTOM_SHAPE_LAYER = 13
MCH_BONE_EXTENSION_LAYER = 14
DEF_BONE_LAYER = 15
MCH_BONE_LAYER = 31
def deselect_edit_bones(ob):
for b in ob.data.edit_bones:
b.select = False
b.select_head = False
b.select_tail = False
def create_constraint_influence_driver(ob, cns, driver_data_path, base_influence=1.0):
fcurve = cns.driver_add('influence')
drv = fcurve.driver
drv.type = 'AVERAGE'
var = drv.variables.new()
var.name = 'influence'
var.type = 'SINGLE_PROP'
targ = var.targets[0]
targ.id_type = 'OBJECT'
targ.id = ob
targ.data_path = driver_data_path
if base_influence != 1.0:
fmod = fcurve.modifiers[0]
fmod.mode = 'POLYNOMIAL'
fmod.poly_order = 1
fmod.coefficients = (0, base_influence)
def create_rotation_euler_x_driver(ob, bone, driver_data_path):
fcurve = bone.driver_add('rotation_euler', 0)
drv = fcurve.driver
drv.type = 'AVERAGE'
var = drv.variables.new()
var.name = 'rotationAngle'
var.type = 'SINGLE_PROP'
targ = var.targets[0]
targ.id_type = 'OBJECT'
targ.id = ob
targ.data_path = driver_data_path
def create_translation_x_driver(ob, bone, driver_data_path):
fcurve = bone.driver_add('location', 0)
drv = fcurve.driver
drv.type = 'AVERAGE'
var = drv.variables.new()
var.name = 'rotationAngle'
var.type = 'SINGLE_PROP'
targ = var.targets[0]
targ.id_type = 'OBJECT'
targ.id = ob
targ.data_path = driver_data_path
def create_bone_group(pose, group_name, color_set, bone_names):
group = pose.bone_groups.new(name=group_name)
group.color_set = color_set
for bone_name in bone_names:
bone = pose.bones.get(bone_name)
if bone is not None:
bone.bone_group = group
def name_range(prefix, nb=1000):
if nb > 0:
yield prefix
for i in range(1, nb):
yield '%s.%03d' % (prefix, i)
def get_widget(name):
widget = bpy.data.objects.get(name)
if widget is None:
from . import widgets
widgets.create()
widget = bpy.data.objects.get(name)
return widget
def define_custom_property(target, name, value, description=None, overridable=True):
rna_idprop_ui_create(target, name, default=value, description=description, overridable=overridable, min=-inf, max=inf)
def dispatch_bones_to_armature_layers(ob):
re_mch_bone = re.compile(r'^MCH-Wheel(Brake)?\.(Ft|Bk)\.[LR](\.\d+)?$')
default_visible_layers = [False] * 32
for b in ob.data.bones:
layers = [False] * 32
if b.name.startswith('DEF-'):
layers[DEF_BONE_LAYER] = True
elif b.name.startswith('MCH-'):
layers[MCH_BONE_LAYER] = True
if b.name in ('MCH-Body', 'MCH-Steering') or re_mch_bone.match(b.name):
layers[MCH_BONE_EXTENSION_LAYER] = True
else:
layer_num = ob.pose.bones[b.name].bone_group_index
layers[layer_num] = True
default_visible_layers[layer_num] = True
b.layers = layers
ob.data.layers = default_visible_layers
shape_bone_layers = [False] * 32
shape_bone_layers[CUSTOM_SHAPE_LAYER] = True
for b in ob.pose.bones:
if b.custom_shape:
if b.custom_shape_transform:
ob.pose.bones[b.custom_shape_transform.name].custom_shape = b.custom_shape
ob.data.bones[b.custom_shape_transform.name].layers = shape_bone_layers
else:
ob.data.bones[b.name].layers[CUSTOM_SHAPE_LAYER] = True
class NameSuffix(object):
def __init__(self, position, side, index=0):
self.position = position
self.side = side
self.index = index
if index == 0:
self.value = '%s.%s' % (position, side)
else:
self.value = '%s.%s.%03d' % (position, side, index)
def name(self, base_name=None):
return '%s.%s' % (base_name, self.value) if base_name else self.value
@property
def is_front(self):
return self.position == 'Ft'
@property
def is_left(self):
return self.side == 'L'
@property
def is_first(self):
return self.index == 0
def __str__(self):
return self.value
class BoundingBox(object):
def __init__(self, armature, bone_name):
objs = [o for o in armature.children if o.parent_bone == bone_name]
bone = armature.data.bones[bone_name]
self.__center = bone.head.copy()
if not objs:
self.__xyz = [bone.head.x - bone.length / 2, bone.head.x + bone.length / 2, bone.head.y - bone.length, bone.head.y + bone.length, .0, bone.head.z * 2]
else:
self.__xyz = [inf, -inf, inf, -inf, inf, -inf]
self.__compute(mathutils.Matrix(), *objs)
def __compute(self, pmatrix, *objs):
for obj in objs:
omatrix = pmatrix @ obj.matrix_world
if obj.instance_type == 'COLLECTION':
self.__compute(omatrix, *obj.instance_collection.all_objects)
elif obj.bound_box:
for p in obj.bound_box:
world_p = omatrix @ mathutils.Vector(p)
self.__xyz[0] = min(world_p.x, self.__xyz[0])
self.__xyz[1] = max(world_p.x, self.__xyz[1])
self.__xyz[2] = min(world_p.y, self.__xyz[2])
self.__xyz[3] = max(world_p.y, self.__xyz[3])
self.__xyz[4] = min(world_p.z, self.__xyz[4])
self.__xyz[5] = max(world_p.z, self.__xyz[5])
self.__compute(pmatrix, *obj.children)
@property
def center(self):
return self.__center
@property
def box_center(self):
return mathutils.Vector((self.max_x + self.min_x, self.max_y + self.min_y, self.max_z + self.min_z)) / 2
@property
def min_x(self):
return self.__xyz[0]
@property
def max_x(self):
return self.__xyz[1]
@property
def min_y(self):
return self.__xyz[2]
@property
def max_y(self):
return self.__xyz[3]
@property
def min_z(self):
return self.__xyz[4]
@property
def max_z(self):
return self.__xyz[5]
@property
def width(self):
return abs(self.__xyz[0] - self.__xyz[1])
@property
def length(self):
return abs(self.__xyz[2] - self.__xyz[3])
@property
def height(self):
return abs(self.__xyz[4] - self.__xyz[5])
class WheelBoundingBox(BoundingBox):
def __init__(self, armature, bone_name, side):
super().__init__(armature, bone_name)
self.side = side
def compute_outer_x(self, delta=0):
if self.side == 'L':
return self.max_x + delta
else:
return self.min_x - delta
class WheelsDimension(object):
def __init__(self, armature, position, side_position, default):
self.default = default
self.position = position
self.side_position = side_position
self.wheels = []
wheel_bones = (armature.data.edit_bones.get(name) for name in name_range('DEF-Wheel.%s.%s' % (self.position, self.side_position)))
for wheel_bone in wheel_bones:
if wheel_bone is None:
break
self.wheels.append(WheelBoundingBox(armature, wheel_bone.name, side_position))
def name_suffixes(self):
for i in range(len(self.wheels)):
yield NameSuffix(self.position, self.side_position, i)
def names(self, base_name=None):
for name_suffix in name_range('%s.%s' % (self.position, self.side_position), self.nb):
yield '%s.%s' % (base_name, name_suffix) if base_name else name_suffix
def name(self, base_name=None):
suffix = '%s.%s' % (self.position, self.side_position)
return '%s.%s' % (base_name, suffix) if base_name else suffix
@property
def nb(self):
return len(self.wheels)
@property
def min_position(self):
if self.nb == 0:
return self.default
return min(self.wheels, key=lambda w: w.center.y).center
@property
def max_position(self):
if self.nb == 0:
return self.default
return max(self.wheels, key=lambda w: w.center.y).center
@property
def medium_position(self):
if self.nb == 0:
return self.min_position
return (self.min_position + self.max_position) / 2.0
def compute_outer_x(self, delta=0):
if self.side_position == 'L':
x = max(map(lambda w: w.max_x, self.wheels))
x += delta
else:
x = min(map(lambda w: w.min_x, self.wheels))
x -= delta
return x
@property
def outer_z(self):
return max(map(lambda w: w.max_z, self.wheels))
@property
def outer_front(self):
return min(map(lambda w: w.min_y, self.wheels))
@property
def outer_back(self):
return max(map(lambda w: w.max_y, self.wheels))
class CarDimension(object):
def __init__(self, armature):
body = armature.data.edit_bones['DEF-Body']
self.bb_body = BoundingBox(armature, 'DEF-Body')
self.wheels_front_left = WheelsDimension(armature, 'Ft', 'L', default=body.head)
self.wheels_front_right = WheelsDimension(armature, 'Ft', 'R', default=body.head)
self.wheels_back_left = WheelsDimension(armature, 'Bk', 'L', default=body.tail)
self.wheels_back_right = WheelsDimension(armature, 'Bk', 'R', default=body.tail)
@property
def body_center(self):
return self.bb_body.center
@property
def car_center(self):
center = self.bb_body.box_center.copy()
center.y = (self.max_y + self.min_y) / 2
return center
@property
def width(self):
return max([self.bb_body.width] + [abs(w.compute_outer_x() - self.bb_body.center.x) * 2 for w in self.wheels_dimensions])
@property
def height(self):
return max([self.bb_body.max_z] + [w.outer_z for w in self.wheels_dimensions])
@property
def length(self):
return abs(self.max_y - self.min_y)
@property
def min_y(self):
return min([self.bb_body.min_y] + [w.outer_front for w in self.wheels_dimensions])
@property
def max_y(self):
return max([self.bb_body.max_y] + [w.outer_back for w in self.wheels_dimensions])
@property
def wheels_front_position(self):
position = (self.wheels_front_left.min_position + self.wheels_front_right.min_position) / 2
position.x = self.bb_body.center.x
return position
@property
def wheels_back_position(self):
position = (self.wheels_back_left.max_position + self.wheels_back_right.max_position) / 2
position.x = self.bb_body.center.x
return position
@property
def suspension_front_position(self):
position = (self.wheels_front_left.medium_position + self.wheels_front_right.medium_position) / 2
position.x = self.bb_body.center.x
return position
@property
def suspension_back_position(self):
position = (self.wheels_back_left.medium_position + self.wheels_back_right.medium_position) / 2
position.x = self.bb_body.center.x
return position
@property
def has_wheels(self):
return self.has_front_wheels or self.has_back_wheels
@property
def has_front_wheels(self):
return self.nb_front_wheels > 0
@property
def has_back_wheels(self):
return self.nb_back_wheels > 0
@property
def nb_front_wheels(self):
return max(self.wheels_front_left.nb, self.wheels_front_right.nb)
@property
def nb_back_wheels(self):
return max(self.wheels_back_left.nb, self.wheels_back_right.nb)
@property
def wheels_dimensions(self):
return filter(lambda w: w.nb, (self.wheels_front_left, self.wheels_front_right, self.wheels_back_left, self.wheels_back_right))
def create_wheel_brake_bone(wheel_brake, parent_bone, wheel_bone):
wheel_brake.use_deform = False
wheel_brake.parent = parent_bone
wheel_brake.head = wheel_bone.head
wheel_brake.tail = wheel_bone.tail
def generate_constraint_on_wheel_brake_bone(wheel_brake_pose_bone, wheel_pose_bone):
wheel_brake_pose_bone.lock_location = (True, True, True)
wheel_brake_pose_bone.lock_rotation = (True, True, True)
wheel_brake_pose_bone.lock_rotation_w = True
wheel_brake_pose_bone.lock_scale = (True, False, False)
wheel_brake_pose_bone.custom_shape = get_widget('WGT-CarRig.WheelBrake')
wheel_brake_pose_bone.bone.show_wire = True
wheel_brake_pose_bone.bone_group = wheel_pose_bone.bone_group
wheel_brake_pose_bone.bone.layers = wheel_pose_bone.bone.layers
cns = wheel_brake_pose_bone.constraints.new('LIMIT_SCALE')
cns.name = 'Brakes'
cns.use_transform_limit = True
cns.owner_space = 'LOCAL'
cns.use_max_x = True
cns.use_min_x = True
cns.min_x = 1.0
cns.max_x = 1.0
cns.use_max_y = True
cns.use_min_y = True
cns.min_y = .5
cns.max_y = 1.0
cns.use_max_z = True
cns.use_min_z = True
cns.min_z = .5
cns.max_z = 1.0
class ArmatureGenerator(object):
def __init__(self, ob):
self.ob = ob
def generate(self, scene, adjust_origin):
define_custom_property(self.ob,
name='wheels_on_y_axis',
value=False,
description="Activate wheels rotation when moving the root bone along the Y axis")
define_custom_property(self.ob,
name='suspension_factor',
value=.5,
description="Influence of the dampers over the pitch of the body")
define_custom_property(self.ob,
name='suspension_rolling_factor',
value=.5,
description="Influence of the dampers over the roll of the body")
location = self.ob.location.copy()
self.ob.location = (0, 0, 0)
try:
bpy.ops.object.mode_set(mode='EDIT')
self.dimension = CarDimension(self.ob)
self.generate_animation_rig()
self.ob.data['Car Rig'] = True
deselect_edit_bones(self.ob)
if adjust_origin:
bpy.ops.object.mode_set(mode='OBJECT')
self.set_origin(scene)
bpy.ops.object.mode_set(mode='POSE')
self.generate_constraints_on_rig()
self.ob.display_type = 'WIRE'
self.generate_bone_groups()
dispatch_bones_to_armature_layers(self.ob)
finally:
self.ob.location += location
def generate_animation_rig(self):
amt = self.ob.data
body = amt.edit_bones['DEF-Body']
root = amt.edit_bones.new('Root')
if self.dimension.has_back_wheels:
root.head = self.dimension.wheels_back_position
elif self.dimension.has_front_wheels:
root.head = self.dimension.wheels_front_position
else:
root.head = self.dimension.body_center
root.head.z = 0
root.tail = root.head
root.tail.y += max(self.dimension.length / 1.95, self.dimension.width * 1.1)
root.use_deform = False
shape_root = amt.edit_bones.new('SHP-Root')
shape_root.head = self.dimension.car_center
shape_root.head.z = 0.01
shape_root.tail = shape_root.head
shape_root.tail.y += root.length
shape_root.use_deform = False
shape_root.parent = root
drift = amt.edit_bones.new('Drift')
drift.head = self.dimension.wheels_front_position
drift.head.z = self.dimension.wheels_back_position.z
drift.tail = drift.head
drift.tail.y -= self.dimension.width * .95
drift.roll = math.pi
drift.use_deform = False
drift.parent = root
base_bone_parent = drift
if self.dimension.has_front_wheels:
groundsensor_axle_front = amt.edit_bones.new('GroundSensor.Axle.Ft')
groundsensor_axle_front.head = self.dimension.wheels_front_position
groundsensor_axle_front.tail = groundsensor_axle_front.head
groundsensor_axle_front.tail.y += self.dimension.length / 16
groundsensor_axle_front.parent = root
shp_groundsensor_axle_front = amt.edit_bones.new('SHP-GroundSensor.Axle.Ft')
shp_groundsensor_axle_front.head = groundsensor_axle_front.head
shp_groundsensor_axle_front.tail = groundsensor_axle_front.tail
shp_groundsensor_axle_front.head.z = shp_groundsensor_axle_front.tail.z = 0.001
shp_groundsensor_axle_front.parent = groundsensor_axle_front
mch_root_axle_front = amt.edit_bones.new('MCH-Root.Axle.Ft')
mch_root_axle_front.head = self.dimension.wheels_front_position
mch_root_axle_front.head.z = 0.001
mch_root_axle_front.tail = mch_root_axle_front.head
mch_root_axle_front.tail.y += self.dimension.length / 6
mch_root_axle_front.parent = groundsensor_axle_front
if not self.dimension.has_back_wheels:
drift.parent = mch_root_axle_front
if self.dimension.has_back_wheels:
groundsensor_axle_back = amt.edit_bones.new('GroundSensor.Axle.Bk')
groundsensor_axle_back.head = self.dimension.wheels_back_position
groundsensor_axle_back.tail = groundsensor_axle_back.head
groundsensor_axle_back.tail.y += self.dimension.length / 16
groundsensor_axle_back.parent = drift
shp_groundsensor_axle_back = amt.edit_bones.new('SHP-GroundSensor.Axle.Bk')
shp_groundsensor_axle_back.head = groundsensor_axle_back.head
shp_groundsensor_axle_back.tail = groundsensor_axle_back.tail
shp_groundsensor_axle_back.head.z = shp_groundsensor_axle_back.tail.z = 0.001
shp_groundsensor_axle_back.parent = groundsensor_axle_back
mch_root_axle_back = amt.edit_bones.new('MCH-Root.Axle.Bk')
mch_root_axle_back.head = self.dimension.wheels_back_position
mch_root_axle_back.head.z = 0
mch_root_axle_back.tail = mch_root_axle_back.head
mch_root_axle_back.tail.y += self.dimension.length / 6
mch_root_axle_back.parent = groundsensor_axle_back
base_bone_parent = mch_root_axle_back
shape_drift = amt.edit_bones.new('SHP-Drift')
shape_drift.head = self.dimension.body_center
shape_drift.head.y = self.dimension.max_y + drift.length * .2
shape_drift.head.z = self.dimension.wheels_back_position.z
shape_drift.tail = shape_drift.head
shape_drift.tail.y += drift.length
shape_drift.use_deform = False
shape_drift.parent = base_bone_parent
for wheel_dimension in self.dimension.wheels_dimensions:
for name_suffix, wheel_bounding_box in zip(wheel_dimension.name_suffixes(), wheel_dimension.wheels):
self.generate_animation_wheel_bones(name_suffix, wheel_bounding_box, base_bone_parent)
self.generate_wheel_damper(wheel_dimension, base_bone_parent)
if self.dimension.has_front_wheels:
wheel_ft_r = amt.edit_bones.get('DEF-Wheel.Ft.R')
wheelFtL = amt.edit_bones.get('DEF-Wheel.Ft.L')
axis_ft = amt.edit_bones.new('MCH-Axis.Ft')
axis_ft.head = wheel_ft_r.head
axis_ft.tail = wheelFtL.head
axis_ft.use_deform = False
axis_ft.parent = base_bone_parent
mch_steering = amt.edit_bones.new('MCH-Steering')
mch_steering.head = self.dimension.wheels_front_position
mch_steering.tail = self.dimension.wheels_front_position
mch_steering.tail.y += self.dimension.width / 2
mch_steering.use_deform = False
mch_steering.parent = groundsensor_axle_front if groundsensor_axle_front else root
steering_rotation = amt.edit_bones.new('MCH-Steering.rotation')
steering_rotation.head = mch_steering.head
steering_rotation.tail = mch_steering.tail
steering_rotation.tail.y += 1
steering_rotation.use_deform = False
steering = amt.edit_bones.new('Steering')
steering.head = steering_rotation.head
steering.head.y = self.dimension.min_y - 4 * wheelFtL.length
steering.tail = steering.head
steering.tail.y -= self.dimension.width / 2
steering.use_deform = False
steering.parent = steering_rotation
if self.dimension.has_back_wheels:
wheel_bk_r = amt.edit_bones.get('DEF-Wheel.Bk.R')
wheel_bk_l = amt.edit_bones.get('DEF-Wheel.Bk.L')
axisBk = amt.edit_bones.new('MCH-Axis.Bk')
axisBk.head = wheel_bk_r.head
axisBk.tail = wheel_bk_l.head
axisBk.use_deform = False
axisBk.parent = base_bone_parent
suspension_bk = amt.edit_bones.new('MCH-Suspension.Bk')
suspension_bk.head = self.dimension.suspension_back_position
suspension_bk.tail = self.dimension.suspension_back_position
suspension_bk.tail.y += 2
suspension_bk.use_deform = False
suspension_bk.parent = base_bone_parent
suspension_ft = amt.edit_bones.new('MCH-Suspension.Ft')
suspension_ft.head = self.dimension.suspension_front_position
align_vector = suspension_bk.head - suspension_ft.head
align_vector.magnitude = 2
suspension_ft.tail = self.dimension.suspension_front_position + align_vector
suspension_ft.use_deform = False
suspension_ft.parent = base_bone_parent
axis = amt.edit_bones.new('MCH-Axis')
axis.head = suspension_ft.head
axis.tail = suspension_bk.head
axis.use_deform = False
axis.parent = suspension_ft
mch_body = amt.edit_bones.new('MCH-Body')
mch_body.head = body.head
mch_body.tail = body.tail
mch_body.tail.y += 1
mch_body.use_deform = False
mch_body.parent = axis
suspension = amt.edit_bones.new('Suspension')
suspension.head = self.dimension.body_center
suspension.head.z = self.dimension.height + self.dimension.width * .25
suspension.tail = suspension.head
suspension.tail.y += root.length * .5
suspension.use_deform = False
suspension.parent = axis
def generate_animation_wheel_bones(self, name_suffix, wheel_bounding_box, parent_bone):
amt = self.ob.data
def_wheel_bone = amt.edit_bones.get(name_suffix.name('DEF-Wheel'))
if def_wheel_bone is None:
return
ground_sensor = amt.edit_bones.new(name_suffix.name('GroundSensor'))
ground_sensor.head = wheel_bounding_box.box_center
ground_sensor.head.z = def_wheel_bone.head.z
ground_sensor.tail = ground_sensor.head
ground_sensor.tail.y += max(max(wheel_bounding_box.height, ground_sensor.head.z) / 2.5, wheel_bounding_box.width * 1.02)
ground_sensor.use_deform = False
ground_sensor.parent = parent_bone
shp_ground_sensor = amt.edit_bones.new(name_suffix.name('SHP-GroundSensor'))
shp_ground_sensor.head = ground_sensor.head
shp_ground_sensor.tail = ground_sensor.tail
shp_ground_sensor.head.z = shp_ground_sensor.tail.z = .001
shp_ground_sensor.use_deform = False
shp_ground_sensor.parent = ground_sensor
mch_wheel = amt.edit_bones.new(name_suffix.name('MCH-Wheel'))
mch_wheel.head = def_wheel_bone.head
mch_wheel.tail = def_wheel_bone.tail
mch_wheel.tail.y += .5
mch_wheel.use_deform = False
mch_wheel.parent = ground_sensor
define_custom_property(self.ob,
name=name_suffix.name('Wheel.rotation'),
value=.0,
description="Animation property for wheel spinning")
mch_wheel_rotation = amt.edit_bones.new(name_suffix.name('MCH-Wheel.rotation'))
mch_wheel_rotation.head = def_wheel_bone.head
mch_wheel_rotation.tail = def_wheel_bone.head
mch_wheel_rotation.tail.y += mch_wheel_rotation.tail.z
mch_wheel_rotation.use_deform = False
def_wheel_brake_bone = amt.edit_bones.get(name_suffix.name('DEF-WheelBrake'))
if def_wheel_brake_bone is not None:
mch_wheel = amt.edit_bones.new(name_suffix.name('MCH-WheelBrake'))
mch_wheel.head = def_wheel_brake_bone.head
mch_wheel.tail = def_wheel_brake_bone.tail
mch_wheel.tail.y += .5
mch_wheel.use_deform = False
mch_wheel.parent = ground_sensor
wheel = amt.edit_bones.new(name_suffix.name('Wheel'))
wheel.use_deform = False
wheel.parent = ground_sensor
wheel.head = def_wheel_bone.head
wheel.head.x = wheel_bounding_box.compute_outer_x(wheel_bounding_box.length * .05)
wheel.tail = wheel.head
wheel.tail.y += wheel.tail.z * .9
if name_suffix.is_left and name_suffix.is_first:
wheel_brake = amt.edit_bones.new(name_suffix.name('WheelBrake'))
create_wheel_brake_bone(wheel_brake, mch_wheel, wheel)
def generate_wheel_damper(self, wheel_dimension, parent_bone):
amt = self.ob.data
if wheel_dimension.nb == 1:
wheel_damper_parent = amt.edit_bones[wheel_dimension.name('GroundSensor')]
else:
wheel_damper_parent = amt.edit_bones.new(wheel_dimension.name('MCH-GroundSensor'))
wheel_damper_parent.head = wheel_dimension.medium_position
wheel_damper_parent.tail = wheel_dimension.medium_position
wheel_damper_parent.tail.y += 1.0
wheel_damper_parent.head.z = 0
wheel_damper_parent.tail.z = 0
wheel_damper_parent.use_deform = False
wheel_damper_parent.parent = parent_bone
wheel_damper = amt.edit_bones.new(wheel_dimension.name('WheelDamper'))
wheel_damper.head = wheel_dimension.medium_position
wheel_damper_scale_ratio = abs(wheel_damper.head.z)
wheel_damper.head.x = wheel_dimension.compute_outer_x(wheel_damper_scale_ratio * .25)
wheel_damper.head.z *= 1.5
wheel_damper.tail = wheel_damper.head
wheel_damper.tail.y += wheel_damper_scale_ratio
wheel_damper.use_deform = False
wheel_damper.parent = wheel_damper_parent
mch_wheel_damper = amt.edit_bones.new(wheel_dimension.name('MCH-WheelDamper'))
mch_wheel_damper.head = wheel_dimension.medium_position
mch_wheel_damper.tail = wheel_dimension.medium_position
mch_wheel_damper.tail.y += 2
mch_wheel_damper.use_deform = False
mch_wheel_damper.parent = wheel_damper
def generate_constraints_on_rig(self):
pose = self.ob.pose
amt = self.ob.data
for b in pose.bones:
if b.name.startswith('DEF-') or b.name.startswith('MCH-') or b.name.startswith('SHP-'):
b.lock_location = (True, True, True)
b.lock_rotation = (True, True, True)
b.lock_scale = (True, True, True)
b.lock_rotation_w = True
for wheel_dimension in self.dimension.wheels_dimensions:
for name_suffix in wheel_dimension.name_suffixes():
self.generate_constraints_on_wheel_bones(name_suffix)
self.generate_constraints_on_wheel_damper(wheel_dimension)
self.generate_constraints_on_axle_bones('Ft')
self.generate_constraints_on_axle_bones('Bk')
mch_axis = pose.bones.get('MCH-Axis')
if mch_axis is not None:
for axis_pos, influence in (('Ft', 1), ('Bk', .5)):
subtarget = 'MCH-Axis.%s' % axis_pos
if subtarget in pose.bones:
cns = mch_axis.constraints.new('TRANSFORM')
cns.name = 'Rotation from %s' % subtarget
cns.target = self.ob
cns.subtarget = subtarget
cns.map_from = 'ROTATION'
cns.from_min_x_rot = math.radians(-180)
cns.from_max_x_rot = math.radians(180)
cns.map_to_y_from = 'X'
cns.map_to = 'ROTATION'
cns.to_min_y_rot = math.radians(180)
cns.to_max_y_rot = math.radians(-180)
cns.owner_space = 'LOCAL'
cns.target_space = 'LOCAL'
create_constraint_influence_driver(self.ob, cns, '["suspension_rolling_factor"]', base_influence=influence)
root = pose.bones['Root']
root.lock_scale = (True, True, True)
root.custom_shape = get_widget('WGT-CarRig.Root')
root.custom_shape_transform = pose.bones['SHP-Root']
root.bone.show_wire = True
for ground_sensor_axle_name in ('GroundSensor.Axle.Ft', 'GroundSensor.Axle.Bk'):
groundsensor_axle = pose.bones.get(ground_sensor_axle_name)
if groundsensor_axle:
groundsensor_axle.lock_location = (True, True, False)
groundsensor_axle.lock_rotation = (True, True, True)
groundsensor_axle.lock_scale = (True, True, True)
groundsensor_axle.custom_shape = get_widget('WGT-CarRig.GroundSensor.Axle')
groundsensor_axle.lock_rotation_w = True
groundsensor_axle.custom_shape_transform = pose.bones['SHP-%s' % groundsensor_axle.name]
groundsensor_axle.bone.show_wire = True
self.generate_ground_projection_constraint(groundsensor_axle)
if groundsensor_axle.name == 'GroundSensor.Axle.Ft' and 'GroundSensor.Axle.Bk' in pose.bones:
cns = groundsensor_axle.constraints.new('LIMIT_DISTANCE')
cns.name = 'Limit distance from Root'
cns.limit_mode = 'LIMITDIST_ONSURFACE'
cns.target = self.ob
cns.subtarget = 'GroundSensor.Axle.Bk'
cns.use_transform_limit = True
cns.owner_space = 'POSE'
cns.target_space = 'POSE'
mch_root_axle_front = pose.bones.get('MCH-Root.Axle.Ft')
mch_root_axle_back = pose.bones.get('MCH-Root.Axle.Bk')
if mch_root_axle_front and mch_root_axle_back:
cns = mch_root_axle_back.constraints.new('DAMPED_TRACK')
cns.name = 'Track front axle'
cns.target = self.ob
cns.subtarget = mch_root_axle_front.name
cns.track_axis = 'TRACK_NEGATIVE_Y'
drift = pose.bones['Drift']
drift.lock_location = (True, True, True)
drift.lock_rotation = (True, True, False)
drift.lock_scale = (True, True, True)
drift.rotation_mode = 'ZYX'
drift.custom_shape = get_widget('WGT-CarRig.DriftHandle')
drift.custom_shape_transform = pose.bones['SHP-Drift']
drift.bone.show_wire = True
suspension = pose.bones['Suspension']
suspension.lock_rotation = (True, True, True)
suspension.lock_scale = (True, True, True)
suspension.lock_rotation_w = True
suspension.custom_shape = get_widget('WGT-CarRig.Suspension')
suspension.bone.show_wire = True
steering = pose.bones.get('Steering')
if steering is not None:
steering.lock_location = (False, True, True)
steering.lock_rotation = (True, True, True)
steering.lock_scale = (True, True, True)
steering.lock_rotation_w = True
steering.custom_shape = get_widget('WGT-CarRig.Steering')
steering.bone.show_wire = True
mch_steering_rotation = pose.bones['MCH-Steering.rotation']
mch_steering_rotation.rotation_mode = 'QUATERNION'
define_custom_property(self.ob,
name='Steering.rotation',
value=.0,
description="Animation property for steering")
create_translation_x_driver(self.ob, mch_steering_rotation, '["Steering.rotation"]')
if mch_root_axle_back:
cns = mch_steering_rotation.constraints.new('COPY_ROTATION')
cns.name = 'Copy back axle rotation'
cns.target = self.ob
cns.subtarget = mch_root_axle_back.name
cns.use_x = True
cns.use_y = False
cns.use_z = False
cns.owner_space = 'LOCAL'
cns.target_space = 'LOCAL'
self.generate_childof_constraint(mch_steering_rotation, mch_root_axle_front if mch_root_axle_front else root)
mch_steering = pose.bones['MCH-Steering']
cns = mch_steering.constraints.new('DAMPED_TRACK')
cns.name = 'Track steering bone'
cns.target = self.ob
cns.subtarget = 'Steering'
cns.track_axis = 'TRACK_NEGATIVE_Y'
cns = mch_steering.constraints.new('COPY_ROTATION')
cns.name = 'Drift counter animation'
cns.target = self.ob
cns.subtarget = 'Drift'
cns.use_x = False
cns.use_y = False
cns.use_z = True
cns.use_offset = True
cns.owner_space = 'LOCAL'
cns.target_space = 'LOCAL'
mch_body = self.ob.pose.bones['MCH-Body']
cns = mch_body.constraints.new('TRANSFORM')
cns.name = 'Suspension on rollover'
cns.target = self.ob
cns.subtarget = 'Suspension'
cns.map_from = 'LOCATION'
cns.from_min_x = -2
cns.from_max_x = 2
cns.from_min_y = -2
cns.from_max_y = 2
cns.map_to_x_from = 'Y'
cns.map_to_y_from = 'X'
cns.map_to = 'ROTATION'
cns.to_min_x_rot = math.radians(6)
cns.to_max_x_rot = math.radians(-6)
cns.to_min_y_rot = math.radians(-7)
cns.to_max_y_rot = math.radians(7)
cns.owner_space = 'LOCAL'
cns.target_space = 'LOCAL'
cns = mch_body.constraints.new('TRANSFORM')
cns.name = 'Suspension on vertical'
cns.target = self.ob
cns.subtarget = 'Suspension'
cns.map_from = 'LOCATION'
cns.from_min_z = -0.5
cns.from_max_z = 0.5
cns.map_to_z_from = 'Z'
cns.map_to = 'LOCATION'
cns.to_min_z = -0.1
cns.to_max_z = 0.1
cns.owner_space = 'LOCAL'
cns.target_space = 'LOCAL'
body = self.ob.pose.bones['DEF-Body']
cns = body.constraints.new('COPY_TRANSFORMS')
cns.target = self.ob
cns.subtarget = 'MCH-Body'
def generate_ground_projection_constraint(self, bone):
cns = bone.constraints.new('SHRINKWRAP')
cns.name = 'Ground projection'
cns.shrinkwrap_type = 'NEAREST_SURFACE'
cns.project_axis_space = 'LOCAL'
cns.project_axis = 'NEG_Z'
cns.distance = abs(bone.head.z)
def generate_childof_constraint(self, child, parent):
cns = child.constraints.new('CHILD_OF')
cns.target = self.ob
cns.subtarget = parent.name
cns.inverse_matrix = self.ob.data.bones[parent.name].matrix_local.inverted()
cns.use_location_x = True
cns.use_location_y = True
cns.use_location_z = True
cns.use_rotation_x = True
cns.use_rotation_y = True
cns.use_rotation_z = True
return cns
def generate_constraints_on_axle_bones(self, position):
pose = self.ob.pose
subtarget = 'MCH-Axis.%s' % position
if subtarget in pose.bones:
mch_suspension = pose.bones['MCH-Suspension.%s' % position]
cns = mch_suspension.constraints.new('COPY_LOCATION')
cns.name = 'Location from %s' % subtarget
cns.target = self.ob
cns.subtarget = subtarget
cns.head_tail = .5
cns.use_x = False
cns.use_y = False
cns.use_z = True
cns.owner_space = 'WORLD'
cns.target_space = 'WORLD'
create_constraint_influence_driver(self.ob, cns, '["suspension_factor"]')
if position == 'Ft':
cns = mch_suspension.constraints.new('DAMPED_TRACK')
cns.name = 'Track suspension back'
cns.target = self.ob
cns.subtarget = 'MCH-Suspension.Bk'
cns.track_axis = 'TRACK_Y'
mch_axis = pose.bones.get('MCH-Axis.%s' % position)
if mch_axis is not None:
cns = mch_axis.constraints.new('COPY_LOCATION')
cns.name = 'Copy location from right wheel'
cns.target = self.ob
cns.subtarget = 'MCH-WheelDamper.%s.R' % position
cns.use_x = True
cns.use_y = True
cns.use_z = True
cns.owner_space = 'WORLD'
cns.target_space = 'WORLD'
mch_axis = pose.bones['MCH-Axis.%s' % position]
cns = mch_axis.constraints.new('DAMPED_TRACK')
cns.name = 'Track Left Wheel'