Skip to content

Commit

Permalink
Version 2.16.0
Browse files Browse the repository at this point in the history
* Improved MirrorTable matching logic
  • Loading branch information
krathjen committed Apr 30, 2024
1 parent ff54fa7 commit d834a92
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 76 deletions.
97 changes: 29 additions & 68 deletions src/mutils/mirrortable.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,22 +249,12 @@ def matchSide(name, side):
:rtype: bool
"""
if side:
# Support for prefix naming convention.
if side.endswith("*"):
return MirrorTable.replacePrefix(name, side, "X") != name

# Support for suffix naming convention.
elif side.startswith("*"):
return MirrorTable.replaceSuffix(name, side, "X") != name

# Support for all other naming conventions.
else:
return side in name
return MirrorTable.rreplace(name, side, "X") != name

return False

@staticmethod
def rreplace(name, old, new, count=1):
def rreplace(name, old, new):
"""
convenience method.
Expand All @@ -275,10 +265,24 @@ def rreplace(name, old, new, count=1):
:type name: str
:type old: str
:type new: str
:type count: int
:rtype: str
"""
return new.join(name.rsplit(old, count))
names = []

for n in name.split("|"):

namespaces = ''
if ':' in n:
namespaces, n = n.rsplit(':', 1)

other = mutils.mirrortable.MirrorTable.replace(n, old, new)

if namespaces:
other = ':'.join([namespaces, other])

names.append(other)

return "|".join(names)

@staticmethod
def replace(name, old, new):
Expand Down Expand Up @@ -322,25 +326,13 @@ def replacePrefix(name, old, new):
:type new: str
:rtype: str
"""
dstName = name
old = old.replace("*", "")
new = new.replace("*", "")

# Support for the prefix with namespaces
# Group|Character1:LfootRollExtra|Character1:LfootRoll
if ":" in name:
dstName = MirrorTable.rreplace(name, ":" + old, ":" + new, 1)
if name != dstName:
return dstName
if name.startswith(old):
name = name.replace(old, new, 1)

# Support for the prefix with long names
# Group|LfootRollExtra|LfootRoll
if "|" in name:
dstName = name.replace("|" + old, "|" + new)
elif dstName.startswith(old):
dstName = name.replace(old, new, 1)

return dstName
return name

