11import bpy
2- from mathutils import Vector
32from ...utils import copy_bone , flip_bone
43from ...utils import strip_org , make_deformer_name , connected_children_names , make_mechanism_name
5- from ...utils import create_circle_widget , create_sphere_widget , create_widget
4+ from ...utils import create_circle_widget , create_widget
65from ...utils import MetarigError
76from rna_prop_ui import rna_idprop_ui_prop_get
87
@@ -27,126 +26,128 @@ def __init__(self, obj, bone_name, params):
2726 def generate (self ):
2827 org_bones = self .org_bones
2928
30- bpy .ops .object .mode_set (mode = 'EDIT' )
29+ bpy .ops .object .mode_set (mode = 'EDIT' )
3130 eb = self .obj .data .edit_bones
3231
3332 # Bone name lists
34- ctrl_chain = []
35- def_chain = []
36- mch_chain = []
33+ ctrl_chain = []
34+ def_chain = []
35+ mch_chain = []
3736 mch_drv_chain = []
3837
3938 # Create ctrl master bone
4039 org_name = self .org_bones [0 ]
4140 temp_name = strip_org (self .org_bones [0 ])
4241
43- suffix = temp_name [- 2 :]
44- master_name = temp_name [:- 5 ] + "_master" + suffix
45- master_name = copy_bone ( self .obj , org_name , master_name )
46- ctrl_bone_master = eb [ master_name ]
42+ if temp_name [- 2 :] == '.L' or temp_name [- 2 :] == '.R' :
43+ suffix = temp_name [- 2 :]
44+ master_name = temp_name [:- 2 ] + "_master" + suffix
45+ else :
46+ master_name = temp_name + "_master"
47+ master_name = copy_bone (self .obj , org_name , master_name )
48+ ctrl_bone_master = eb [master_name ]
4749
48- ## Parenting bug fix ??
50+ # Parenting bug fix ??
4951 ctrl_bone_master .use_connect = False
50- ctrl_bone_master .parent = None
52+ ctrl_bone_master .parent = None
5153
52- ctrl_bone_master .tail += ( eb [ org_bones [- 1 ] ].tail - eb [org_name ].head ) * 1.25
54+ ctrl_bone_master .tail += (eb [org_bones [- 1 ]].tail - eb [org_name ].head ) * 1.25
5355
5456 for bone in org_bones :
5557 eb [bone ].use_connect = False
56- if org_bones .index ( bone ) != 0 :
57- eb [bone ].parent = None
58+ if org_bones .index (bone ) != 0 :
59+ eb [bone ].parent = None
5860
5961 # Creating the bone chains
6062 for i in range (len (self .org_bones )):
6163
62- name = self .org_bones [i ]
64+ name = self .org_bones [i ]
6365 ctrl_name = strip_org (name )
6466
6567 # Create control bones
66- ctrl_bone = copy_bone ( self .obj , name , ctrl_name )
67- ctrl_bone_e = eb [ ctrl_name ]
68+ ctrl_bone = copy_bone (self .obj , name , ctrl_name )
69+ ctrl_bone_e = eb [ctrl_name ]
6870
6971 # Create deformation bones
70- def_name = make_deformer_name ( ctrl_name )
71- def_bone = copy_bone ( self .obj , name , def_name )
72+ def_name = make_deformer_name (ctrl_name )
73+ def_bone = copy_bone (self .obj , name , def_name )
7274
7375 # Create mechanism bones
74- mch_name = make_mechanism_name ( ctrl_name )
75- mch_bone = copy_bone ( self .obj , name , mch_name )
76+ mch_name = make_mechanism_name (ctrl_name )
77+ mch_bone = copy_bone (self .obj , name , mch_name )
7678
7779 # Create mechanism driver bones
78- drv_name = make_mechanism_name (ctrl_name ) + "_drv"
79- mch_bone_drv = copy_bone (self .obj , name , drv_name )
80- mch_bone_drv_e = eb [drv_name ]
80+ drv_name = make_mechanism_name (ctrl_name ) + "_drv"
81+ mch_bone_drv = copy_bone (self .obj , name , drv_name )
8182
8283 # Adding to lists
83- ctrl_chain += [ctrl_name ]
84- def_chain += [def_bone ]
85- mch_chain += [mch_bone ]
86- mch_drv_chain += [drv_name ]
84+ ctrl_chain += [ctrl_bone ]
85+ def_chain += [def_bone ]
86+ mch_chain += [mch_bone ]
87+ mch_drv_chain += [mch_bone_drv ]
8788
8889 # Restoring org chain parenting
8990 for bone in org_bones [1 :]:
90- eb [bone ].parent = eb [ org_bones [ org_bones .index (bone ) - 1 ] ]
91+ eb [bone ].parent = eb [org_bones [org_bones .index (bone ) - 1 ] ]
9192
9293 # Parenting the master bone to the first org
93- ctrl_bone_master = eb [ master_name ]
94- ctrl_bone_master .parent = eb [ org_bones [0 ] ]
94+ ctrl_bone_master = eb [master_name ]
95+ ctrl_bone_master .parent = eb [org_bones [0 ]]
9596
9697 # Parenting chain bones
9798 for i in range (len (self .org_bones )):
9899 # Edit bone references
99- def_bone_e = eb [def_chain [i ]]
100- ctrl_bone_e = eb [ctrl_chain [i ]]
101- mch_bone_e = eb [mch_chain [i ]]
100+ def_bone_e = eb [def_chain [i ]]
101+ ctrl_bone_e = eb [ctrl_chain [i ]]
102+ mch_bone_e = eb [mch_chain [i ]]
102103 mch_bone_drv_e = eb [mch_drv_chain [i ]]
103104
104105 if i == 0 :
105106 # First ctl bone
106- ctrl_bone_e .parent = mch_bone_drv_e
107+ ctrl_bone_e .parent = mch_bone_drv_e
107108 ctrl_bone_e .use_connect = False
108109 # First def bone
109- def_bone_e .parent = eb [self .org_bones [i ]].parent
110- def_bone_e .use_connect = False
110+ def_bone_e .parent = eb [self .org_bones [i ]].parent
111+ def_bone_e .use_connect = False
111112 # First mch bone
112113 mch_bone_e .parent = eb [self .org_bones [i ]].parent
113- mch_bone_e .use_connect = False
114+ mch_bone_e .use_connect = False
114115 # First mch driver bone
115116 mch_bone_drv_e .parent = eb [self .org_bones [i ]].parent
116- mch_bone_drv_e .use_connect = False
117+ mch_bone_drv_e .use_connect = False
117118 else :
118119 # The rest
119- ctrl_bone_e .parent = mch_bone_drv_e
120- ctrl_bone_e .use_connect = False
120+ ctrl_bone_e .parent = mch_bone_drv_e
121+ ctrl_bone_e .use_connect = False
121122
122- def_bone_e .parent = eb [def_chain [i - 1 ]]
123- def_bone_e .use_connect = True
123+ def_bone_e .parent = eb [def_chain [i - 1 ]]
124+ def_bone_e .use_connect = True
124125
125- mch_bone_drv_e .parent = eb [ctrl_chain [i - 1 ]]
126+ mch_bone_drv_e .parent = eb [ctrl_chain [i - 1 ]]
126127 mch_bone_drv_e .use_connect = False
127128
128129 # Parenting mch bone
129- mch_bone_e .parent = ctrl_bone_e
130+ mch_bone_e .parent = ctrl_bone_e
130131 mch_bone_e .use_connect = False
131132
132- # Creating tip conrtol bone
133- tip_name = copy_bone ( self .obj , org_bones [- 1 ], temp_name )
134- ctrl_bone_tip = eb [ tip_name ]
135- flip_bone ( self .obj , tip_name )
133+ # Creating tip control bone
134+ tip_name = copy_bone (self .obj , org_bones [- 1 ], temp_name )
135+ ctrl_bone_tip = eb [tip_name ]
136+ flip_bone (self .obj , tip_name )
136137 ctrl_bone_tip .length /= 2
137138
138139 ctrl_bone_tip .parent = eb [ctrl_chain [- 1 ]]
139140
140- bpy .ops .object .mode_set (mode = 'OBJECT' )
141+ bpy .ops .object .mode_set (mode = 'OBJECT' )
141142
142143 pb = self .obj .pose .bones
143144
144145 # Setting pose bones locks
145146 pb_master = pb [master_name ]
146- pb_master .lock_scale = True ,False ,True
147+ pb_master .lock_scale = True , False , True
147148
148- pb [tip_name ].lock_scale = True ,True ,True
149- pb [tip_name ].lock_rotation = True ,True ,True
149+ pb [tip_name ].lock_scale = True , True , True
150+ pb [tip_name ].lock_rotation = True , True , True
150151 pb [tip_name ].lock_rotation_w = True
151152
152153 pb_master ['finger_curve' ] = 0.0
@@ -160,103 +161,98 @@ def generate(self):
160161 # Pose settings
161162 for org , ctrl , deform , mch , mch_drv in zip (self .org_bones , ctrl_chain , def_chain , mch_chain , mch_drv_chain ):
162163
163- # Constraining the org bones
164- #con = pb[org].constraints.new('COPY_TRANSFORMS')
165- #con.target = self.obj
166- #con.subtarget = ctrl
167-
168164 # Constraining the deform bones
169- con = pb [deform ].constraints .new ('COPY_TRANSFORMS' )
170- con .target = self .obj
165+ con = pb [deform ].constraints .new ('COPY_TRANSFORMS' )
166+ con .target = self .obj
171167 con .subtarget = mch
172168
173169 # Constraining the mch bones
174170 if mch_chain .index (mch ) == 0 :
175- con = pb [mch ].constraints .new ('COPY_LOCATION' )
176- con .target = self .obj
171+ con = pb [mch ].constraints .new ('COPY_LOCATION' )
172+ con .target = self .obj
177173 con .subtarget = ctrl
178174
179- con = pb [mch ].constraints .new ('COPY_SCALE' )
180- con .target = self .obj
175+ con = pb [mch ].constraints .new ('COPY_SCALE' )
176+ con .target = self .obj
181177 con .subtarget = ctrl
182178
183- con = pb [mch ].constraints .new ('DAMPED_TRACK' )
184- con .target = self .obj
179+ con = pb [mch ].constraints .new ('DAMPED_TRACK' )
180+ con .target = self .obj
185181 con .subtarget = ctrl_chain [ctrl_chain .index (ctrl )+ 1 ]
186182
187- con = pb [mch ].constraints .new ('STRETCH_TO' )
188- con .target = self .obj
183+ con = pb [mch ].constraints .new ('STRETCH_TO' )
184+ con .target = self .obj
189185 con .subtarget = ctrl_chain [ctrl_chain .index (ctrl )+ 1 ]
190- con .volume = 'NO_VOLUME'
186+ con .volume = 'NO_VOLUME'
191187
192188 elif mch_chain .index (mch ) == len (mch_chain ) - 1 :
193- con = pb [mch ].constraints .new ('DAMPED_TRACK' )
194- con .target = self .obj
189+ con = pb [mch ].constraints .new ('DAMPED_TRACK' )
190+ con .target = self .obj
195191 con .subtarget = tip_name
196192
197- con = pb [mch ].constraints .new ('STRETCH_TO' )
198- con .target = self .obj
193+ con = pb [mch ].constraints .new ('STRETCH_TO' )
194+ con .target = self .obj
199195 con .subtarget = tip_name
200- con .volume = 'NO_VOLUME'
196+ con .volume = 'NO_VOLUME'
201197 else :
202- con = pb [mch ].constraints .new ('DAMPED_TRACK' )
203- con .target = self .obj
198+ con = pb [mch ].constraints .new ('DAMPED_TRACK' )
199+ con .target = self .obj
204200 con .subtarget = ctrl_chain [ctrl_chain .index (ctrl )+ 1 ]
205201
206- con = pb [mch ].constraints .new ('STRETCH_TO' )
207- con .target = self .obj
202+ con = pb [mch ].constraints .new ('STRETCH_TO' )
203+ con .target = self .obj
208204 con .subtarget = ctrl_chain [ctrl_chain .index (ctrl )+ 1 ]
209- con .volume = 'NO_VOLUME'
205+ con .volume = 'NO_VOLUME'
210206
211207 # Constraining and driving mch driver bones
212208 pb [mch_drv ].rotation_mode = 'YZX'
213209
214210 if mch_drv_chain .index (mch_drv ) == 0 :
215211 # Constraining to master bone
216- con = pb [mch_drv ].constraints .new ('COPY_LOCATION' )
217- con .target = self .obj
218- con .subtarget = master_name
212+ con = pb [mch_drv ].constraints .new ('COPY_LOCATION' )
213+ con .target = self .obj
214+ con .subtarget = master_name
219215
220- con = pb [mch_drv ].constraints .new ('COPY_ROTATION' )
221- con .target = self .obj
222- con .subtarget = master_name
216+ con = pb [mch_drv ].constraints .new ('COPY_ROTATION' )
217+ con .target = self .obj
218+ con .subtarget = master_name
223219 con .target_space = 'LOCAL'
224- con .owner_space = 'LOCAL'
220+ con .owner_space = 'LOCAL'
225221
226222 else :
227223 # Match axis to expression
228224 options = {
229- "X" : { "axis" : 0 ,
230- "expr" : '(1-sy)*pi' },
231- "-X" : { "axis" : 0 ,
232- "expr" : '-((1-sy)*pi)' },
233- "Y" : { "axis" : 1 ,
234- "expr" : '(1-sy)*pi' },
235- "-Y" : { "axis" : 1 ,
236- "expr" : '-((1-sy)*pi)' },
237- "Z" : { "axis" : 2 ,
238- "expr" : '(1-sy)*pi' },
239- "-Z" : { "axis" : 2 ,
240- "expr" : '-((1-sy)*pi)' }
225+ "X" : {"axis" : 0 ,
226+ "expr" : '(1-sy)*pi' },
227+ "-X" : {"axis" : 0 ,
228+ "expr" : '-((1-sy)*pi)' },
229+ "Y" : {"axis" : 1 ,
230+ "expr" : '(1-sy)*pi' },
231+ "-Y" : {"axis" : 1 ,
232+ "expr" : '-((1-sy)*pi)' },
233+ "Z" : {"axis" : 2 ,
234+ "expr" : '(1-sy)*pi' },
235+ "-Z" : {"axis" : 2 ,
236+ "expr" : '-((1-sy)*pi)' }
241237 }
242238
243239 axis = self .params .primary_rotation_axis
244240
245241 # Drivers
246- drv = pb [mch_drv ].driver_add ("rotation_euler" , options [axis ]["axis" ]).driver
247- drv .type = 'SCRIPTED'
248- drv .expression = options [axis ]["expr" ]
249- drv_var = drv .variables .new ()
250- drv_var .name = 'sy'
251- drv_var .type = "SINGLE_PROP"
252- drv_var .targets [0 ].id = self .obj
242+ drv = pb [mch_drv ].driver_add ("rotation_euler" , options [axis ]["axis" ]).driver
243+ drv .type = 'SCRIPTED'
244+ drv .expression = options [axis ]["expr" ]
245+ drv_var = drv .variables .new ()
246+ drv_var .name = 'sy'
247+ drv_var .type = "SINGLE_PROP"
248+ drv_var .targets [0 ].id = self .obj
253249 drv_var .targets [0 ].data_path = pb [master_name ].path_from_id () + '.scale.y'
254250
255- # Setting bone curvature setting, costum property, and drivers
251+ # Setting bone curvature setting, custom property, and drivers
256252 def_bone = self .obj .data .bones [deform ]
257253
258254 def_bone .bbone_segments = 8
259- drv = def_bone .driver_add ("bbone_in" ).driver # Ease in
255+ drv = def_bone .driver_add ("bbone_in" ).driver # Ease in
260256
261257 drv .type = 'SUM'
262258 drv_var = drv .variables .new ()
@@ -265,7 +261,7 @@ def generate(self):
265261 drv_var .targets [0 ].id = self .obj
266262 drv_var .targets [0 ].data_path = pb_master .path_from_id () + '["finger_curve"]'
267263
268- drv = def_bone .driver_add ("bbone_out" ).driver # Ease out
264+ drv = def_bone .driver_add ("bbone_out" ).driver # Ease out
269265
270266 drv .type = 'SUM'
271267 drv_var = drv .variables .new ()
@@ -274,7 +270,6 @@ def generate(self):
274270 drv_var .targets [0 ].id = self .obj
275271 drv_var .targets [0 ].data_path = pb_master .path_from_id () + '["finger_curve"]'
276272
277-
278273 # Assigning shapes to control bones
279274 create_circle_widget (self .obj , ctrl , radius = 0.3 , head_tail = 0.5 )
280275
0 commit comments