PDA

View Full Version : Script to select all objects with same name as current selection


lellis2k
03-03-2009, 12:34 PM
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

Piflik
03-03-2009, 05:22 PM
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
)
)

ZeBoxx2
03-03-2009, 06:32 PM
sname = $.name
for i in $.(sname) do selectmore i


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 ).

==========

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
)
)


Note that the above should be "o.name == Name_Var" (comparison, instead of variable value assignment)

JHN
03-03-2009, 07:46 PM
You can't use a variable in a name path, I'm afraid.

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

ZeBoxx2
03-03-2009, 08:15 PM
Not directly but you could build a namepath string and execute it.


select (execute ("$*"+selection[1].name+"*"))



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 %)

Insanto
03-03-2009, 09:22 PM
i don't know the exact notation but with special character you should use ''

$'bla.kjasd 3'
as far as i remember

PiXeL_MoNKeY
03-03-2009, 09:27 PM
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

ZeBoxx2
03-03-2009, 09:39 PM
Oh yeah, that's right! :D

well... screw the matchpattern, then - just go with dollarasteriskquotedoublequoteplus :)

... but what happens if it contains a quote?

thatoneguy
03-03-2009, 10:18 PM
I always use: \" which is the literal for ".

ZeBoxx2
03-03-2009, 10:26 PM
I always use: \" which is the literal for ".

as in the following...?
select (execute ("$*\""+a.name+"\"*"))

thatoneguy
03-03-2009, 10:30 PM
Yes.

EDIT: Wait... no.

EDIT: Wait... Wait... Yes. :D

PiXeL_MoNKeY
03-03-2009, 10:37 PM
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

thatoneguy
03-03-2009, 10:39 PM
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. :D

ZeBoxx2
03-04-2009, 02:59 AM
Well, what I was getting at with the first one (single quotes)...

This fine...

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
selection.count
2


This is not so fine...

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
selection.count
0


This is definitely not fine...

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 of script, expected end of literal
-- In line: $*''Foot.L'*


Did I miss one?

JHN
03-04-2009, 07:56 AM
By the time this thread is finished everybody is well educated in path literals.

@Zeboxx2: Funny thing you mention the use of . and ' in a name. Where I work we shoot people for using that, no questions, no trial.

Only underscores _ and and minus - are allowed. I mean really c'mon... :thumbsup:

-Johan

P.s. I agree that getNodeByName should take wildcards, I cannot remember I ever used it's ability to return an array... of what, objects with the same name(!?) you have to really do your best to make a scene with objects with the same name...

ZeBoxx2
03-04-2009, 08:35 AM
Well although I agree that one -shouldn't- use them.. any more than, say, spaces in file names, that doesn't mean other people won't... especially as 3ds Max allows them :\ That's usually where these methods fall apart, sadly.

( I mention spaces in filenames as that's known to break a ton of utilities that take a filename as a parameter but don't know how to deal with the spaces.. many of them later being retrofitted to take double quotes around them e.g. 'some.exe /param "some file.txt"' .. but then what happens when the file itself has double quotes (allowed in most file systems) )

And yes, I'm not entirely too sure what the point of getNodeByName with the #all option is.. I guess to get all objects that have the same name without having to construct a path literal? It feels a bit 'tacked on' given the name of the function, really.

But at least the matchpattern method seems to behave itself :x

ZeBoxx2
03-04-2009, 03:31 PM
not applicable to this thread, but...

But at least the matchpattern method seems to behave itself :x
errr...
matchPattern "" pattern:"**" -- returns false. d'oh!

Kameleon
03-04-2009, 04:36 PM
How about this?

a = sphere name:"F.oot\d\\\"//''L"
b = sphere name:"OtherDude.F.oot\d\\\"//''L"
tstring=dotnetobject "System.String" a.name
sstring=dotnetobject "System.String" b.name
sstring.contains tstring

And the "full" script would be something like this:

theSel=selection[1]
theName=theSel.name
select $*
deselect theSel
for i in selection do
(
theString=dotnetobject "System.String" i.name
if (theString.contains theName)==false then deselect i
)

lellis2k
03-05-2009, 03:54 PM
Wow,

wasn't expecting such an in depth response, thanks everyone who added something.

Here's what I went with:

if selection.count == 1 do ( Name_Var = $.name for o in objects where o.name == Name_Var do selectmore o )

Though I don't have to worry about special characters as we don't use any.

Leigh

CGTalk Moderation
03-05-2009, 03:54 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.