Skip to content

Instantly share code, notes, and snippets.

@Pullusb
Created April 12, 2016 10:53
Show Gist options
  • Save Pullusb/ee5bb80df67d3d866809ed8815cf7fbf to your computer and use it in GitHub Desktop.
Save Pullusb/ee5bb80df67d3d866809ed8815cf7fbf to your computer and use it in GitHub Desktop.
Blender - increment number in name (if any) while removing trailing blender incrementation
import bpy, re
#increment number in name (if any) while removing trailing blender incrementation
#ex: obj02.L.001 >> obj03.L
def checkNum(n):
'''check if number in string (without trailing number)'''
if len(n) > 4:
return any(char.isdigit() for char in n)
#return bool(re.search(r'\d', inputString)) #regex method
else:
return (False)
def increment_rename(o):
'''assign new name with incremented number'''
name = o.name[:-4]
try:
regex = re.search('^(.*)(\d+)(.*)$', name) #regex with capture groups
padding = len(regex.group(2))#get padding from source
num = str(int(regex.group(2))+1).zfill(padding) #increment number captured and str/paddit
new = regex.group(1) + num + regex.group(3)
o.name = new
except:
print("regex error on ", o.name)
for o in bpy.context.selected_objects:
if o.name[-4:-3] == ".": #if "." character before the end (.###)
if checkNum(o.name):
increment_rename(o)
else:
print(o.name, "hasn't number in name to increment")
else:
print (o.name, "not ending with '.###'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment