diff --git a/rigify/utils/mechanism.py b/rigify/utils/mechanism.py index 232fb7afc2c9c018b9d3537b106c1d5aecef6e8b..e3f9f1c224c2e8520f62ad0435e200cf0c9ec85c 100644 --- a/rigify/utils/mechanism.py +++ b/rigify/utils/mechanism.py @@ -39,9 +39,9 @@ def _set_default_attr(obj, options, attr, value): options.setdefault(attr, value) def make_constraint( - owner, type, target=None, subtarget=None, *, insert_index=None, + owner, con_type, target=None, subtarget=None, *, insert_index=None, space=None, track_axis=None, use_xyz=None, use_limit_xyz=None, invert_xyz=None, - **options): + targets=None, **options): """ Creates and initializes constraint of the specified type for the owner bone. @@ -55,11 +55,30 @@ def make_constraint( use_limit_xyz : list of 3 items is assigned to use_limit_x/y/z options invert_xyz : list of 3 items is assigned to invert_x, invert_y and invert_z options min/max_x/y/z : a corresponding use_(min/max/limit)_(x/y/z) option is set to True + targets : list of strings, tuples or dicts describing Armature constraint targets Other keyword arguments are directly assigned to the constraint options. Returns the newly created constraint. """ - con = owner.constraints.new(type) + con = owner.constraints.new(con_type) + + # For Armature constraints, allow passing a "targets" list as a keyword argument. + if targets is not None: + assert con.type == 'ARMATURE' + for target_info in targets: + con_target = con.targets.new() + con_target.target = owner.id_data + # List element can be a string, a tuple or a dictionary. + if isinstance(target_info, str): + con_target.subtarget = target_info + elif isinstance(target_info, tuple): + if len(target_info) == 2: + con_target.subtarget, con_target.weight = target_info + else: + con_target.target, con_target.subtarget, con_target.weight = target_info + else: + for key, val in target_info.items(): + setattr(con_target, key, val) if insert_index is not None: owner.constraints.move(len(owner.constraints)-1, insert_index) diff --git a/rigify/utils/switch_parent.py b/rigify/utils/switch_parent.py index 61721266e3122bd1a52dad8dd95dd1b659c5aece..1a0e81fe8e41b7e4ca077b82d9df474e870b83c6 100644 --- a/rigify/utils/switch_parent.py +++ b/rigify/utils/switch_parent.py @@ -385,19 +385,16 @@ class SwitchParentBuilder(GeneratorPlugin, MechanismUtilityMixin): # Implement via an Armature constraint mch = child['mch_bone'] - con = self.make_constraint(mch, 'ARMATURE', name='SWITCH_PARENT') + con = self.make_constraint( + mch, 'ARMATURE', name='SWITCH_PARENT', + targets=[ (parent, 0.0) for parent, _ in child['parent_bones'] ] + ) prop_var = [(child['prop_bone'], child['prop_id'])] for i, (parent, parent_name) in enumerate(child['parent_bones']): - tgt = con.targets.new() - - tgt.target = self.obj - tgt.subtarget = parent - tgt.weight = 0.0 - expr = 'var == %d' % (i+1) - self.make_driver(tgt, 'weight', expression=expr, variables=prop_var) + self.make_driver(con.targets[i], 'weight', expression=expr, variables=prop_var) # Add copy constraints copy = child['copy']