Rigify: move drivers together with constraints and code improvement.

This commit is contained in:
Alexander Gavrilov 2021-01-04 11:48:24 +03:00
parent a6ee5b0e6f
commit 2660631657
5 changed files with 59 additions and 19 deletions

View File

@ -24,7 +24,6 @@ from ...base_rig import BaseRig
from ...utils.naming import make_derived_name
from ...utils.bones import set_bone_widget_transform
from ...utils.mechanism import copy_custom_properties_with_ui
from ...utils.widgets import layout_widget_dropdown, create_registered_widget
from ...utils.widgets_basic import create_pivot_widget
from ...utils.switch_parent import SwitchParentBuilder
@ -119,9 +118,7 @@ class Rig(BaseRig):
ctrl = self.bones.ctrl
main_ctl = ctrl.master if self.make_control else ctrl.pivot
self.copy_bone_properties(org, main_ctl, props=False)
copy_custom_properties_with_ui(self, org, main_ctl)
self.copy_bone_properties(org, main_ctl, ui_controls=True)
def rig_bones(self):

View File

@ -21,7 +21,7 @@
import bpy
from ...utils.naming import strip_org, strip_prefix, choose_derived_bone, is_control_bone
from ...utils.mechanism import copy_custom_properties_with_ui
from ...utils.mechanism import copy_custom_properties_with_ui, move_all_constraints
from ...utils.widgets import layout_widget_dropdown, create_registered_widget
from ...base_rig import BaseRig
@ -66,13 +66,7 @@ class RelinkConstraintsMixin:
def relink_move_constraints(self, from_bone, to_bone, *, prefix=''):
if self.params.relink_constraints:
src = self.get_bone(from_bone).constraints
dest = self.get_bone(to_bone).constraints
for con in list(src):
if con.name.startswith(prefix):
dest.copy(con)
src.remove(con)
move_all_constraints(self.obj, from_bone, to_bone, prefix=prefix)
def relink_bone_parent(self, bone_name):

View File

@ -23,7 +23,6 @@ import bpy
from ...base_rig import BaseRig
from ...utils.naming import strip_org, make_deformer_name
from ...utils.mechanism import copy_custom_properties_with_ui
from ...utils.widgets import layout_widget_dropdown, create_registered_widget
from ...utils.widgets_basic import create_bone_widget
@ -78,9 +77,7 @@ class Rig(BaseRig, RelinkConstraintsMixin):
bones = self.bones
if self.make_control:
self.copy_bone_properties(bones.org, bones.ctrl, props=False)
copy_custom_properties_with_ui(self, bones.org, bones.ctrl)
self.copy_bone_properties(bones.org, bones.ctrl)
def rig_bones(self):

View File

@ -24,7 +24,7 @@ from mathutils import Vector, Matrix, Color
from rna_prop_ui import rna_idprop_ui_prop_get
from .errors import MetarigError
from .naming import get_name, make_derived_name
from .naming import get_name, make_derived_name, is_control_bone
from .misc import pairwise
#=======================
@ -391,9 +391,19 @@ class BoneUtilityMixin(object):
self.register_new_bone(name, bone_name)
return name
def copy_bone_properties(self, src_name, tgt_name, **kwargs):
def copy_bone_properties(self, src_name, tgt_name, *, props=True, ui_controls=None, **kwargs):
"""Copy pose-mode properties of the bone."""
copy_bone_properties(self.obj, src_name, tgt_name, **kwargs)
if props:
if ui_controls is None and is_control_bone(tgt_name) and hasattr(self, 'script'):
ui_controls = [tgt_name]
elif ui_controls is True:
ui_controls = self.bones.flatten('ctrl')
copy_bone_properties(self.obj, src_name, tgt_name, props=props and not ui_controls, **kwargs)
if props and ui_controls:
from .mechanism import copy_custom_properties_with_ui
copy_custom_properties_with_ui(self, src_name, tgt_name, ui_controls=ui_controls)
def rename_bone(self, old_name, new_name):
"""Rename the bone, returning the actual new name."""

View File

@ -337,6 +337,48 @@ def driver_var_transform(target, bone=None, *, type='LOC_X', space='WORLD', rota
return { 'type': 'TRANSFORMS', 'targets': [ target_map ] }
#=============================================
# Constraint management
#=============================================
def move_constraint(source, target, con):
"""
Move a constraint from one owner to another, together with drivers.
"""
assert source.constraints[con.name] == con
if isinstance(target, str):
target = con.id_data.pose.bones[target]
con_tgt = target.constraints.copy(con)
if target.id_data == con.id_data:
adt = con.id_data.animation_data
if adt:
prefix = con.path_from_id()
new_prefix = con_tgt.path_from_id()
for fcu in adt.drivers:
if fcu.data_path.startswith(prefix):
fcu.data_path = new_prefix + fcu.data_path[len(prefix):]
source.constraints.remove(con)
def move_all_constraints(obj, source, target, *, prefix=''):
"""
Move all constraints with the specified name prefix from one bone to another.
"""
if isinstance(source, str):
source = obj.pose.bones[source]
if isinstance(target, str):
target = obj.pose.bones[target]
for con in list(source.constraints):
if con.name.startswith(prefix):
move_constraint(source, target, con)
#=============================================
# Custom property management
#=============================================