For my scripting class, I have been working on a Maya Python script that create control curves. The script works when I create the first and second object. But if I create more objects with the same name, the name is not working like I want it to work. Right now, the object names go as follow: PREF_NAME_SUF, PREF_NAME_SUF1, PREF_NAME_SUF11, PREF_NAME_SUF111, etc.
How would I make it PREF_NAME_SUF, PREF_NAME_SUF1, PREF_NAME_SUF2, PREF_NAME_SUF3, etc?
This is what I have at the moment:
def createCurves(controlName):
if cmds.objExists(curveObject):
print("Warning: This name already exists.")
oldCurveObject = curveObject
print oldCurveObject, curveObject
for i in range(1):
i = i+1
print controlName + str(i)
controlName = oldCurveObject + str(i)
you can use the module re to extract the name.
import re
controlName = 'PREF_NAME_SUF1'
pattern = re.compile('\d+$') # finish by a number
r = pattern.search(controlName) # we search the string, if there is no result, we will do special operations
if r:
new_number = str(int(r.group(0)) + 1) # from our search we extract the number
new_name = pattern.sub(new_number, controlName) # we replace the end number by the new one
else:
if cmds.objExists(controlName):
# if the control exists already we append 1 to the name
new_name = controlName + '1'
else:
# if it doesn't exists, we dont add an iteration number
new_name = controlName
_____ EDIT _________
you don&t really understand how naming your control is done. you need to generate your three names together otherwise you create duplicated names letting maya doing it wrongly. Also try to remove debugging prints and also maybe remove globals and use more return
def createCurves(controlName):
# if the control exists already we append 1 to the name
name_pattern = re.compile( controlName + '(\d+)?$') # it doesn mean your ControlName can have a number but it is not mandatory
tr = cmds.ls(type='transform') # we list all transform
number_finder = [name_pattern.findall(i)[0] for i in tr if name_pattern.findall(i)] # we find if the name already exists
# if we find something, we work on the iteration
if number_finder:
last_number = sorted(number_finder)[-1]
if last_number != '':
new_number = str(int(last_number) + 1)
else:
new_number = '1'
else:
# if nothing we don&t need to iterate
new_number = ''
new_name = controlName + new_number
new_offname = controlName + ccOFFName + new_number
new_grpname = controlName + ccGRPName + new_number
#create the curve according to which shape is selected
if (selectedShapeValue == 'Circle'):
created_curve, createNode = cmds.circle(n= new_name)
if (selectedShapeValue == 'Square'):
created_curve = cmds.curve(n= new_name, d = 1, p = [(-1, 0, 1),(-1, 0, -1), (1, 0, -1),(1, 0, 1),(-1, 0, 1)], k = [0, 1, 2, 3, 4])
if ccOFFcheck:
cc_OFF = cmds.group(created_curve, n = new_offname)
if ccGRPcheck:
cc_GRP = cmds.group(cc_OFF, n = new_grpname)
return new_name
in this piece of code I generate the name based on one assumption : your control name dependencies must be unique Also now you generate the three names with the iteration number at the end.