Skip to content

Instantly share code, notes, and snippets.

@yamahigashi
Last active January 29, 2018 02:15
Show Gist options
  • Select an option

  • Save yamahigashi/97baa5eea5aabbb30446b587d96c58bd to your computer and use it in GitHub Desktop.

Select an option

Save yamahigashi/97baa5eea5aabbb30446b587d96c58bd to your computer and use it in GitHub Desktop.
Select opposite nodes currentry selected for Autodesk Maya.
import re
from maya.api import OpenMaya as OpenMaya2
def select_opposite():
# type: () -> None
y = OpenMaya2.MGlobal.getActiveSelectionList()
sel_strings = y.getSelectionStrings()
sel_list = OpenMaya2.MSelectionList()
for s in sel_strings:
sel_list.add(swap_l_r_name(s))
OpenMaya2.MGlobal.setActiveSelectionList(sel_list)
def swap_l_r_name(name):
# type: (Text) -> Text
"""Returns text replaced right/left.
>>> swap_l_r_name("hoge_l0_ctl")
'hoge_r0_ctl'
>>> swap_l_r_name("hoge_L0_ctl")
'hoge_R0_ctl'
>>> swap_l_r_name("Hoge_Left_Ctl")
'Hoge_Right_Ctl'
>>> swap_l_r_name("hoge_left_ctl")
'hoge_right_ctl'
>>> swap_l_r_name("LeftCtl")
'RightCtl'
>>> swap_l_r_name("HogeCtlLeft")
'HogeCtlRight'
>>> swap_l_r_name("HogeLeftCtl") # invalid naming
'HogeLeftCtl'
"""
rules = [
{
"exp": re.compile("_[RL][0-9]+_|^[RL][0-9]+_|_[RL][0-9]+$|_[RL]_|^[RL]_|_[RL]$"),
"left": "L",
"right": "R"
},
{
"exp": re.compile("_[rl][0-9]+_|^[rl][0-9]+_|_[rl][0-9]+$|_[rl]_|^[rl]_|_[rl]$"),
"left": "l",
"right": "r"
},
{
"exp": re.compile("_(right|left)_|^(right|left)|(right|left)$"),
"left": "left",
"right": "right"
},
{
"exp": re.compile("_(Right|Left)_|^(Right|Left)|(Right|Left)$|_(Right|Left)[A-Z]"),
"left": "Left",
"right": "Right"
},
]
if ":" in name:
splat = name.split(":")
namespace, name = ":".join(splat[0:-1]), splat[-1]
else:
namespace = None
for entry in rules:
exp = entry.get("exp")
left = entry.get("left")
right = entry.get("right")
# shortcuts
if name == left:
name = right
break
if name == right:
name = left
break
m = re.search(exp, name)
if m:
instance = m.group(0)
if instance.find(right) != -1:
rep = instance.replace(right, left)
else:
rep = instance.replace(left, right)
name = re.sub(exp, rep, name)
break
if namespace:
name = namespace + ":" + name
return name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment