logo

CreeperMeshDeform_PY

#Code written by Luis Quinones @ [complicitMatter]
#Feel free to use and modify the code in any way you want, please comment below with suggestions, ideas of any relevant throughts
import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

def movePoints(mVerts,flattened,myIndexes,attrPts):
    otherIndexList = []
    staticList = []
    #go through and move the points that are not affected by the attactor
    for k in range(len(mVerts)):
        if k not in flattened:
           otherIndexList.append(k)
           mOV = gh.Vector2Pt(mVerts[k], mVerts[k], False) [0]
           otherVertsLoc = gh.Move(mVerts[k], mOV)
           staticList.append(otherVertsLoc[0])
            
    movedList = []
    #Go through and move the points that are affected by the attactors
    for k in range(len(myIndexes)): 
        for index in myIndexes[k]: #we are going through a list of list of indexes 
            myVec = gh.Vector2Pt(mVerts[index],attrPts[k], False) [0] #create the vector to the correct attractor
            myLargeVec = gh.Amplitude(myVec, vAmp)
            transVerts = gh.Move(mVerts[index], myLargeVec)
            movedList.append(transVerts[0]) #append it to the moved list
            
    return staticList, movedList, otherIndexList
    
def resortData(mVerts, flattened, staticList, movedList, otherIndexList):
    count = 0
    comboList = []
    #resort everything to match the input list orders
    for p in range(len(mVerts)):
        if p not in flattened:
            comboList.insert(p,staticList[otherIndexList.index(count)]) #insert the static point at the correct index in a new list
        if p in flattened:
            comboList.insert(p,movedList[flattened.index(count)]) #insert the moved point at the correct index in a new list
        count += 1
        
    return comboList
    
#--------------------------------------------------------------
# Deconstruct Input Mesh 
mDecon = gh.DeconstructMesh(meshIn)
mVerts = mDecon[0]
mFaces = mDecon[1]

# create a list of list matching each vertex index to its corresponding attractor pt
myIndexes = []
for m in range (len(attrPts)):
    cPCalc = gh.ClosestPoints(attrPts[m], mVerts, cPCount)
    myCP = cPCalc[0]
    myIndexBase = cPCalc[1]
    myIndexes.append(myIndexBase)
dupIndexList = myIndexes[:] #dup list
flattened = [val for sublist in dupIndexList for val in sublist] #flatten it

#------------------Build Functions-----------------------------
pointSet = movePoints(mVerts,flattened,myIndexes,attrPts)
newMeshVertices = resortData(mVerts, flattened, pointSet[0], pointSet[1], pointSet[2])
#--------------------------------------------------------------

meshOut = gh.ConstructMesh(newMeshVertices, mFaces) #build that ish
Update
  • Share