From c6296d88b2992494af4e8c3ce775d05bd452461a Mon Sep 17 00:00:00 2001 From: miquelcampos Date: Tue, 1 Oct 2024 11:00:09 +0900 Subject: [PATCH] dagmenu.py add support to different naming in blend attr. Before only _blend was supported #224 --- release/scripts/mgear/core/anim_utils.py | 25 +++-- release/scripts/mgear/core/dagmenu.py | 134 +++++++++++++++-------- 2 files changed, 103 insertions(+), 56 deletions(-) diff --git a/release/scripts/mgear/core/anim_utils.py b/release/scripts/mgear/core/anim_utils.py index 413789a9..71c5fdf4 100644 --- a/release/scripts/mgear/core/anim_utils.py +++ b/release/scripts/mgear/core/anim_utils.py @@ -325,9 +325,9 @@ def get_ik_fk_controls_by_role(uiHost, attr_ctl_cnx): role = c.ctl_role.get() if "fk" in role: fk_controls.append(c.stripNamespace()) - elif role == "upv": + elif role in ["upv", "leg_pv", "arm_pv"]: ik_controls["pole_vector"] = c.stripNamespace() - elif role == "ik": + elif role in ["ik", "wrist_ik", "ankle_ik"]: ik_controls["ik_control"] = c.stripNamespace() elif role == "ikRot": ik_controls["ik_rot"] = c.stripNamespace() @@ -889,6 +889,8 @@ def ikFkMatch_with_namespace( ik_rot=None, key=None, ik_controls=None, + ik_val=1.0, + fk_val=0.0, ): """Switch IK/FK with matching functionality @@ -902,8 +904,14 @@ def ikFkMatch_with_namespace( fks ([str]): List of fk controls names ik (str): Ik control name upv (str): Up vector control name - ikRot (None, str): optional. Name of the Ik Rotation control + ik_rot (None, optional): Ik Rotation control if exist key (None, bool): optional. Whether we do an snap with animation + ik_controls (None, optional): Ik controls + ik_val (float, optional): Value that will define IK active + fk_val (float, optional): Value that will define FK active + + Deleted Parameters: + ikRot (None, str): optional. Name of the Ik Rotation control """ # ----------------------------------------------- # NOTE: the following section is a workaround to match and reset the gimbal @@ -1032,19 +1040,18 @@ def _get_mth(name): ] # if is IK then snap FK - if val == 1.0: + if val == ik_val: for target, ctl in zip(fk_targets, fk_ctrls): transform.matchWorldTransform(target, ctl) - - o_attr.set(0.0) + pm.setAttr(o_attr, fk_val) # we match the foot FK after switch blend attr if foot_cnx: for i, c in enumerate(foot_fk): c.setMatrix(foot_FK_matrix[i], worldSpace=True) # if is FK then sanp IK - elif val == 0.0: + elif val == fk_val: transform.matchWorldTransform(ik_target, ik_ctrl) if ik_rot: transform.matchWorldTransform(ik_rot_target, ik_rot_node) @@ -1086,7 +1093,8 @@ def _get_mth(name): upv_ctrl.setTranslation(final_vector, space="world") # sets blend attribute new value - o_attr.set(1.0) + pm.setAttr(o_attr, ik_val) + # print(o_attr.get()) # handle the upvector roll roll_att_name = ikfk_attr.replace("blend", "roll") @@ -1125,6 +1133,7 @@ def _get_mth(name): if gimbal_exist: for x in fks_gimbal + [ik_gimbal]: pm.setKeyframe(x, time=(cmds.currentTime(query=True))) + cmds.dgdirty(a=True) def ikFkMatch(model, ikfk_attr, ui_host, fks, ik, upv, ik_rot=None, key=None): diff --git a/release/scripts/mgear/core/dagmenu.py b/release/scripts/mgear/core/dagmenu.py index 87f94cea..efb0a69e 100644 --- a/release/scripts/mgear/core/dagmenu.py +++ b/release/scripts/mgear/core/dagmenu.py @@ -189,6 +189,8 @@ def __range_switch_callback(*args): switch_control = args[0].split("|")[-1] blend_attr = args[1] + # blend attriute extension _blend, _switch or other + # blend_attr_ext = args[2] # the gets root node for the given control # this assumes the rig is root is a root node. @@ -202,7 +204,8 @@ def __range_switch_callback(*args): # ik_controls, fk_controls = _get_controls(switch_control, blend_attr) # search criteria to find all the components sharing the blend - criteria = blend_attr.replace("_blend", "") + "_id*_ctl_cnx" + # criteria = blend_attr.replace(blend_attr_ext, "") + "_id*_ctl_cnx" + criteria = "*_id*_ctl_cnx" component_ctl = ( cmds.listAttr(switch_control, ud=True, string=criteria) or [] ) @@ -291,39 +294,51 @@ def __switch_fkik_callback(*args): switch_control = args[0].split("|")[-1] keyframe = args[1] blend_attr = args[2] + # blend attr extension. _blend, _switch or other + blend_attr_ext = args[3] # gets namespace namespace = getNamespace(switch_control) # search criteria to find all the components sharing the blend - criteria = blend_attr.replace("_blend", "") + "_id*_ctl_cnx" + # criteria = blend_attr.replace(blend_attr_ext, "") + "_id*_ctl_cnx" + criteria = "*_id*_ctl_cnx" component_ctl = ( cmds.listAttr(switch_control, ud=True, string=criteria) or [] ) blend_fullname = "{}.{}".format(switch_control, blend_attr) + if blend_attr_ext == "_blend": + ik_val = 1.0 + fk_val = 0.0 + else: + ik_val = False + fk_val = True for i, comp_ctl_list in enumerate(component_ctl): - # we need to need to set the original blend value for each ik/fk match - if i == 0: - init_val = cmds.getAttr(blend_fullname) - else: - cmds.setAttr(blend_fullname, init_val) ik_controls, fk_controls = get_ik_fk_controls_by_role( switch_control, comp_ctl_list ) - - # runs switch - ikFkMatch_with_namespace( - namespace=namespace, - ikfk_attr=blend_attr, - ui_host=switch_control, - fks=fk_controls, - ik=ik_controls["ik_control"], - upv=ik_controls["pole_vector"], - ik_rot=ik_controls["ik_rot"], - key=keyframe, - ik_controls=ik_controls, - ) + init_val = None + if ik_controls["ik_control"] and fk_controls: + # we need to set the original blend value for each ik/fk match + if not init_val: + init_val = cmds.getAttr(blend_fullname) + else: + cmds.setAttr(blend_fullname, init_val) + # runs switch + ikFkMatch_with_namespace( + namespace=namespace, + ikfk_attr=blend_attr, + ui_host=switch_control, + fks=fk_controls, + ik=ik_controls["ik_control"], + upv=ik_controls["pole_vector"], + ik_rot=ik_controls["ik_rot"], + key=keyframe, + ik_controls=ik_controls, + ik_val=ik_val, + fk_val=fk_val, + ) def __switch_parent_callback(*args): @@ -769,7 +784,8 @@ def mgear_dagmenu_fill(parent_menu, current_control): child_controls.append(current_control) attrs = _get_switch_node_attrs(current_control, "_blend") attrs2 = _get_switch_node_attrs(current_control, "ref") - if attrs or attrs2: + attrs3 = _get_switch_node_attrs(current_control, "_switch") + if attrs or attrs2 or attrs3: ui_host = current_control else: @@ -779,7 +795,8 @@ def mgear_dagmenu_fill(parent_menu, current_control): )[0] attrs = _get_switch_node_attrs(ui_host, "_blend") attrs2 = _get_switch_node_attrs(ui_host, "ref") - if not attrs and not attrs2: + attrs3 = _get_switch_node_attrs(ui_host, "_switch") + if not attrs and not attrs2 and not attrs3: ui_host = None except ValueError: ui_host = None @@ -799,37 +816,58 @@ def mgear_dagmenu_fill(parent_menu, current_control): # divider cmds.menuItem(parent=parent_menu, divider=True) - for attr in attrs: - # found attribute so get current state - current_state = cmds.getAttr("{}.{}".format(ui_host, attr)) - states = {0: "Fk", 1: "Ik"} + def add_menu_items(attrs, attr_extension="_blend"): + for attr in attrs: + # found attribute so get current state + current_state = cmds.getAttr("{}.{}".format(ui_host, attr)) + if attr_extension == "_blend": + states = {0: "Fk", 1: "Ik"} + else: + states = {0: "IK", 1: "Fk"} - rvs_state = states[int(not current_state)] + rvs_state = states[int(not current_state)] - cmds.menuItem( - parent=parent_menu, - label="Switch {} to {}".format(attr.split("_blend")[0], rvs_state), - command=partial(__switch_fkik_callback, ui_host, False, attr), - image="kinReroot.png", - ) + cmds.menuItem( + parent=parent_menu, + label="Switch {} to {}".format( + attr.split(attr_extension)[0], rvs_state + ), + command=partial( + __switch_fkik_callback, + ui_host, + False, + attr, + attr_extension, + ), + image="kinReroot.png", + ) - cmds.menuItem( - parent=parent_menu, - label="Switch {} to {} + Key".format( - attr.split("_blend")[0], rvs_state - ), - command=partial(__switch_fkik_callback, ui_host, True, attr), - image="character.svg", - ) + cmds.menuItem( + parent=parent_menu, + label="Switch {} to {} + Key".format( + attr.split(attr_extension)[0], rvs_state + ), + command=partial( + __switch_fkik_callback, ui_host, True, attr, attr_extension + ), + image="character.svg", + ) - cmds.menuItem( - parent=parent_menu, - label="Range switch", - command=partial(__range_switch_callback, ui_host, attr), - ) + cmds.menuItem( + parent=parent_menu, + label="Range switch", + command=partial( + __range_switch_callback, ui_host, attr, attr_extension + ), + ) - # divider - cmds.menuItem(parent=parent_menu, divider=True) + # divider + cmds.menuItem(parent=parent_menu, divider=True) + + if attrs: + add_menu_items(attrs) + if attrs3: + add_menu_items(attrs3, attr_extension="_switch") # select all function cmds.menuItem(