[Closed] Script to select all objects with same name as current selection
Hi Guys,
Im a total n00b at MaxScript and have hit a wall.
I select an object then when I click my button I want it to select all objects that have the same name as that object.
I attempted it with:
sname = $.name
for i in $.(sname) do selectmore i
as this works to select all objects with ‘tree’ in the name:
for i in $tree do selectmore i
however I can’t get it to work,
any ideas?
Thanks,
Leigh
You can’t use a variable in a name path, I’m afraid.
If your object is “Teapot” and you’re looking for objects such as “Hello.Teapot”, “World.Teapot” (i.e. ending in “.Teapot”), you’d have to use something like:
sname = $.name
nodes = for o in objects where (matchPattern o.name pattern:("*." + sname)) collect ( o )
selectMore nodes
( if sname doesn’t contain periods, there’s a few other methods you can use, but the above is pretty flexible ).
==========
Note that the above should be “o.name == Name_Var” (comparison, instead of variable value assignment)
Not directly but you could build a namepath string and execute it.
on button pressed do
(
if selection[1] != undefined then
select (execute ("$*"+selection[1].name+"*"))
)
Which off course only makes sense if the name can be found in other object completely… which won’t be the case often, me thinks…
-Johan
If they have the exact same name that should work:
on Button pressed do
(
if selection.count == 1 do
(
Name_Var = $.name
for o in objects where o.name = Name_Var do selectmore o
)
)
The problem with that approach is when your objects have a period in their name. E.g.
-- This works
a = sphere name:"Foot_L"
b = sphere name:"OtherDude.Foot_L"
select (execute ("$*"+a.name+"*"))
$Sphere:Foot_L @ [0.000000,0.000000,0.000000]
$Sphere:OtherDude.Foot_L @ [0.000000,0.000000,0.000000]
OK
-- This goes boink.
a = sphere name:"Foot.L"
b = sphere name:"OtherDude.Foot.L"
select (execute ("$*"+a.name+"*"))
$Sphere:Foot.L @ [0.000000,0.000000,0.000000]
$Sphere:OtherDude.Foot.L @ [0.000000,0.000000,0.000000]
-- Error occurred during fileIn in StringStream:"$*Foot.L*"
-- Error occurred in anonymous codeblock; filename: none; position: 0
-- Frame:
-- Syntax error: at end, expected <factor>
-- In line:
At one point I tried a workaround and it told me there’s no such property ‘L’ in “$*Foot//etc.”. Might’ve missed some magic there, not sure.
Edit: like escaping the period character. Though at that point you might as well matchpattern >_<
ideally, getNodeByName would take wildcard characters – but alas %)
i don’t know the exact notation but with special character you should use ‘’
$‘bla.kjasd 3’
as far as i remember
Insanto is correct, anything that has anything but alphanumeric characters should be wrapped in ’ ‘. Richard if you modify your second script to:
a = sphere name:“Foot.L”
b = sphere name:“OtherDude.Foot.L”
select (execute (“$’“+a.name+”’“))
It won’t error any more.
-Eric
Oh yeah, that’s right!
well… screw the matchpattern, then – just go with dollarasteriskquotedoublequoteplus
… but what happens if it contains a quote?
Yes, if you wanted to search for “$“‘Foot.L’”” (NOTE: you would still need the ‘, because of the non-alphanumeric characters), instead of “$‘Foot.L’”.
-Eric
Oh I thought the literal escape would force it.
What about
@“Foot.l”
EDIT: Haha nope that doesn’t format right at all. I see what the problem is now. ’ it is.