[Closed] select an object with a part of his name
Hello,
I would like to make a script that recognise the name of a selected object and select the opposite. For example: If a hand of a character named “Hand_L” is selected, the script will select the opposite hand named “Hand_R”.
thanks a lot
Arno
not fully tested and no error catching in it, but this should do the job:
if your left and right labels are different, then change them here:
sideIdentifierStrL = “_L”
sideIdentifierStrR = “_R”
This script works with multiple objects as well.
sideIdentifierStrL = "_L"
sideIdentifierStrR = "_R"
fn returnObjSideID obj =
(
return (substring obj.name (obj.name.count - 1) obj.name.count)
)
fn returnObjNameWithoutSideID obj =
(
return (substring obj.name 1 (obj.name.count - 2) )
)
fn selectOppObjs =
(
theSelObjs = selection as array
allRObjs = #();
allLObjs = #();
for i = 1 to theSelObjs.count do
(
curObj = theSelObjs[i];
curObjSideID = returnObjSideID(curObj);
curObjNameOnly = returnObjNameWithoutSideID(curObj)
if(curObjSideID == sideIdentifierStrL) then
(
allObjs = $* as array;
for obj in allObjs do
(
if(curObjNameOnly == returnObjNameWithoutSideID(obj) and returnObjSideID(obj) == sideIdentifierStrR ) then
append allRObjs obj
)
)
if(curObjSideID == sideIdentifierStrR) then
(
allObjs = $* as array;
for obj in allObjs do
(
if(curObjNameOnly == returnObjNameWithoutSideID(obj) and returnObjSideID(obj) == sideIdentifierStrL ) then
append allLObjs obj
)
)
)
print allRObjs
clearSelection()
select allRObjs
selectmore allLObjs
)
selectOppObjs()
yes it works ! Very usefull script for a character animator like me.
I put it as macroscript in my Quads.
Arno
hmm … it could do with some enhancements …
- It currently only looks for the side ID at the end of the object name. So the tool is of no use with bipeds which has default object names like
bip L hand
or something like that - The tool is restricted to the side ID string being of 2 letters eg “_L” or “_U” “_D”
here’s a slightly different method – you can keep the macros, it uses the same function name
fn selectOpposites report:false = (
local curSel = getCurrentSelection()
local leftIDs = #("_L", "L ", "Left")
local rightIDs = #("_R", "R ", "Right")
local newSel = #()
for o in curSel do (
local oppNames = for i = 1 to leftIDs.count collect ( substituteString o.name leftIDs[i] rightIDs[i] )
oppNames += for i = 1 to leftIDs.count collect ( substituteString o.name rightIDs[i] leftIDs[i] )
local opposites = for oppName in oppNames where (oppName != o.name) collect ( oppName )
local nodes = for opp in opposites collect (getNodeByName opp)
if (report) do (
format "'%' opposites:
" o.name
for i = 1 to opposites.count do (
format " '%' (exists: %)
" opposites[i] (nodes[i] != undefined)
)
)
for i = nodes.count to 1 by -1 do (
if (nodes[i] == undefined) then ( deleteItem nodes i )
)
join newSel nodes
)
select newSel
)
Adjust the “leftIDs” and “rightIDs” to match whatever you encounter.
If you call “selectOpposites report:true”, then a small report will be printed. If the report states “exists: false”, it means that the opposite object wasn’t found. That’s usually a good indication of one of two things:
- it’s not a name that should have been ‘mirrored’
- your character has lost a limb
Example output and effect in a test scene:
selectOpposites report:true
'Arm_L' opposites:
'Arm_R' (exists: true)
'Leg R Lower' opposites:
'Leg L Lower' (exists: true)
'Left Eye' opposites:
'Right Eye' (exists: true)
'L L Cool J' opposites:
'R R Cool J' (exists: false)
OK
-- and back the other way around
selectOpposites report:true
'Arm_R' opposites:
'Arm_L' (exists: true)
'Leg L Lower' opposites:
'Leg R Lower' (exists: true)
'Right Eye' opposites:
'Left Eye' (exists: true)
OK