Script to select all objects with same name as current selection


#1

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


#2

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




#3

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)


#4

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


#5

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


#6

i don’t know the exact notation but with special character you should use ‘’

$‘bla.kjasd 3’
as far as i remember


#7

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


#8

Oh yeah, that’s right! :smiley:

well… screw the matchpattern, then - just go with dollarasteriskquotedoublequoteplus :slight_smile:

… but what happens if it contains a quote?


#9

I always use: " which is the literal for ".


#10

as in the following…?

select (execute ("$*\""+a.name+"\"*"))

#11

Yes.

EDIT: Wait… no.

EDIT: Wait… Wait… Yes. :smiley:


#12

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


#13

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. :smiley:


#14

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?


#15

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…


#16

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


#17

not applicable to this thread, but…

errr…

matchPattern "" pattern:"**" -- returns false. d'oh!

#18

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
)

#19

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


#20

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.