Renaming multiple illegal nodes

Renaming multiple illegal nodes

daniel.j.rutter
Contributor Contributor
1,390 Views
5 Replies
Message 1 of 6

Renaming multiple illegal nodes

daniel.j.rutter
Contributor
Contributor

Hi All,

I imported a file and ended up with hundreds of nodes named as follows: mmici_v2_2_extended:

 

Is there any script available that would remove the illegal colon and replace it with an underscore? I've searched the information superhighway and haven't come up with anything.  I would think this would be simple but the search and replace and namespace editors don't seem to have any effect.

 

Thanks in advance for advice,

 

Dan

0 Likes
Accepted solutions (1)
1,391 Views
5 Replies
Replies (5)
Message 2 of 6

daniel.j.rutter
Contributor
Contributor
Just to clarify: the name includes the colon at the end, so mmici_v2_2_extended:59

I need to remove the colon and replace it with an underscore.

Thanks!
0 Likes
Message 3 of 6

BigRoy
Advocate
Advocate

The colon defines a namespace in Maya. You can remove the namespace if you'd like through Windows > General Editors > Namespace Editor.

 

To e.g. do it through code and instead swap it with a namespace here's a snippet of code:

from maya import cmds

# Rename all nodes
# Iterate in reverse order by length of the node path so
# that we rename children before their parents otherwise
# node names get invalid as you rename them
for node in reversed(sorted(cmds.ls(long=True), key=len)):
    if ":" in node:
        # Get short name
        name = node.rsplit("|", 1)[-1]
        cmds.rename(node, name.replace(":", "_"))
        
# Delete all empty namespaces
for namespace in cmds.namespaceInfo(listOnlyNamespaces=True, internal=False):
    
    if namespace in {"UI", "shared"}:
        # Ignore some internal UI namespaces
        continue
    
    members = cmds.namespaceInfo(namespace, listNamespace=True)
    if not members:
        print namespace
        cmds.namespace(removeNamespace=namespace)

 

Message 4 of 6

daniel.j.rutter
Contributor
Contributor
Accepted solution

Hi All,

 

I got a solution from Marko S at Autodesk Support and thought I'd share it. Just highlight the nodes to be renamed, enter the code below into the script editor (after changing the "corr_name" to suit your needs then press enter.

 

// rename multiple objects
{
    string $new_name = "corr_name"; //change corr_name to any name you want to use
    string $allnodes[] = `ls -sl`;
    int $icount = 0;
    for ($cnode in $allnodes) {
        $icount++;
        rename $cnode ($new_name + $icount);  
    }
    print ( $icount + " nodes renamed ; " );
}
Message 5 of 6

daniel.j.rutter
Contributor
Contributor
By highlight I mean select.
0 Likes
Message 6 of 6

hskel1sc
Community Visitor
Community Visitor

This didnt work. Any other solution. It sucks, I literally have thousands of illegal names.

0 Likes