Automatically rename a node with the same name found while working

This article is the 15th day article of Maya Advent Calendar 2019.

You saw zebraed's callback using pyside: MEventMessage the other day. Is it?

It is said that OpenMaya can handle the same functions as scriptJob lightly and simply, so I will utilize it in the future.

This time I will write using scriptJob, but ...


Introduction

Duplicate node names do not cause a fatal error in Maya. However, if you do not write the process in the correct way when creating a mel script, the specified object may not be found and the script may stop.

--Maya Error: Line 0: Multiple Objects Match Name -Avoiding Mel's "More than one object matches name"

--There is no problem if the tool works correctly even if there are duplicate node names, and if you can finally clean the data, you do not need to read this article.

It would be easier if we could rename it automatically before something happened.

Automatic renaming process

1. Rename

  1. Get the node from uuid. -[Maya] Object selection without object name _ Use UUID
  2. If the node name contains | (vertical line), the node name is duplicated.
  3. Get the base node name with a regular expression. --Get the range marked in red as the base name.
    image.png
  4. Rename
    • cmds.rename --If you add a "#" at the end of the new name, the rename command replaces the last "#" with a number that makes the new name unique.

The resulting code looks like this

python


def rerename(node_uuid):
    for node in cmds.ls(node_uuid):
        if "|" in node:
            basename = re.search("^(.+)?\|(.+?)(\d+)?$", node).group(2)
            rename_name = cmds.rename(node, "{}#".format(basename))

nonSameNameAlliance.py#L5-L9

2. Processing during manual renaming

--Performs rename processing when NameChanged is executed. --When renaming manually, I think that only one object is selected, so get the selected object with the ls command, convert it to UUID, and execute the renaming process.

def renameEvent():
    for node_uuid in cmds.ls(cmds.ls(sl=True), uuid=True):
        rerename(node_uuid)

nonSameNameAlliance.py#L11-L13

3. Processing at the time of replication

--When duplicating manually, the selection moves to the duplicated object, so rename processing is executed when SelectionChanged is executed. --Dag = True to get the hierarchy, tr = True to get only the transform node, convert it to UUID and execute the rename process.

def duplicateEvent():
    for node_uuid in cmds.ls(cmds.ls(sl=True, dag=True, tr=True), uuid=True):
        rerename(node_uuid)

nonSameNameAlliance.py#L15-L17

Register for script job

def main():
    try:
        if jobIds is not None:
            for jobId in jobIds:
                cmds.scriptJob(kill=jobId, force=True)
    except:pass
        
    jobIds = []
    jobIds.append(cmds.scriptJob(event=["NameChanged", renameEvent], protected=True))
    jobIds.append(cmds.scriptJob(event=["SelectionChanged", duplicateEvent], protected=True))

--If you register a process in a script job, the registered process will be executed at the timing that meets the conditions. --This time, the processes are registered for renameEvent and SelectionChanged respectively.

nonSameNameAlliance.py#L19-L28

Automatically run script jobs when Maya starts

userSetup.py


import nonSameNameAlliance;nonSameNameAlliance.main()

Place nonSameNameAlliance.py in Maya's Scripts folder and add the startup process to userSetup.py. Now when Maya starts, the process will be registered in scriptJob and will be automatically renamed.

download

Click here for this source code https://github.com/teionn/nonSameNameAlliance

important point

  1. The node is automatically renamed when you operate it in the scene, so I think it is basically for modelers.

  2. It is designed to run only on transform nodes. --If you want to get the effect on all nodes such as intermediate shape nodes, delete tr = True.

  3. This script does not judge case. Maya can retain case-sensitive objects even in the same hierarchy. image.png

If you bring an asset to Unity in this state, if there are objects ʻobject_a and ʻobject_A, it seems that either object cannot be referenced.


Tomorrow's article will be about rateionn Do you properly Remove: Unknown Plugins?.

That ... I'm writing ... I haven't written an article yet ...

I'm planning to release the script for free in time, so I'd be happy if I just downloaded it.

Recommended Posts

Automatically rename a node with the same name found while working
(Note) Importing Excel with the same column name
Load the module with the same name in another location
When a local variable with the same name as a global variable is defined in the function
Precautions when creating a two-dimensional array with all the same values
I made a program that automatically calculates the zodiac with tkinter
Memorandum (Add name only to people with the same surname in the list)
Replace the directory name and the file name in the directory together with a Linux command.