[Closed] Telling the difference between same-named objects?
I’ve made a script that saves the names of objects and their corresponding materials to a file to apply to the updated version of a scene. This works fine as long as every object has its own individual name and is never renamed. Problems arise when some objects are renamed, or when identically named objects appear in the scene.
So I’m wondering, is there some sort of ID or something that is attached to an object in addition to the name to make it individual in the scene that I can use to select objects instead of using the name?
You can try .handle, but keep in mind that this ID allocation is dynamic at the time of creation, so if you were to import an object with a given .handle ID into another scene, chances are it would be assigned a different ID at that point.
Here is a simple solution that should work.
select(for o in objects where o.name == "Teapot01" collect o)[1]
this code will run through all objects in the scene in the order of their creation and collect all objects named “Teapot01”, then it will select the first one
In the same way you can access the 2nd one, the 3rd and so on…
You can also us the AnimHandle System introduced in Max 2008, if you’re using that version or higher. It’s supposed to be more reliable than using the .handle property of an object, although I haven’t personally done any kind of comparison.
This is from the help docs…
AnimHandle System
[left]The AnimHandle system defines a unique ID number for every entity in the scene, including nodes, modifiers, materials, controllers, etc. When an entity is deleted from memory, its number will never be reused by any entity created later, except in the case of the so-called “wrap-around” – an unhealthy condition possible only when four billion entities (the 32-bit limit) are created in a single 3ds Max session.
[/left]
[left]Therefore you can determine whether an object is deleted by requesting the object using its handle, and seeing if the result is undefined.
[/left]
[left]Available in 3ds Max 2008 and higher.
[/left]
[left]Methods:
[/left]
[left]<integer>GetHandleByAnim <object>
[/left]
[left]This method returns the unique AnimHandle ID for the given scene entity.
[/left]
[left]
[/left]
[left]<object>GetAnimByHandle <integer>
[/left]
[left]This method returns the scene entity corresponding to the unique AnimHandle ID, or undefined if the entity has been deleted or has not been created yet.
[/left]
[left]
[/left]
[left]FOR EXAMPLE:
[/left]
[left]
[/left]
[left]x = teapot()[color=green]–create a teapot[/color]
[/left]
[left]xHandle = GetHandleByAnim x [color=green]–[/color][color=green]get the anim.handle [/color]
[/left]
[left]x_verify = GetAnimByHandle xhandle [color=green]–[/color][color=green]get by anim.handle[/color]
[/left]
[left]x == x_verify [color=green]–[/color][color=green]compare the two – [/color][color=green]returns true[/color]
[/left]
[left]delete x[color=green]–delete the teapot[/color]
[/left]
[left]clearUndoBuffer() [color=green]–object deleted [/color][color=green]completely [/color][color=green]from memory[/color]
[/left]
[left]x_verify = GetAnimByHandle xhandle [color=green]–now returns undefined[/color]
[/left]