@staticmethod
def replaceSuffix(name, old, new):
Expand All @@ -360,20 +352,13 @@ def replaceSuffix(name, old, new):
:type new: str
:rtype: str
"""
dstName = name
old = old.replace("*", "")
new = new.replace("*", "")

# Support for the suffix with long name
# Group|Character1:footRollExtraR|Character1:footRollR
if "|" in name:
dstName = name.replace(old + "|", new + "|")

# Eg: Character1:footRollR
if dstName.endswith(old):
dstName = dstName[:-len(old)] + new
if name.endswith(old):
name = name[:-len(old)] + new

return dstName
return name

def mirrorObject(self, obj):
"""
Expand All @@ -398,37 +383,13 @@ def _mirrorObject(obj, leftSide, rightSide):
:type obj: str
:rtype: str or None
"""
# Support for the prefix naming convention.
if leftSide.endswith("*") or rightSide.endswith("*"):
dstName = MirrorTable.replacePrefix(obj, leftSide, rightSide)

if obj == dstName:
dstName = MirrorTable.replacePrefix(obj, rightSide, leftSide)

if dstName != obj:
return dstName

# Support for the suffix naming convention.
elif leftSide.startswith("*") or rightSide.startswith("*"):

dstName = MirrorTable.replaceSuffix(obj, leftSide, rightSide)

if obj == dstName:
dstName = MirrorTable.replaceSuffix(obj, rightSide, leftSide)

if dstName != obj:
return dstName

# Support for all other naming conventions.
else:

dstName = obj.replace(leftSide, rightSide)
dstName = MirrorTable.rreplace(obj, leftSide, rightSide)

if dstName == obj:
dstName = obj.replace(rightSide, leftSide)
if obj == dstName:
dstName = MirrorTable.rreplace(obj, rightSide, leftSide)

if dstName != obj:
return dstName
if dstName != obj:
return dstName

# Return None as the given name is probably a center control
# and doesn't have an opposite side.
Expand Down
66 changes: 59 additions & 7 deletions src/mutils/tests/test_mirrortable.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ def test_match_side(self):
result = MirrorTable.matchSide("CHR1:RIG:LRollCON", "L*")
assert result is True, msg

result = MirrorTable.matchSide("CHR1:RIG:LRollCON", "RIG:L*")
assert result is True, msg
# This is no longer supported!
# result = MirrorTable.matchSide("CHR1:RIG:LRollCON", "RIG:L*")
# assert result is True, msg

result = MirrorTable.matchSide("Group|right_arm_car_ik", "right*")
assert result is True, msg
Expand Down Expand Up @@ -148,12 +149,63 @@ def test_mirror_object(self):
result = MirrorTable._mirrorObject("Group|Ch1:RIG:Offset|Ch1:RIG:RRoll", "R*", "L*")
assert "Group|Ch1:RIG:Offset|Ch1:RIG:LRoll" == result, msg

result = MirrorTable._mirrorObject("Group|Ch1:RIG:RExtra|Ch1:RIG:RRoll", "RIG:R", "RIG:L")
# This is no longer supported!
# result = MirrorTable._mirrorObject("Group|Ch1:RIG:RExtra|Ch1:RIG:RRoll", "RIG:R", "RIG:L")
# assert "Group|Ch1:RIG:LExtra|Ch1:RIG:LRoll" == result, msg

result = MirrorTable._mirrorObject("Group|Ch1:RIG:RExtra|Ch1:RIG:RRoll", "R*", "L*")
assert "Group|Ch1:RIG:LExtra|Ch1:RIG:LRoll" == result, msg

# # WARNING: The following condition is not supported yet!
# result = MirrorTable._mirrorObject("Group|Ch1:RIG:RExtra|Ch1:RIG:RRoll", "R*", "L*")
# assert "Group|Ch1:RIG:LExtra|Ch1:RIG:LRoll" == result, msg
result = MirrorTable._mirrorObject("hand_R0_ctl", "_R", "_L")
assert "hand_L0_ctl" == result, msg

result = MirrorTable._mirrorObject("Lyle_R001:hand_R0_ctl", "_R", "_L")
assert "Lyle_R001:hand_L0_ctl" == result, msg

result = MirrorTable._mirrorObject("Lyle_R001:other_R0_ctl|Lyle_R001:hand_R0_ctl", "_R", "_L")
assert "Lyle_R001:other_L0_ctl|Lyle_R001:hand_L0_ctl" == result, msg

result = MirrorTable._mirrorObject("Character1:LfootRoll", "L*", "R*")
assert "Character1:RfootRoll" == result, msg

result = MirrorTable._mirrorObject("Group|LyleCharacter1:LfootRollExtra|LyleCharacter1:LfootRoll", "R*", "L*")
assert "Group|LyleCharacter1:RfootRollExtra|LyleCharacter1:RfootRoll" == result, msg

result = MirrorTable._mirrorObject("Character3_Ctrl_RightForeArm", "Right", "Left")
assert "Character3_Ctrl_LeftForeArm" == result, msg

result = MirrorTable._mirrorObject("r_arm_car_r", "r_*", "l_*")
assert "l_arm_car_r" == result, msg

result = MirrorTable._mirrorObject("Character:r_arm_car_r", "r_*", "l_*")
assert "Character:l_arm_car_r" == result, msg

result = MirrorTable._mirrorObject("Group|r_arm_car_r", "r_*", "l_*")
assert "Group|l_arm_car_r" == result, msg

result = MirrorTable._mirrorObject("arm_car_r", "*_r", "*_l")
assert "arm_car_l" == result, msg

result = MirrorTable._mirrorObject("Group|right_arm_car_ik", "right*", "left*")
assert "Group|left_arm_car_ik" == result, msg

result = MirrorTable._mirrorObject("CHR1:RIG:RLegCON", "R*", "L*")
assert "CHR1:RIG:LLegCON" == result, msg

result = MirrorTable._mirrorObject("RhandCON", "R*", "L*")
assert "LhandCON" == result, msg

result = MirrorTable._mirrorObject("Group|ctlHandRt", "*Rt", "*Lt")
assert "Group|ctlHandLt" == result, msg

result = MirrorTable._mirrorObject("malcolm:ctlArmIkRt", "*Rt", "*Lt")
assert "malcolm:ctlArmIkLt" == result, msg

result = MirrorTable._mirrorObject("IKExtraLegFront_R|IKLegFront_R", "*_R", "*_L")
assert "IKExtraLegFront_L|IKLegFront_L" == result, msg

result = MirrorTable._mirrorObject("Rig_v01_ref:Rig_v01:leftArm1_UpperArmControl", "_R", "_L")
assert None == result, msg


def testSuite():
Expand Down Expand Up @@ -183,4 +235,4 @@ def run():


if __name__ == "__main__":
run()
run()
2 changes: 1 addition & 1 deletion src/studiolibrary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library. If not, see <http://www.gnu.org/licenses/>.

__version__ = "2.15.0"
__version__ = "2.16.0"


def version():
Expand Down

0 comments on commit d834a92

Please sign in to comment.