Notifications
Clear all

[Closed] grabbing part of a name

Hi,

Can someone tell me how to read and store part of an object’s name, ie. if it has 16 characters and I just want to check the first 8.

This is to allow me to perform a check on all objects which start with the same characters, even though their full names may be unique.

6 Replies
1 Reply
(@bobo)
Joined: 10 months ago

Posts: 0

Many ways to do this.

You can filer objects by name using substring or matchPattern if you know the name as a string:

theFilteredArray = for o in objects where matchPattern o.name pattern:"SomeName*" collect o

theFilteredArray = for o in objects where substring o.name 1 8 == "SomeName" collect o

Note that matchPattern is by default case-insensitive unless you tell it not to be, while SubString is case-sensitive and would not catch “somename” as a match…

If you are just typing in the Listener, you can just say

theArray = $SomeName* as array

The result will be an array of all the objects starting with “SomeName”. This is case insensitive, too. You can also use the above with Execute if you have the name pattern as a string and don’t want to use a for loop:

theNamePattern = "SomeName"
theArray = Execute ("$" + theNamePattern + "* as array" )

Hope this helps.

Thanks Bobo.

I’ll need to give these a shot so that I understand them better. I’ll check the code against the maxscript reference to help me work out what those commands do. I didn’t realise you could use wildcards in maxscript though.

Just checked out this one

theArray = $somename* as array

When I check the array I see it gives me the object class, its name and its position for each element in the array. How do I now use only the name portion for each element to perform additional functions?

$Editable_Mesh:somename_test @ [0.000000,0.000000,0.000000]

1 Reply
(@bobo)
Joined: 10 months ago

Posts: 0

The array contains pointers at scene nodes. It does not contain strings with class, name and position, what you see is how MAXScript is showing you the REAL NODES.
So you can apply any node operations on this array or its elements, like inquiring their properties including their .name property (see other replies).
For example, you could add a bend modifier to all the objects in the array using

addModifier theArray (bend angle:90)
  

and it would work because theArray contains a collection of actual scene objects!

The shortest way to collect all names of those objects would be

theNamesArray = for o in $somename* collect o.name

I just assumed you don’t want just the names, you want the objects that have the names…

To get the name of an object just do ‘.name’ i.e.

theObjArray = $somename* as array;
  theNameArray = (for eachObj in theObjArray collect eachObj.name);

or more specifically

theObjArray = $somename* as array;
  theObjArray[1].name;

where ‘[1]’ is the first item in the object array

Thanks a lot guys, understand it now.