Well, I’ve been meaning to make this script. So here you go.
Run on selected mesh components
redistributeVertex(ch=False)
def redistributeVertex(ch=False):
''' redistribute vertex based on a uniform curve '''
selection = cmds.ls(sl=True, fl=True)
orgSel = selection
verts = cmds.polyListComponentConversion(selection,
fromFace=True,fromEdge=True,fromVertex=True,
toVertex=True, internal=False)
verts = cmds.ls(verts, fl=True)
# convert to edge so we can use polyToCurve to fix vert order issues
edges = cmds.polyListComponentConversion(selection,
fromFace=True,fromEdge=True,fromVertex=True,
toEdge=True, internal=True)
vertData = {}
positions = []
for ii, vert in enumerate(verts):
pos = cmds.xform(vert, q=True, ws=True, t=True)
positions.append(pos)
data = {'vertex':vert,'vertexPos':pos,'cv':None,'cvPos':None}
vertData[ii] = data
#create Curve
cmds.select(edges, r=True)
curve = cmds.polyToCurve( form=2, degree=1)[0]
#find the matching cv
cvList = []
cvs = cmds.getAttr('{}.cv[li]'.format(curve))
[/li] for i in range(len(cvs)):
curveCV = '{}.cv[{}]'.format(curve,i)
cvList.append(curveCV)
cvPos = cmds.xform(curveCV, q=True, ws=True, t=True)
for vert, data in vertData.items():
vertex = vertData[vert]['vertex']
pos = cmds.xform(vertex, q=True, ws=True, t=True)
roundCvPos = [round(x, 3) for x in cvPos]
roundPos = [round(x, 3) for x in pos]
if roundCvPos == roundPos:
vertData[vert]['cv'] = curveCV
continue
cmds.rebuildCurve(
curve, ch=1, rpo=1, rt=0, end=1, kr=0, kcp=0, kep=1, kt=0, s=0, d=1, tol=0.01)
#set New vertex postion
for vert, data in vertData.items():
vertex = vertData[vert]['vertex']
curveCV = vertData[vert]['cv']
if curveCV:
cvPos = cmds.xform(curveCV, q=True, ws=True, t=True)
vertData[vert]['cvPos'] = cvPos
cmds.xform(vertex, ws=True, t=cvPos)
else:
print "No matchign Cv found for vertex", vertex
if not ch:
cmds.delete(curve)
if orgSel:
cmds.select(orgSel, r=True)
return vertData