How do I get the object by object name?

MAXScript Frequently Asked Questions

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. $Box01 returns the object called "Box01"). But sometimes all I have is the name of the object as string ("Box01"), 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 "." (dot) 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.

For Example

--if an object is called Box01, you could use

objName = "Box01"

objPath = execute ("$'"+objName + "'")

See also

Why does Execute return 'undefined' when accessing a loop variable?

Frequently Asked Questions