rename_hash.py

There are lots of cases where you want to name things incrementally:

This is a pain to do manually, and there have been many ways to script this process. My implementation uses regex.

Snippet - save to shelf!

import maya.cmds as cmds
import re


def increment(match):
    num = int(match.group())
    pad = match.end() - match.start()
    num += 1
    return f'{num:0{pad}d}'

sel = cmds.ls(sl=True, l=True)
if len(sel) < 2:
    raise RuntimeError("Select at least two objects to rename.")

first, *rest = sel
name = first.split('|')[-1]

objs = []

for path in sel:
    name = re.sub(r'\d+', increment, name) if objs else name
    objs.append((path, name))

for obj in sorted(objs, key=lambda x: x[0].count('|'), reverse=True):
    cmds.rename(obj[0], obj[1])

Select the object to base your names from, then the objects to be renamed.

This script renames objects incrementally based on the first selected one. It respects the original padding; object_0001_suffix will increment to object_0002_suffix. It can also increment from an arbitrary number; obj_0123 increments to obj_0124.

This implementation does not need a UI and require no extra user input. It's been of great use so far.

Update 2024-12-31: Altered the logic: Prevent 'Invalid Path' error when renaming a hierarchy