You can call me a numerophobe when naming rig objects, but the reason I personally avoid numbers in my rigging naming conventions (99% of the time) is so that when I duplicate something, a number at the end won’t increase by itself because there isn’t one. (The issue can be circumvented by having numbers somewhere before the end, but I’m the kind of weirdo that prefers to go with letters altogether.)
We humans work numbers in what’s known as the “Numeral system“, also known as the “base 10” system (as we have that many fingers…) but hold on… the alphabet has 26 letters, not 10, so what do we call that system? It has a name: it’s the Hexavigesimal (or “base 26”) system.
As the ever-so-handy Wikipedia denotes,
Any number may be converted to base-26 by repeatedly dividing the number by 26.
Pretty easy stuff for Python.
This is how I’d do it:
def base26str(i, padding = ""):
"""
Turns a number to a base26 string, with optional padding support.
"""
alphabet = map(chr, range(65, 91))
alphaStr = ""
if i > 0:
number = i
while (number > 0):
remainder = number % 26
alphaStr = alphabet[remainder] + alphaStr
number /= 26
else:
alphaStr = "A"
# Pad to X digits?
if str(padding).isdigit():
alphaStr = alphaStr.zfill(int(padding)).replace("0",alphabet[0])
return alphaStr
# ------------------------------------
# Check this out...
print base26str(4) # "E"
print base26str(4, 3) # "AAE" ("E" padded to 3 letters. A = 0)
print base26str(28) # "BC"
#
Now let’s put it to use in a simple renamer example. This sample below is Softimage-oriented, but you could easily change it to PyMEL:
from math import ceil
xsi = Application
typeSuffix = {
"crvlist": "CRV",
"polymsh": "GEO",
"surfmsh": "GEO",
"null": "NULL",
"root": "RT",
"bone": "BN",
"eff": "EFF"
}
oSel = xsi.Selection
prefix = xsi.XSIInputBox("What would you like to prefix these objects?", "NAME PREFIX", oSel(0).Name )
# Find out the padding required:
padding = ""
if oSel.Count > 26:
padding = int( ceil( oSel.Count / 26.0 ) )
for i, eachObj in enumerate(oSel):
# Get suffix if it has been defined, else leave blank:
suffix = ( "" if eachObj.Type not in typeSuffix.keys() else "_"+typeSuffix[eachObj.Type] )
# Rename object:
newname = prefix+"_"+ base26str(i) +suffix
print eachObj.Name+" --RENAMED-TO--> "+newname
eachObj.Name = newname
#