Skip to content

Instantly share code, notes, and snippets.

@apokellypse
Last active April 8, 2019 05:47
Show Gist options
  • Save apokellypse/e82d751a903edf3a8d464e273bfe8573 to your computer and use it in GitHub Desktop.
Save apokellypse/e82d751a903edf3a8d464e273bfe8573 to your computer and use it in GitHub Desktop.
Separate Faces Into Objects Along Mesh Height
### iterative_polyseparate.py
### author: Kelly Yu
import maya.cmds as cmds
cmds.select(clear=True)
# select a list of objects starting with 'bunny' and get the first element
bunny = cmds.ls('bunny')[0]
# retrieve face count
bunny_face_count = cmds.polyEvaluate(bunny, face=True)
slices_map = {}
for i in xrange(bunny_face_count):
# get something like bunny.f[333]
face_name = bunny + '.f[%d]' % i
# select the specific face
cmds.select(face_name, replace=True)
# find the bounding box info of this face, specifically where it lies on the y axis
((_,_), (ymin,ymax), (_,_)) = cmds.polyEvaluate(face_name, boundingBoxComponent=True)
# assign a slice number
slice_number = int(ymax/0.135 + 1)
# init the list in the dict if necessary
if slice_number not in slices_map:
slices_map[slice_number] = []
# add the face to the right slice
slices_map[slice_number].append(face_name)
for slice in slices_map.keys():
# select all the faces in this slice
cmds.select(slices_map[slice], replace=True)
### psuedocode starts here ###
# separate faces from bunny mesh
name_of_separated_mesh, name_of_remaining_mesh = cmds.polySeparate(useSelected=True)
# rename the remaining mesh to be the original name (bunny) so the keys in slices_map still work
cmds.select(name_of_remaining_mesh, replace=True)
cmds.rename('bunny', useSelected=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment