PDA

View Full Version : select an object with a part of his name


arno3d
01-22-2009, 10:54 AM
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

shibumenon
01-22-2009, 11:50 AM
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()

arno3d
01-22-2009, 12:04 PM
yes it works ! Very usefull script for a character animator like me.
I put it as macroscript in my Quads.

Arno

shibumenon
01-22-2009, 12:29 PM
hmm ... it could do with some enhancements ...
1) 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
2) The tool is restricted to the side ID string being of 2 letters eg "_L" or "_U" "_D"

ZeBoxx2
01-22-2009, 04:22 PM
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:\n" o.name
for i = 1 to opposites.count do (
format "\t'%' (exists: %)\n" 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:
1. it's not a name that should have been 'mirrored'
2. 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

shibumenon
01-22-2009, 05:18 PM
Your script is so much better, Richard. And extendable too :)

CGTalk Moderation
01-22-2009, 05:18 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.