How do I get the object by object name?

MAXScript FAQ > Accessing Object Properties > How do I get the object by object name?

Many Users Asked:

When typing in known object paths in the Listener, in scripts or scripted controllers, I can use the $ prefix to denote an object path (for example. $Box001 returns the object called "Box001"). But sometimes all I have is the name of the object as string ("Box001"), but I want to access the scene object that the name corresponds to.

Answer:

There are multiple ways to convert an object name (a string) to the object path (a MAXScript expression) corresponding to the scene object:

getNodeByName

This function provides an easy way to convert a name string to object path with case-sensitive and case-insensitive options.See the link for details.

Execute

Using the execute method, it is possible to convert any string into a MAXScript expression. This includes evaluating a string containing an object path into the object path itself. If an object name is stored in a string variable, you have to add the "$" character as string to the name to convert it into object path. Since certain special characters like "." (period) or " " (space) cannot be evaluated when being part of the name string, it is advisable to add "'" (single quote) characters around the object's name to prevent an evaluation error. Any characters enclosed between the single quote characters will be recognized as part of the string.

EXAMPLE

 --if an object is called Box001, you could use
objName = "Box001"
objPath = execute ("$'"+objName + "'")

See Also