Query if a node name it's Unique (Maya Python)

maya
#1

Hi, sorry if it is a silly question but I don’t have to much coding experience.

I’m asking for the easiest way to check if a node have an unique name or not (ignoring the DAG hierarchy, just the node name itself).

Thanks :smiley:

There’s a plenty options (like in any easy task) so I recommend to read all the answers, just for fun…

1 Like
#2

Hey Jorge,

You can use this, very simple.

import maya.cmds as mc
result = mc.ls(“pSphere1”)
if len(result) > 1:
print “Non unique.”

Use maya.cmds.ls to query the desired name, in this case “pSphere1” and if anything more than 1 result is returned, you know its non unique.

Hope that helps!

2 Likes
#3

Duplicate names in Maya have a “|” character in their name. (How Maya recognizes them)
You can simply check for that.

Example using PyMel (Prints all duplicates in scene):

list_all = pm.ls()
for i in list_all:
    if "|" in i.name():
        print i.name()
1 Like
#4

To be more accurate, all nodes have underscores in their long name, and if there are duplicates, Maya shows the longName instead of the name, by default, but not necessarily, depending on your flags.

For examples:

# cmds
cmds.ls('foo', long=True)
# PyMEL
somePyNode.longName()
2 Likes
#5

Oh, thanks everyone, now I realize how silly it was… I use ls command every day and never use with this purpose…:sweat_smile:

Yep, I learned about it the hard way when someday I started to sort names with the long name flag… tricky behavior :upside_down_face:

#6

from pymel import core as pm
pm.objExists(‘pSphere1’)

or

from maya import cmds
cmds.objExists(‘pSphere1’)

1 Like