When you start to automate repetitive processes via script, it quickly becomes important how you manage object names within Maya. Up on the surface, where the human brain hangs out, we know nodes if not by their spatial position and appearance in the scene then by their names; what the objects are called, such as in the outliner. But that's for the user's benefit. Maya itself keeps track of objects by different names then the ones shown in the outliner.
I'll give you an example. If you have an object in the scene and then you duplicate it, Maya will automatically edit the name of that duplicate (adding a number on the end) so that it doesn't have the exact same name. Maya doesn't let two objects have the same name.
Or does it? Try putting that duplicate object into a group, so the two matching objects are not on the same level in the hierarchy. Now try to give it the same name as the original object and you'll notice Maya won't object.
Hm. So Maya only objects to two objects having the same name if they're adjacent in the hierarchy? That seems arbitrary.
Well the key here is that when Maya looks at an object, it doesn't see the same name you do.
In my case, my duplicate cubes appear to both be called cube1. But what Maya sees is that the cube in the world is called |cube1 and the cube inside the group is called |grp|cube1.
Yes, Maya keeps track of objects by looking at their entire addresses in the hierarchy.
So as far as Maya is concerned these two cubes don't have the same name. Because their paths are different.
So when writing scripts, you often want to handle objects by the full path names so that the script doesn't spit out an error saying "'cube1' refers to multiple objects".
Here are a few handy tags to keep in mind:
When creating lists of objects in Python using the 'ls' maya command, if Maua detects multiple objects of the same name it will automatically return the full path name in order to differentiate. But you can include the 'long' tag with a value of True or of 1 and this will force Maya to return the full path names of objects even if there's only one of the object in question.
Try it out. Select an object with a unique name...
...and run this:
import maya.cmds as cmds
obj = cmds.ls(selection=True)[0]
print(obj)
Now try it again with the long tag added:
import maya.cmds as cmds
obj = cmds.ls(selection=True, long=True)[0]
print(obj)
Another Maya command with a built in tag to assist you is 'listRelatives', with its 'fullPath' tag.
Select and object and run this to return its relatives:
import maya.cmds as cmds
obj = cmds.ls(selection=1, long=True)[0]
objRelatives = cmds.listRelatives(obj, children=True)
print(objRelatives)
Now do the same including the 'fullPath' tag:
import maya.cmds as cmds
obj = cmds.ls(selection=1, long=True)[0]
objRelatives = cmds.listRelatives(obj, children=True, fullPath=True)
print(objRelatives)
You can go a level deeper and refer to objects in your scripts by their address in your computer's memory, then the script will be able to keep track of an object even if its name changes. But that's next level. Let's say no more about it... for now.
No comments:
Post a Comment