Skip to content

Instantly share code, notes, and snippets.

@bohdon
Last active December 19, 2015 15:58
Show Gist options
  • Save bohdon/5979812 to your computer and use it in GitHub Desktop.
Save bohdon/5979812 to your computer and use it in GitHub Desktop.
cluster geometry shell by geo
import butterfly
from butterfly.attrs import LinkerAttr as A
def combineAndCluster(geo):
# combine the geometry
newGeo = pm.duplicate(geo)
combinedMesh = pm.polyUnite(newGeo, ch=False)[0]
# cluster the combined mesh
transforms = []
lastVertIndex = 0
for g in geo:
# create group for cluster
grp = pm.group(em=True, n='{0}_clusterGrp'.format(g.nodeName()))
mtx = butterfly.utils.getWorldMatrix(g)
butterfly.utils.setWorldMatrix(grp, mtx, matchAxes=True)
pos = pm.xform(g, q=True, ws=True, a=True, rp=True)
grp.setTranslation(pos, 'world')
# duplicate grp for offset
grp2 = pm.duplicate(grp)[0]
grp2.rename('{0}_clusterGrpOffset'.format(g.nodeName()))
grp.setParent(grp2)
transforms.append(grp)
# get vertices for this shell
thisGeo = g.getShape()
vertCount = len(thisGeo.vtx)
verts = combinedMesh.vtx[lastVertIndex:lastVertIndex+vertCount-1]
# cluster the verts
clstr, clstrHandle = pm.cluster(verts)
clstrHandle.setParent(grp)
# increment vert index for next round
lastVertIndex += vertCount
pm.select(transforms)
return transforms
geo = pm.selected()
transforms = combineAndCluster(geo)
ctl = pm.selected()[0]
def blendTransformRotateExpression(ctl, trans, maxX=5):
for t in trans:
# get translate.x in the space of the control
# and use it to blend the splay rotation
relMtx = butterfly.utils.getRelativeMatrix(ctl, t)
pos = relMtx[3]
# rotate x + rotate Y with splay + translate Z only in the center
ryMult = pos[0] / maxX
tzMult = max(maxX - abs(pos[0]), 0) * 20
A(ctl.rx) * 2 + A(ctl.ry)*ryMult + A(ctl.tz)*tzMult >> t.rx
A(ctl.rz) >> t.rotateZ
A(ctl.tx) * 20 >> t.ry
blendTransformRotateExpression(ctl, transforms)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment